//Template--------BYHottey2004-4-13-0:18//
// Author's website: http://asp.itdrp.com/hottey //
PRogramdelphi;
uses
windows,
messages;
const
hellostr='HelloWorld!';
{$Rdelphi.res}
//Window message processing function.
functionMyWinProc(hWnd:THandle;uMsg:UINT;wParam,lParam:Cardinal):Cardinal;exp
ort;stdcall;
var
hdca,hdcb:THandle;// Device description table handle.
rect:TRect;//Rectangular structure.
font:HFont;
ps:TPaintStruct;//Drawing structure.
Begin
result:=0;
caseuMsgof
WM_PAINT:
Begin
hdca:=BeginPaint(hWnd,ps);
SetBkMode(hdca,Transparent);
SetBkColor(hdca,GetBkColor(hdca));
GetClientRect(hWnd,rect);//Get the size of the window client area.
DrawText(hdca,Pchar(hellostr),-1,rect,DT_SINGLELINEorDT_CENTERorDT
_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,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
//Use the default window message processing function.
result:=DefWindowProc(hWnd,uMsg,wParam,lParam);
end;
end;
//The main program starts.
var
Msg:TMsg;//Message structure.
hWnd,hInst:THandle;//Windows window handle.
WinClass:TWndClassEx;//Windows window class structure.
Begin
hInst:=GetModuleHandle(nil);//gettheapplicationinstance
WinClass.cbSize:=SizeOf(TWndClassEx);
WinClass.lpszClassName:='MyWindow';//Class name.
WinClass.style:=CS_HREDRAWorCS_VREDRAWorCS_OWNDC;
WinClass.hInstance:=hInst;//The instance handle of the program.
//Set window message processing function.
WinClass.lpfnWndProc:=@MyWinProc;//Window process.
WinClass.cbClsExtra:=0;//The following two domains are used in class structure and Window
The window structure saved inside s
WinClass.cbWndExtra:=0;//Reserve some extra space in it.
WinClass.hIcon:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
WinClass.hIconsm:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
WinClass.hCursor:=LoadCursor(0,IDC_Arrow);
//GetStockObject gets a graphic object, here is to get the brush that draws the window background, returning a white brush
The child's handle.
WinClass.hbrBackground:=HBRUSH(GetStockObject(white_Brush));
WinClass.lpszMenuName:=nil;//Specify the window class menu.
//Register window class with Windows.
ifRegisterClassEx(WinClass)=0then
Begin
MessageBox(0,'RegisterationError!','SDK/API',MB_OK);
Exit;
end;
//Create window object.
hWnd:=CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,//Extended window style.
WinClass.lpszClassName,//Class name.
'HelloWindow',//Window title.
WS_OVERLAPPEDWINDOW,//Window style.
CW_USEDEFAULT,//The upper left corner of the window is relative to the screen
The initial position x in the upper left corner.
0,//....Right y.
CW_USEDEFAULT,//Window width x.
0,//Window height y.
0,// Parent window handle.
0,//Window menu handle.
hInst,//Program instance handle.
nil);//Create parameter pointer.
ifhWnd<>0then
Begin
ShowWindow(hWnd,SW_SHOWNORMAL);//Show window.
UpdateWindow(hWnd);//Instruction window to refresh itself.
SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE+SWP_NOSIZE);
end
else
MessageBox(0,'WindownotCreated!','SDK/API',MB_OK);
//Main message loop program.
whileGetMessage(Msg,0,0,0)do
Begin
TranslateMessage(Msg);//Convert certain keyboard messages.
DispatchMessage(Msg);//Send messages to the window process.
end;
end.
>In fact, Windows programming is something that everyone who learns to write programs must master. It is best to learn Windows programming first when learning Delphi.
The above code is not as fast as New->Form is used in Delphi, but it can tell you this
Qualified things can help you better understand the message loop and other things. These are the parts that the New form covers
.
>Note: The above code is literally translated from Windows programming () through C++ syntax, and there is no problem after testing.
If there are any errors in the annotation, please correct me!^_^
hottey on 2004-5-19 author website: http://asp.itdrp.com/hottey (by routine)