// Delphi 下Windows API 创建窗体. //
// 模板-------BY Hottey 2004-4-13-0:18 //
// 作者网站WebSite:http://asp.itdrp.com/hottey //
program delphi;
uses
windows,
messages;
const
hellostr='Hello World!';
{$R delphi.res}
//窗口消息处理.
function MyWinProc(hWnd:THandle;uMsg:UINT;wParam,lParam:Cardinal):Cardinal;exp
ort;stdcall;
var
hdca,hdcb:THandle; //设备描述表句柄.
rect:TRect; //矩形结构.
font:HFont;
ps:TPaStruct; //绘图结构.
begin
result:=0;
uMsg of
WM_PAINT:
begin
hdca:=BeginPa(hWnd,ps);
SetBkMode(hdca, Transparent);
SetBkColor(hdca,GetBkColor(hdca));
GetClientRect(hWnd,rect); //获取窗口客户区尺寸.
DrawText(hdca,Pchar(hellostr),-1,rect,DT_SINGLELINE or DT_CENTER or DT
_VCENTER);
// TextOut(hdc,100,40,hellostr,Length(hellostr));
EndPa(hWnd,ps);
end;
WM_CREATE:
begin
hdcb := GetDC(hWnd);
font := CreateFont(45, 0, 0, 0, FW_normal, 0, 0, 0, ansi_char, out
_default_precis, clip_default_precis,
default_quality, 34, PChar('Arial'));
SelectObject(hdcb, font);
ReleaseDC(hWnd, hdcb);
end;
WM_DESTROY:
PostQuitMessage(0)
//使用缺省窗口消息处理.
result:=DefWindowProc(hWnd,uMsg,wParam,lParam);
end;
end;
//主开始.
var
Msg :TMsg; //消息结构.
hWnd,hInst :THandle; //Windows 窗口句柄.
WinClass :TWndClassEx; //Windows 窗口类结构.
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; //例子句柄.
//设置窗口消息处理.
WinClass.lpfnWndProc:=@MyWinProc; //窗口过程.
WinClass.cbClsExtra:=0; //以下两个域用于在类结构和Window
s内部保存窗口结构
WinClass.cbWndExtra:=0; //中预留些额外空间.
WinClass.hIcon:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
WinClass.hIconsm:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
WinClass.hCursor:=LoadCursor(0,IDC_Arrow);
//GetStockObject 获取个图形对象,在这里是获取绘制窗口背景刷子,返回个白色刷
子句柄.
WinClass.hbrBackground:=HBRUSH(GetStockObject(white_Brush));
WinClass.lpszMenuName:=nil; //指定窗口类菜单.
//向Windows 注册窗口类.
RegisterClassEx(WinClass)=0 then
begin
MessageBox(0,'Registeration Error!','SDK/API',MB_OK);
Exit;
end;
//建立窗口对象.
hWnd:=CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW, //扩展窗口风格.
WinClass.lpszClassName, //类名.
'Hello Window', //窗口标题.
WS_OVERLAPPEDWINDOW, //窗口风格.
CW_USEDEFAULT, //窗口左上角相对于屏幕
左上角位置x.
0, //....右y.
CW_USEDEFAULT, //窗口宽度x.
0, //窗口高度y.
0, //父窗口句柄.
0, //窗口菜单句柄.
hInst, //例子句柄.
nil); //创建参数指针.
hWnd<>0 then
begin
ShowWindow(hWnd,SW_SHOWNORMAL); //显示窗口.
UpdateWindow(hWnd); //指示窗口刷新自己.
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
end
MessageBox(0,'Window not Created!','SDK/API',MB_OK);
//主消息循环.
while GetMessage(Msg,0,0,0) do
begin
TranslateMessage(Msg); //转换某些键盘消息.
DispatchMessage(Msg); //将消息发送给窗口过程.
end;
end.
>其实Windows 编程是每个学写人都要掌握,学Delphi时也最好能先学习Windos编
程(最少要知道).以上代码虽说不如在Delphi中直接来个New->Form来快,但它能告诉你本
质东西.能让你更好了解消息循环以及其他.而这些正是让New出来窗体给掩盖部分
.
>注:以上代码是我从Windows 设计上通过C语法直译过来,测试后没有问题.若我
注解有什么地方,请各位指正!^_^
hottey 于2004-5-19
作者网站WebSite:http://asp.itdrp.com/hottey (附例程)
最新评论