DELPHI怎样模拟c++来创建窗口
DELPHI怎样模拟c++来创建窗口
program Project1;
uses
Windows;
var
wc: WNDCLASSEX ;
hMod: HINST ;
hMainWnd: HWND ;
w:integer ;
mydc:hdc;
a:pdevmode;
function adMainWndProc (hWnd:HWND; uMsgID:UINT; wParam:WPARAM; lParam:LPARAM):LRESULT; stdcall;
begin
if uMsgID = HSHELL_WINDOWDESTROYED then {WM_DESTROY}
begin
PostQuitMessage (0) ;
adMainWndProc := 0
end
else
adMainWndProc := DefWindowProc (hWnd, uMsgID, wParam, lParam) ;
end;
procedure adPumpMessage (hWnd: HWND); {消息循环}
var
stmsg: MSG;
begin
while GetMessage(stmsg, hWnd, 0, 0) do
begin
if stmsg.hwnd = 0 then Break;//
TranslateMessage (stmsg) ;
DispatchMessage (stmsg) ;
end;
end;
begin
hMod := GetModuleHandle (nil);
with wc do
begin
cbSize := SizeOf (WNDCLASSEX) ; {此函数在system单元中}
style := CS_HREDRAW or CS_VREDRAW ;
lpfnWndProc := @adMainWndProc ;
cbClsExtra := 0 ;
cbWndExtra := 0 ;
hInstance := hMod ;
hIcon := LoadIcon (0, IDI_APPLICATION) ;
hIconSm := hIcon ;
hCursor := LoadCursor (0, IDC_ARROW) ;
hbrBackground := COLOR_WINDOW + 1 ;
lpszMenuName := nil ;
lpszClassName := 'MyClass' ;
end ;
RegisterClassEx (wc) ;
hMainWnd := CreateWindowEx (0, 'MyClass', 'First Window', WS_OVERLAPPEDWINDOW,200, 200, 600, 400, 0, 0, hMod, nil) ;
ShowWindow (hMainWnd, SW_SHOW) ;
UpdateWindow (hMainWnd) ;
adPumpMessage (hMainWnd)
end. 我从网上找的,这样对吗?
还有,怎样在画个圆?
DC是什么?
[解决办法]
- Delphi(Pascal) code
program Project1;uses Forms, Windows, SysUtils, Messages, Dialogs;type TMyForm = class(TObject) private winclass:WNDCLASSA; hwindow:HWND; Amsg:TMsg; FAppName:string; FWndproc:TFNAPCProc; function winregister():Boolean; procedure CreateMyWindow; public constructor Create; destructor Destroy;override; procedure WinCreate; procedure MyRun; procedure CreateButton(x,y:Integer;className,windowName:string); property ApplicationName : string read FAppName write FAppName; property MyProcedure : TFNAPCProc read FWndproc write FWndproc; end;const AppName = 'Myclass';var Mywindow:TMyForm;{ TMyForm }constructor TMyForm.Create;beginend;procedure TMyForm.CreateButton(x,y:Integer;className,windowName:string);var hMyButton:HWND;begin hMyButton:=CreateWindow(PAnsiChar(className),PAnsiChar(windowName),WS_VISIBLE or WS_Child or BS_Bitmap, x,y,65,24,hwindow,0,system.MainInstance,nil); ShowWindow(hMyButton,CmdShow); ShowWindow(hMyButton,SW_SHOWNORMAL); UpdateWindow(hMyButton);end;procedure TMyForm.CreateMyWindow;begin hwindow := CreateWindowEx(WS_EX_CLIENTEDGE,PAnsiChar(FAppName),AppName,WS_OVERLAPPEDWINDOW,100, 100,600,400,0,0,SYSTEM.MainInstance,nil); // hwindow := CreateWindow(AppName,'创建窗口',WS_OVERLAPPED,CW_USEDEFAULT, // CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,SYSTEM.MainInstance,nil); if hwindow <> 0 then begin ShowWindow(hwindow,CmdShow); ShowWindow(hwindow,SW_SHOWNORMAL); UpdateWindow(hwindow); CreateButton(75,175,'BUTTON','Mybutton1'); CreateButton(75,275,'BUTTON','Mybutton2'); end;end;destructor TMyForm.Destroy;begin inherited;end;procedure TMyForm.MyRun;begin while GetMessage(Amsg,0,0,0) do begin TranslateMessage(Amsg); DispatchMessage(Amsg); end; Halt(Amsg.wParam);end;procedure TMyForm.WinCreate;begin if winregister then begin CreateMyWindow; end;end;function TMyForm.winregister: Boolean;begin winclass.style := CS_HREDRAW or CS_VREDRAW; winclass.lpfnWndProc := FWndproc; winclass.cbClsExtra := 0; winclass.cbWndExtra := 0; winclass.hInstance := System.MainInstance; winclass.hIcon := LoadIcon(0,IDI_APPLICATION); winclass.hCursor := LoadCursor(0,IDC_ARROW); winclass.hbrBackground := GetStockObject(WHITE_BRUSH); winclass.lpszMenuName := nil; winclass.lpszClassName := PChar(FAppName); Result := RegisterClass(winclass)<> 0;end;function WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;var dc:HDC; ps:TPaintStruct; r:TRect; Curxy:TPoint;begin WindowProc := 0 ; case umsg of WM_PAINT: begin dc := BeginPaint(hwnd,ps); GetClientRect(hwnd,r); DrawText(dc,'哈哈哈',-1,r,DT_SINGLELINE or DT_CENTER or DT_VCENTER); EndPaint(hwnd,ps); Exit; end; WM_DESTROY: begin PostQuitMessage(0); Exit; end; WM_COMMAND: begin if GetCursorPos(Curxy) then if Curxy.Y < 350 then begin ShowMessage('按钮1按下了'); end else begin ShowMessage('按钮2按下了'); end; end; end; WindowProc := DefWindowProc(hwnd,umsg,wParam,lParam); end;begin Mywindow := TMyForm.Create; Mywindow.ApplicationName := AppName; Mywindow.FWndproc := TFNAPCProc(@WindowProc); Mywindow.WinCreate; try Mywindow.MyRun; // Mywindow.CreateButton; finally FreeAndNil(Mywindow); end; end.