1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #define WM_UPDATE_DATA (WM_APP + 7)
// Decendant de TControl donc pas de Handle
class TTestLabel : public TCustomLabel
{
__published:
__property Caption;
private:
void __fastcall WindowMethod(TMessage &Msg); // Metode de reponse
public:
HWND FWindow; // Handle sur le methode de reponse
__fastcall virtual TTestLabel(Classes::TComponent* AOwner);
__fastcall virtual ~TTestLabel(void);
};
//---------------------------------------------------------------------------
__fastcall TTestLabel::TTestLabel(Classes::TComponent* AOwner)
: TCustomLabel (Owner)
{
FWindow = AllocateHWnd(WindowMethod); // Cree un Handle
}
//---------------------------------------------------------------------------
__fastcall TTestLabel::~TTestLabel(void)
{
DeallocateHWnd(FWindow); // detruit le Handle
}
//---------------------------------------------------------------------------
void __fastcall TTestLabel::WindowMethod(TMessage &Msg)
{
if (Msg.Msg == WM_UPDATE_DATA)
{
if( Msg.WParam )
strcpy((char *)Msg.LParam, Caption.c_str());
else
Caption = (char *)Msg.LParam;
Msg.Result = DefWindowProc(FWindow, Msg.Msg, Msg.WParam, Msg.LParam);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char buf[50];
// Lecture du controle
SendMessage(TestLabel->FWindow ,WM_UPDATE_DATA, 1, (long)buf);
Label1->Text = buf;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
char buf[50];
// Ecriture du controle
strcpy(buf, Edit1->Text.c_str());
SendMessage(TestLabel->FWindow ,WM_UPDATE_DATA, 0, (long)buf);
} |