Use Windows API to create a form in Delphi
Subtitle:
Author: famous author Source: monopoly hits: 119 updated on:
Use Windows API to create a form in Delphi
// Call Windows API to create a form in Delphi .//
// Template ------- by hottey 2004-4-13-0: 18 //
// Author's website: http://asp.itdrp.com/hottey //
Program Delphi;
Uses
Windows,
Messages;
Const
Hellostr = 'Hello world! ';
{$ R delphi. Res}
// Window message processing function.
Function mywinproc (hwnd: thandle; umsg: uint; wparam, lparam: Cardinal): Cardinal; exp
ORT; stdcall;
VaR
HDCA, hdcb: thandle; // handle of the device description table.
Rect: trect; // rectangular structure.
Font: hfont;
PS: tpaintstruct; // drawing structure.
Begin
Result: = 0;
Case umsg
Wm_paint:
Begin
HDCA: = beginpaint (hwnd, PS );
Setbkmode (HDCA, transparent );
Setbkcolor (HDCA, getbkcolor (HDCA ));
Getclientrect (hwnd, rect); // obtain the size of the customer area in the window.
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
// 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); // get the application instance
Winclass. cbsize: = sizeof (twndclassex );
Winclass. lpszclassname: = 'mywindow'; // class name.
Winclass. Style: = cs_hredraw or cs_vredraw or cs_owndc;
Winclass. hinstance: = hinst; // instance handle of the program.
// Set the window message processing function.
Winclass. lpfnwndproc: = @ mywinproc; // window process.
Winclass. cbclsextra: = 0; // The following two fields are used in the class structure and window
Window structure saved in S
Winclass. cbwndextra: = 0; // reserve some extra space.
Winclass. hicon: = loadicon (hinstance, makeintresource ('mainicons '));
Winclass. hiconsm: = loadicon (hinstance, makeintresource ('mainicons '));
Winclass. hcursor: = loadcursor (0, idc_arrow );
// Getstockobject: Get a graphic object. Here, it is the paint brush for drawing the background of the window. A white brush is returned.
Sub-handle.
Winclass. hbrbackground: = hbrush (getstockobject (white_brush ));
Winclass. lpszmenuname: = nil; // specify the window class menu.
// Register the window class with windows.
If registerclassex (winclass) = 0 then
Begin
MessageBox (0, 'registeration error! ', 'Sdk/API', mb_ OK );
Exit;
End;
// Create a window object.
Hwnd: = createmediawex (
Ws_ex_overlappedwindow, // extended window style.
Winclass. lpszclassname, // class name.
'Hello Window', // window title.
Ws_overlappedwindow, // window style.
Cw_usedefault, // relative to the screen in the upper left corner of the window
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 a parameter pointer.
If hwnd <> 0 then
Begin
Showwindow (hwnd, sw_shownormal); // display window.
Updatewindow (hwnd); // indicates the window to refresh itself.
Setwindowpos (hwnd, hwnd_topmost, 0, 0, 0, 0, swp_nomove + swp_nosize );
End
Else
MessageBox (0, 'window not created! ', 'Sdk/API', mb_ OK );
// Main message loop program.
While getmessage (MSG, 0, 0) Do
Begin
Translatemessage (MSG); // convert some keyboard messages.
Dispatchmessage (MSG); // sends the message to the window.
End;
End.