我有如下编的工程文件,是API创建窗口和一些鼠标事件的,现在我想把它转换成在DELPHI FORM窗口用一个BUTTON点击事件创建。大家帮帮忙program ProjectSp;
uses
Windows, Messages;
const
ClassName = 'KingStarGroup';
var
AMessage: MSG;
HMyWnd: HWND;
MyPChar: PChar;
{$R *.res}procedure OnChar;
var
HMyDc: HDC;
begin
MessageBox(0, MyPChar, ClassName, MB_OK);
HMyDc := GetDC(HMyWnd);
TextOut(HMyDc, 100, 100, MyPChar, Length(MyPChar));
end;procedure OnPaint;
var
HMyDc: HDC;
begin
HMyDc := GetDC(HMyWnd);
Rectangle(HMyDc, 100, 100, 200, 200);
TextOut(HMyDc, 0, 0, ClassName, Length(ClassName));
end;function WindowProc (Window:HWnd; AMessage: UINT; WParam:WPARAM; LParam:LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
case AMessage of
WM_PAINT:
OnPaint;
WM_CLOSE:
begin
if MessageBox(0, 'Are you want to close the window?', ClassName, MB_YESNO or
MB_ICONINFORMATION) = ID_NO then
Exit;
end;
WM_DESTROY:
PostQuitMessage(0);
WM_CHAR:
begin
MyPChar := @PChar(WParam);
OnChar;
end;
end;
Result := DefWindowProc(Window, AMessage, WParam, LParam);
end;function WinRegister: Boolean;
var
MyWndCls : WNDCLASS;
begin
with MyWndCls do
begin
style := CS_HREDRAW or CS_VREDRAW;
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := MainInstance;
hIcon := LoadIcon(0, IDI_APPLICATION);
hCursor := LoadCursor(0, IDC_ARROW);
hbrBackground := GetStockObject(WHITE_BRUSH);
lpszMenuName := nil;
lpszClassName := ClassName;
lpfnWndProc := TFNWndProc(@WindowProc);
end;
Result := RegisterClass(MyWndCls) <> 0;
end;function CreateMyWnd: HWND;
begin
Result := CreateWindow(ClassName, ClassName, WS_OVERLAPPEDWINDOW,
300, 300, 400, 400, 0, 0, MainInstance, nil);
end;begin
if not WinRegister then
begin
MessageBox(0, 'Register WindowClass failed!', ClassName, MB_OK or MB_ICONERROR);
Exit;
end;
HMyWnd := CreateMyWnd;
ShowWindow(HMyWnd, SW_NORMAL);
UpdateWindow(HMyWnd);
while GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
end.
uses
Windows, Messages;
const
ClassName = 'KingStarGroup';
var
AMessage: MSG;
HMyWnd: HWND;
MyPChar: PChar;
{$R *.res}procedure OnChar;
var
HMyDc: HDC;
begin
MessageBox(0, MyPChar, ClassName, MB_OK);
HMyDc := GetDC(HMyWnd);
TextOut(HMyDc, 100, 100, MyPChar, Length(MyPChar));
end;procedure OnPaint;
var
HMyDc: HDC;
begin
HMyDc := GetDC(HMyWnd);
Rectangle(HMyDc, 100, 100, 200, 200);
TextOut(HMyDc, 0, 0, ClassName, Length(ClassName));
end;function WindowProc (Window:HWnd; AMessage: UINT; WParam:WPARAM; LParam:LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
case AMessage of
WM_PAINT:
OnPaint;
WM_CLOSE:
begin
if MessageBox(0, 'Are you want to close the window?', ClassName, MB_YESNO or
MB_ICONINFORMATION) = ID_NO then
Exit;
end;
WM_DESTROY:
PostQuitMessage(0);
WM_CHAR:
begin
MyPChar := @PChar(WParam);
OnChar;
end;
end;
Result := DefWindowProc(Window, AMessage, WParam, LParam);
end;function WinRegister: Boolean;
var
MyWndCls : WNDCLASS;
begin
with MyWndCls do
begin
style := CS_HREDRAW or CS_VREDRAW;
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := MainInstance;
hIcon := LoadIcon(0, IDI_APPLICATION);
hCursor := LoadCursor(0, IDC_ARROW);
hbrBackground := GetStockObject(WHITE_BRUSH);
lpszMenuName := nil;
lpszClassName := ClassName;
lpfnWndProc := TFNWndProc(@WindowProc);
end;
Result := RegisterClass(MyWndCls) <> 0;
end;function CreateMyWnd: HWND;
begin
Result := CreateWindow(ClassName, ClassName, WS_OVERLAPPEDWINDOW,
300, 300, 400, 400, 0, 0, MainInstance, nil);
end;begin
if not WinRegister then
begin
MessageBox(0, 'Register WindowClass failed!', ClassName, MB_OK or MB_ICONERROR);
Exit;
end;
HMyWnd := CreateMyWnd;
ShowWindow(HMyWnd, SW_NORMAL);
UpdateWindow(HMyWnd);
while GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
end.
form2:=tform2.create(application);
form2.show;
begin
Form1 := TForm1.Create(Application);
Form1.ShowModal;
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
const
ClassName = 'KingStarGroup';
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
mypchar:pchar;
implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
self.KeyPreview:=true;
self.Caption:=classname;
self.Left:=300;
self.Top:=300;
self.Width:=400;
self.Height:=400;
end;procedure TForm1.FormPaint(Sender: TObject);
begin
self.Canvas.Rectangle(100, 100, 200, 200);
self.Canvas.TextOut(0, 0, ClassName);
end;procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
showmessage(key);
self.Canvas.TextOut(100, 100, key);
end;end.
procedure TMainFrm.Button1Click(Sender: TObject);
begin
Form1:=TForm1.Create(Application);
Form1.ShowModal;
Form1.free;
end;