Create a form with Windows API under Delphi

Source: Internet
Author: User

Delphi down use Windows API to create a form.//

Program Delphi;

Uses
Windows
Messages
Const
Hellostr= ' Hello world! ';
{$R Delphi.res}
Window Message handler function.
function Mywinproc (hwnd:thandle;umsg:uint;wparam,lparam:cardinal): Cardinal;exp
Ort;stdcall;
Var
Hdca,hdcb:thandle; The device description table handle.
Rect:trect; Rectangular structure.
Font:hfont;
Ps:tpaintstruct; The drawing structure.
Begin
result:=0;
Case Umsg of
WM_PAINT:
Begin
Hdca:=beginpaint (HWND,PS);
SetBkMode (HDCA, transparent);
SetBkColor (Hdca,getbkcolor (HDCA));
GetClientRect (Hwnd,rect); Gets the size of the client area of 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 (0, 0, 0, fw_normal, 0, 0, 0, Ansi_charset, out
_default_precis, Clip_default_precis,
Default_quality, Pchar (' Arial '));
SelectObject (HDCB, font);
ReleaseDC (HWnd, HDCB);
End
Wm_destroy:
PostQuitMessage (0)
Else
Use the default window message handler 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; The instance handle of the program.
Sets the window message handler function.
Winclass.lpfnwndproc:= @MyWinProc; The window process.
winclass.cbclsextra:=0; The following two fields are used in the class structure and window
s internal saved window structure
winclass.cbwndextra:=0; To reserve some extra space.
Winclass.hicon:=loadicon (Hinstance,makeintresource (' Mainicon '));
Winclass.hiconsm:=loadicon (Hinstance,makeintresource (' Mainicon '));
Winclass.hcursor:=loadcursor (0,idc_arrow);
Getstockobject gets a drawing object, here is the brush that gets the background of the drawing window, and returns a white brush
The handle of the child.
Winclass.hbrbackground:=hbrush (Getstockobject (White_brush));
Winclass.lpszmenuname:=nil; Specifies the window class menu.
Registers the window class with Windows.
If RegisterClassEx (winclass) =0 Then
Begin
MessageBox (0, ' registeration error! ', ' Sdk/api ', MB_OK);
Exit;
End
Creates a Window object.
Hwnd:=createwindowex (
Ws_ex_overlappedwindow,//Extended window style.
Winclass.lpszclassname,//class name.
' Hello window ',//window title.
Ws_overlappedwindow,//window style.
Cw_usedefault,//window top left corner relative to 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); Creates a parameter pointer.
If Hwnd<>0 Then
Begin
ShowWindow (Hwnd,sw_shownormal); Displays the window.
UpdateWindow (HWND); Indicates that the window refreshes itself.
SetWindowPos (hWnd, hwnd_topmost, 0, 0, 0, 0, swp_nomove + swp_nosize);
End
Else
MessageBox (0, ' Window not created! ', ' Sdk/api ', MB_OK);
The main message loop program.
While GetMessage (msg,0,0,0) todo
Begin
TranslateMessage (MSG); Converts some keyboard messages.
DispatchMessage (MSG); Sends a message to the window process.
End
End.

In fact, Windows programming is every learn to write program people have to master, Learning Delphi is also best to learn WinDOS programming (at least to know). The above code is not as good as in Delphi directly to a new->form come fast, But it can tell you the nature of things. It gives you a better idea of the message loop and more. And these are the parts of the form that let new come out.

> Note: The above code is i from the Windows program design through C + + grammar literal translation (), after testing no problem. If there is anything wrong with my annotations, please correct me! ^_^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.