Delphi creates a form with WindowsAPI

xiaoxiao2021-03-06  39

// Delphi Upset Windows API creates a form. //

// Template ------- by Hottey 2004-4-13-0: 18 //

// Author 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

stdcall;

VAR

HDCA, HDCB: THANDLE; / / Device Description Table Handle.

RECT: TRECT; // Rectangular structure.

FONT: HFONT;

PS: TPAINTSTRUCT; // Draw structure.

Begin

Result: = 0;

Case UMSG of

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_SINGLINE 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, ant_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 begins.

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.

/ / Set the window message processing function.

WinClass.lpfnwndproc: = @ mywinproc; // window process.

WinClass.cbclsextra: = 0; // The following two domains are used to save the window structure internally in the class structure and Windows.

WinClass.cbWndextra: = 0; // Reserved some extra space.

Winclass.hicon: = loading (Hinstance, MakeintResource ('MAINICON');

WinClass.hiconsm: = loading (Hinstance, MakeintResource ('mainicon'));

WinClass.hcursor: = loadingcursor (0, IDC_ARROW);

// getStockObject Get a graphic object, here is a brush for drawing a window background, return a white brush

The handle of the child.

Winclass.hbrbackground: = Hbrush (getStockObject (White_brush));

WinClass.lpsz Gene: = nil; // Specifies the window class menu.

/ / Register the window class to the Windows.

IF registerclassex (winclass) = 0 THEN

Begin

Messagebox (0, 'Registeration Error!', 'SDK / API', MB_OK);

EXIT;

END;

// Set up 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, // The upper left corner of the window is relative to the screen

The initial position X in the upper left corner X.

0, // .... Right Y.

CW_USEDEFAULT, // Window Width X.

0, // Window height y.

0, // parent window handle.

0, // Window menu handle.

Hinst, // Program instant sequence.

NIL); // Create a parameter pointer.

IF hwnd <> 0 THEN

Begin

ShowWindow (hwnd, sw_shownormal); // Displays the window.

UpdateWindow (hwnd); // Indicates that the window refreshes yourself.

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) DO

Begin

TranslateMessage (MSG); // Convert some keyboard messages.

DispatchMessage (MSG); // Send the message to the window process.

End.

> In fact, Windows programming is a person who cares about every learning program, and it is best to learn Windos first when studying Delphi.

Cheng (least to know). Although the above code is worse than it is free to come directly in Delphi, it can tell you this

Quality. It can make you better understand the message loop and others. These are some of the forms that let NEW come to the cover

.

> Note: The above code is that I designated from the Windows program through C syntax (), there is no problem after testing. If I

What is wrong with your annotations, please refer to you! ^ _ ^

Hottey at 2004-5-19

Author website:

http://asp.itdrp.com/hottey (with)

转载请注明原文地址:https://www.9cbs.com/read-118448.html

New Post(0)