uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls,Dialogs;
var
WindowAtom: TAtom;
ControlAtom: TAtom;
type
TCreateParams = record
Caption: PChar;
Style: DWORD;
ExStyle: DWORD;
X, Y: Integer;
Width, Height: Integer;
WndParent: HWnd;
Param: Pointer;
WindowClass: TWndClass;
WinClassName: array[0..63] of Char;
end ;
type
ThandleLabel1 = class(TLabel)
private
FHandle: HWnd;
{ Private declarations }
protected
procedure CreateParams(var Params: TCreateParams); virtual;
procedure CreateWindowHandle(const Params: TCreateParams); virtual;
function GetHandle: HWnd;
procedure CreateHandle; virtual;
procedure AddBiDiModeExStyle(var ExStyle: DWORD);
procedure CreateWnd; virtual;
procedure TWMMOUSEMove (var msg :TWMMOUSEMove);message WM_MOUSEMOVE ;
{ Protected declarations }
public
property Handle: HWnd read GetHandle;
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Additional', [ThandleLabel1]);
end;
{ ThandleLabel1 }
procedure ThandleLabel1.AddBiDiModeExStyle(var ExStyle: DWORD);
begin
if UseRightToLeftReading then
ExStyle := ExStyle or WS_EX_RTLREADING;
if UseRightToLeftScrollbar then
ExStyle := ExStyle or WS_EX_LEFTSCROLLBAR;
if UseRightToLeftAlignment then
if GetControlsAlignment = taLeftJustify then
ExStyle := ExStyle or WS_EX_RIGHT
else if GetControlsAlignment = taRightJustify then
ExStyle := ExStyle or WS_EX_LEFT;
end;
procedure ThandleLabel1.CreateHandle;
begin
if FHandle = 0 then
begin
SetProp(FHandle, MakeIntAtom(ControlAtom), THandle(Self));
SetProp(FHandle, MakeIntAtom(WindowAtom), THandle(Self));
end;
end;
procedure ThandleLabel1.CreateParams(var Params: TCreateParams);
begin
FillChar(Params, SizeOf(Params), 0);
with Params do
begin
Caption := Pchar(self.Caption);
Style := WS_CHILD or WS_CLIPSIBLINGS;
AddBiDiModeExStyle(ExStyle);
Style := Style or WS_CLIPCHILDREN;
ExStyle := ExStyle or WS_EX_CONTROLPARENT;
if not (csDesigning in ComponentState) and not Enabled then
Style := Style or WS_DISABLED;
X := self.Left ;
Y := self.Top ;
Width := self.Width ;
Height := self.Height;
WndParent := self.Parent.Handle ;
WindowClass.style := CS_VREDRAW + CS_HREDRAW + CS_DBLCLKS;
WindowClass.lpfnWndProc := @DefWindowProc;
WindowClass.hCursor := LoadCursor(0, IDC_ARROW);
WindowClass.hbrBackground := 0;
WindowClass.hInstance := HInstance;
StrPCopy(WinClassName, ClassName);
end;
end;
procedure ThandleLabel1.CreateWindowHandle(const Params: TCreateParams);
begin
FHandle := CreateWindowEx(Params.ExStyle, Params.WinClassName, Params.Caption, Params.Style,
Params.X,Params.Y, Params.Width, Params.Height, Params.WndParent, 0, Params.WindowClass.hInstance, Params.Param);
end;
procedure ThandleLabel1.CreateWnd;
var
Params: TCreateParams;
begin
CreateParams(Params);
CreateWindowHandle(Params);
if FHandle = 0 then showmessage('aaa');
end;
function ThandleLabel1.GetHandle: HWnd;
begin
if FHandle = 0 then
begin
if Parent <> nil then Parent.HandleNeeded;
CreateHandle;
end ;
Result := FHandle;
end;
procedure ThandleLabel1.TWMMOUSEMove(var msg: TWMMOUSEMove);
begin
self.CreateWnd ;
end;
为什么handle值总为零