Writing entry functions for Windows Applications

Source: Internet
Author: User
. We know that the entry function in the C language is the main () function, so it is also the main () function when writing Windows applications? Of course not. We use the WinMain () function, but we did not see this function when writing the MFC program. In fact, this is Microsoft's attempt to help us quickly write a Windows-based application, we can also encapsulate it. Here, we try to write a WinMain () function by ourselves. Now, let's go to the topic! First, we need to include some libraries.

#include <windows.h>#include <stdio.h>

Also, we should learn to use msdn to quickly find the information we need.

Here, windows is a message-based application. Program First, we should compile a message response function, that is, a callback function.

LRESULT CALLBACK WinMYProc(  HWND hWnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter);

Next is WinMain.

Int WINAPI WinMain (HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state) {WNDCLASS wndcls; // register wndcls. cbClsExtra = 0; wndcls. cbWndExtra = 0; wndcls. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); // wndcls for window painter. hCursor = LoadCursor (NULL, IDC_ARROW); // wndcls. hIcon = LoadIcon (NULL, IDI_WINLOGO );/ /Icon wndcls. hInstance = hInstance; // window instance wndcls. lpfnWndProc = WinMYProc; // Window Function wndcls. lpszClassName = "BEYOND"; // window class name wndcls. lpszMenuName = NULL; // menu name (empty here) wndcls. style = CS_HREDRAW | CS_VREDRAW; // window style (horizontal and vertical redrawing) ReGISterClass (& wndcls); // registration window // after registration, the following window is created and displayed: HWND hWnd; hWnd = CreateWindow ("BEYOND", lovebeyond ", WS_OVERLAPPEDWINDOW, 600,400, NULL, NULL, hInstance, NULL); ShowWindow (hwnd, SW_SHOWNORMAL); // display UpdateWindow (HWnd); // update // next, the message loops through MSG msg; while (GetMessage (& msg, NULL,) // when it is not WM_QUIT, continue message loop {TranslateMessage (& msg); // used to translate message DispatchMessage (& msg); // send message} return msg. wParam; // return a parameter} // message response function lresult callback WinMYProc (HWND hWnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter) {switch (uMsg) {case WM_CHAR: // Keyboard message char szChar [20 ]; Sprintf (szChar, "char is % d", wParam); MessageBox (hWnd, szChar, "char", 0); break; case WM_LBUTTONDOWN: // left-click MessageBox (hWnd, "mouse clicked", "message", 0); break; case WM_PAINT: // redraw HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint (hWnd, & ps); TextOut (hDC, "lovebeyond", strlen ("lovebeyond"); EndPaint (hwnd, & ps); break; case WM_CLOSE: // close the message if (IDYES = MessageBox (hWnd, "Do you want to end? "," Message ", MB_YESNO) {DestroyWindow (hWnd);} break; case WM_DESTROY: // destroy window message PostQuitMessage (0); break; default: // return DefWindowProc (hWnd, uMsg, wParam, lParam);} return 0 ;}

By now, when all programs are finished, readers can try to write similar programs and add more response functions. This ends. I hope to help you understand the windows program.

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.