Entry function writing for Windows applications

Source: Internet
Author: User
Tags command line

Write any one function of course there should be a portal to the function. We know that the C-language entry function is the main () function, so is the write Windows application also main ()? The answer is of course not. We're using the WinMain () function, but we didn't seem to see the function when we wrote the MFC program, but it was Microsoft to help us write a windows-based application quickly and encapsulate it, of course we can find it. Here we try to write a WinMain () function of our own. All right, let's go to the theme! First, we want 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 messaging application, and we should first write a message response function, 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 up 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;//Registration window class
wndcls.cbclsextra=0;
wndcls.cbwndextra=0;
Wndcls.hbrbackground= (Hbrush) getstockobject (White_brush);//Window painting brush
Wndcls.hcursor=loadcursor (Null,idc_arrow);//mouse
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 redraw and vertical redraw)
RegisterClass (&AMP;WNDCLS);//Register window
After the window is registered, the window is created and displayed.
HWND hwnd;
Hwnd=createwindow ("BEYOND", Lovebeyond), Ws_overlappedwindow,
0,0,600,400,null,null,hinstance,null);
ShowWindow (Hwnd,sw_shownormal)//Display
UpdateWindow (hWnd);//update
Next up is the message loop.
MSG msg;
while (GetMessage (&msg,null,0,0))///When not wm_quit, continue message loop
{
TranslateMessage (&msg);//For translating messages
DispatchMessage (&msg);/Send Message
}
Return msg.wparam;//Returns a parameter
}
Message response function
Lresult CALLBACK Winmyproc (
HWND hwnd,//Handle to Window
UINT umsg,//message identifier
WPARAM WPARAM,//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 mouse button pressed
MessageBox (hWnd, "Mouse clicked", "message", 0);
Break
Case wm_paint://window Redraw message
HDC HDC;
Paintstruct PS;
Hdc=beginpaint (HWND,&AMP;PS);
TextOut (hdc,0,0, "Lovebeyond", strlen ("Lovebeyond"));
EndPaint (HWND,&AMP;PS);
Break
Case wm_close://Shutdown message
if (Idyes==messagebox (hWnd), would you like to end? "," message ", Mb_yesno))
{
DestroyWindow (HWND);
}
Break
Case wm_destroy://Destroy window message
PostQuitMessage (0);
Break
default://default window message handling
return DefWindowProc (Hwnd,umsg,wparam,lparam);
}
return 0;
}

By the end of all programs, readers can also try to write a similar program themselves, adding more response capabilities. That's the end of it. I want to help you understand the Windows program.

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.