Practical
Learning: Introducing the Paint Box
|
|
- To create a new project, on the Tool Palette, click C++Builder
Projects and double-click VCL Forms Application
- In the Object Inspector, click Caption and type Picture
Viewer
- Click Name and type frmViewer
- Under the Code Editor, click Unit1.h
- Change the file as follows:
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TfrmViewer : public TForm
{
__published: // IDE-managed Components
TPaintBox *pbxViewer;
void __fastcall pbxViewerPaint(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
UnicodeString strFilename;
void __fastcall WMDropFiles(TWMDropFiles &Message);
public: // User declarations
__fastcall TfrmViewer(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WMDropFiles)
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmViewer *frmViewer;
//---------------------------------------------------------------------------
#endif
- Press F12 to display the form
- Double-click the middle of the form te generate its OnCreate event
- Change the file as follows:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmViewer *frmViewer;
//---------------------------------------------------------------------------
__fastcall TfrmViewer::TfrmViewer(TComponent* Owner)
: TForm(Owner)
{
strFilename = "";
}
//---------------------------------------------------------------------------
void __fastcall TfrmViewer::FormCreate(TObject *Sender)
{
DragAcceptFiles(Handle, True);
}
//---------------------------------------------------------------------------
void __fastcall TfrmViewer::WMDropFiles(TWMDropFiles &Message)
{
int iLength;
strFilename.SetLength(255);
iLength = DragQueryFile((HDROP)Message.Drop, 0,
strFilename.t_str(),
strFilename.Length());
strFilename.SetLength(iLength);
}
//---------------------------------------------------------------------------
- Press F12 to display the form
To support paint boxes, the VCL provides a class named
TPaintBox. The TPaintBox class is derived from the
TGraphicControl class:
To visually add a paint box to your application, in the
System section of the Tool Palette, click the TPaintBox button
and click the container that will own it. To programmatically create a paint
box, declare a variable of type TPaintBox and specify its
owner in the constructor.
Practical
Learning: Adding a Paint Box
|
|
- In the Tool Palette, click System
- Double-click TPaintBox button
- In the Object Inspector, click Name and type pbxViewer
|
|