Delphi下用Windows API建窗體
發(fā)布時(shí)間:2008-07-27 閱讀數(shù): 次 來(lái)源:網(wǎng)樂(lè)原科技
// Delphi 下調(diào)用Windows API 創(chuàng)建窗體. //
// 模板-------BY Hottey 2004-4-13-0:18 //
// 作者網(wǎng)站:http://asp.itdrp.com/hottey //
program delphi;
uses
windows,
messages;
const
hellostr='Hello World!';
{$R delphi.res}
//窗口消息處理函數(shù).
function MyWinProc(hWnd:THandle;uMsg:UINT;wParam,lParam:Cardinal):Cardinal;exp
ort;stdcall;
var
hdca,hdcb:THandle; //設(shè)備描述表句柄.
rect:TRect; //矩形結(jié)構(gòu).
font:HFont;
ps:TPaintStruct; //繪圖結(jié)構(gòu).
begin
result:=0;
case uMsg of
WM_PAINT:
begin
hdca:=BeginPaint(hWnd,ps);
SetBkMode(hdca, Transparent);
SetBkColor(hdca,GetBkColor(hdca));
GetClientRect(hWnd,rect); //獲取窗口客戶區(qū)的尺寸.
DrawText(hdca,Pchar(hellostr),-1,rect,DT_SINGLELINE or DT_CENTER or DT
_VCENTER);
// TextOut(hdc,100,40,hellostr,Length(hellostr));
EndPaint(hWnd,ps);
end;
WM_CREATE:
begin
hdcb := GetDC(hWnd);
font := CreateFont(45, 0, 0, 0, FW_normal, 0, 0, 0, ansi_charset, out
_default_precis, clip_default_precis,
default_quality, 34, PChar('Arial'));
SelectObject(hdcb, font);
ReleaseDC(hWnd, hdcb);
end;
WM_DESTROY:
PostQuitMessage(0)
else
//使用缺省的窗口消息處理函數(shù).
result:=DefWindowProc(hWnd,uMsg,wParam,lParam);
end;
end;
//主程序開始.
var
Msg :TMsg; //消息結(jié)構(gòu).
hWnd,hInst :THandle; //Windows 窗口句柄.
WinClass :TWndClassEx; //Windows 窗口類結(jié)構(gòu).
begin
hInst:=GetModuleHandle(nil); // get the application instance
WinClass.cbSize:=SizeOf(TWndClassEx);
WinClass.lpszClassName:='MyWindow'; //類名.
WinClass.style:=CS_HREDRAW or CS_VREDRAW or CS_OWNDC;
WinClass.hInstance:=hInst; //程序的實(shí)例句柄.
//設(shè)置窗口消息處理函數(shù).
WinClass.lpfnWndProc:=@MyWinProc; //窗口過(guò)程.
WinClass.cbClsExtra:=0; //以下兩個(gè)域用于在類結(jié)構(gòu)和Window
s內(nèi)部保存的窗口結(jié)構(gòu)
WinClass.cbWndExtra:=0; //中預(yù)留一些額外空間.
WinClass.hIcon:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
WinClass.hIconsm:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
WinClass.hCursor:=LoadCursor(0,IDC_Arrow);
//GetStockObject 獲取一個(gè)圖形對(duì)象,在這里是獲取繪制窗口背景的刷子,返回一個(gè)白色刷
子的句柄.
WinClass.hbrBackground:=HBRUSH(GetStockObject(white_Brush));
WinClass.lpszMenuName:=nil; //指定窗口類菜單.
//向Windows 注冊(cè)窗口類.
if RegisterClassEx(WinClass)=0 then
begin
MessageBox(0,'Registeration Error!','SDK/API',MB_OK);
Exit;
end;
//建立窗口對(duì)象.
hWnd:=CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW, //擴(kuò)展的窗口風(fēng)格.
WinClass.lpszClassName, //類名.
'Hello Window', //窗口標(biāo)題.
WS_OVERLAPPEDWINDOW, //窗口風(fēng)格.
CW_USEDEFAULT, //窗口左上角相對(duì)于屏幕
左上角的初始位置x.
0, //....右y.
CW_USEDEFAULT, //窗口寬度x.
0, //窗口高度y.
0, //父窗口句柄.
0, //窗口菜單句柄.
hInst, //程序?qū)嵗浔?
nil); //創(chuàng)建參數(shù)指針.
if hWnd<>0 then
begin
ShowWindow(hWnd,SW_SHOWNORMAL); //顯示窗口.
UpdateWindow(hWnd); //指示窗口刷新自己.
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
end
else
MessageBox(0,'Window not Created!','SDK/API',MB_OK);
//主消息循環(huán)程序.
while GetMessage(Msg,0,0,0) do
begin
TranslateMessage(Msg); //轉(zhuǎn)換某些鍵盤消息.
DispatchMessage(Msg); //將消息發(fā)送給窗口過(guò)程.
end;
end.
>其實(shí)Windows 編程是每個(gè)學(xué)寫程序的人都要掌握的,學(xué)Delphi時(shí)也最好能先學(xué)習(xí)Windos編
程(最少要知道).以上代碼雖說(shuō)不如在Delphi中直接來(lái)個(gè)New->Form來(lái)的快,但它能告訴你本
質(zhì)的東西.能讓你更好的了解消息循環(huán)以及其他.而這些正是讓New出來(lái)的窗體給掩蓋的部分
.
>注:以上代碼是我從Windows 程序設(shè)計(jì)上通過(guò)C++語(yǔ)法直譯過(guò)來(lái)的(),測(cè)試后沒(méi)有問(wèn)題.若我
的注解有什么錯(cuò)誤的地方,請(qǐng)各位指正!^_^
hottey 于2004-5-19
作者網(wǎng)站:http://asp.itdrp.com/hottey (附例程)