Write a Windows simple program using Delphi, SDK

zhaozj2021-02-11  176

Contact Delphi has also been a long time. It is very convenient to develop using Delphi, fast, Delphi itself provides a lot of components (more components on the Internet), which greatly reduces the workload of programmers. But the more convenient and more, the more works of Delphi itself do, and many people have not considered these, but also fully enjoying the shortcut and convenience of it. I am also a fan of C, I have a long time for learning and using C. After learning C / C , it is necessary to perform Windows programming, and there are two ways to use the SDK development package, and MFC There are also BCBs, and more, many people use SDK as a foundation, no matter how much or less, then gradually go to MFC, although it is not difficult to find the latter method. It is an improvement in the former method, with a gradual improving feeling, MFC is a package of API, which has brought huge convenience to procedural development.

There is a book "Windows programming" can be said to be a classic, I think many people have this book, the first complete example in the book is Hello, World, a simple form, with a Windows Form General feature, but wrote a lot of code. However, after learning the PASCAL syntax, using Delphi for Windows programming, it seems to be a bit jump to the use of VCL. For what news loop, message processing, but not involved, many reference books have not mentioned these, for me, I feel like it is C / C is born to develop Windows programs, seamless connection, PASCAL syntax is a slightest, it seems to be unsuitable (I can't feel at the beginning, I can't be able to use the SDK for Windows program development, then I checked it. A little matter, gradually explore, I found a little eyebrow, I hope to make progress with you. This example can be said to be a clone of the charles example, just the syntax is changed to Pascal, and there is also a message loop, message processing, and more here. There is also a detailed note to give the specific code of this program. I am divided into several processing functions for reading the relationship.

In the first time, it is also a new application and turn off the default window. There is also a code window. When the display prompt wants to save, select whether or not, then select the View source option in Project, skip to the code window, modify the code.

Program Project2;

Uses

Windows,

Messages;

VAR

MSG: TMSG;

WC: TwndClass; // registerclass () required by parameters

HWND: THANDLE; / / Handle of the main form

Const

Classname = 'mainwclass';

OutText = 'Hello';

Function mainwndproc (Handle: Thandle; Msgid: uint; wparam, lparam: integer): LRESULT; stdcall;

VAR

PDC: HDC;

LPRECT: TRECT;

Begin

RESULT: = 1;

Case msgid of

WM_CLOSE:

Begin // Close the message generated by the form

If MessageBox (Handle, 'Is it going to close this program?'

Else

Result: = 0;

EXIT;

END;

WM_DESTROY: // DestroyWindow () message generated

Begin

PostquitMessage (0);

END;

WM_PAINT:

Begin

PDC: = Getdc (hwnd);

GetClientRect (hwnd, lprect);

DrawText (PDC, OutText, 5, LPRECT, DT_SIINGLINE OR DT_CENTER OR DT_VCENTER);

// textout (PDC, 10, 10, OutText, 5);

ReleaseDC (HWND, PDC);

END;

END;

// The remaining message is handed over to the Windows preset handler, such as the WM_NCPAINT message of the form, etc.

Result: = DEFWINDOWPROC (Handle, Msgid, WPARAM, LPARAM);

END;

// Initialization, registration window class

Function INITAPPLICATION (Hinstance: Thandle): Boolean

Begin

// First use the registerclass () registration form, this is not the class in the Delphi data type.

Wc.style :=cs_hredraw or cs_vredraw;

Wc.lpfnwndproc: = @ mainwndproc; // message processing function

Wc.hinstance: = hinstance; // The handle of the program is also the base address

Wc.hicon: = loading (0, pchar (idi_application));

wc.hcursor: = loadingcursor (0, IDC_ARROW); // icon

wc.hbrbackground: = getStockObject (White_brush); // Background Paintbrush

wc.lpszclassname: = classname; // The context defined earlier

Result: = Boolean (RegisterClass (WC)); // Register this window class

END;

// InitInstance generated window

Function InitInstance (Hinstance: Thandle; NCMDSHOW: Integer): Boolean;

Begin

HWnd: = CREATEWINDOWEX (0,

Classname, // The name of the class that is just registered

'Example', // Form Title

WS_OVERLAPPEDWINDOW, // Form type, with title bar, system menu, maximization, minimize menu, and stretch border

Integer (CW_USEDEFAULT),

Integer (CW_USEDEFAULT),

Integer (CW_USEDEFAULT),

Integer (CW_USEDEFAULT),

0,

0,

Hinstance,

NIL

);

IF hWnd = 0 THEN

Begin

Result: = FALSE;

EXIT;

END;

ShowWindow (HWND, CMDSHOW);

UpdateWindow (HWND);

RESULT: = true;

END;

// Main program start

Begin

If not initApplication (hinstance) THEN HALT (0); // Initialization Registration window class

IF not initInstance (hinstance, cmdshow) Then Halt (0); // Generate Window While GetMessage (MSG, 0, 0) DO

Begin

TranslateMessage (MSG);

DispatchMessage (MSG); // This API assists a message to the corresponding form message processing function

END;

EXITCODE: = msg.wparam;

End.

The detailed code is given, which is drawn on the article, which is the most basic framework. If you use C to develop Windows program development, it is very familiar with the above code, it is true that it is really similar, and it is only a lot of variables. Writing is a bit different, and there are some attention to the function call.

I prefer to discuss with everyone, I like to investigate, I hope that you can discuss with me and make progress together.

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

New Post(0)