Home > Software engineering >  MFC entry
MFC entry

Time:11-07

 # include & lt; Windows. H>//the underlying implementation of the window header file 

//6 processing window process

//CALLBACK representative __stdcall parameter order: order into the stack from right to left, and before the function returns the empty stack
LRESULT a CALLBACK WindowProc (
The HWND HWND,//the window handle of the message belongs to
UINT uMsg,//specific message name WM_XXXX message name
WPARAM WPARAM,//keyboard attached message
LPARAM LPARAM//mouse attached message
)
{
The switch (uMsg)
{
Case WM_CLOSE:
//all xxxWindow as at the end of the method, not into the message queue, but direct execution
The DestroyWindow (HWND);//DestroyWindow sends another message WM_DESTROY
break;
Case WM_DESTROY:
The PostQuitMessage (0);
break;
Case WM_LBUTTONDOWN://mouse the left key press
{
Int xPos=LOWORD (lParam);
Int yPos=HIWORD (lParam);

Char buf [1024].
Wsprintf (buf, TEXT (" x=% d, y=% d "), xPos, yPos);

MessageBox (HWND, buf, TEXT (" the left mouse button pressed "), MB_OK);

break;
}
Case WM_KEYDOWN://keyboard
MessageBox (HWND, TEXT (" the keyboard by pressing "), TEXT (" the keyboard by pressing "), MB_OK);
break;

Case the WM_PAINT://drawing
{
PAINTSTRUCT ps;//drawing structure
HDC HDC=BeginPaint (HWND, & amp; Ps);

TextOut (HDC, 100, 100, TEXT (" HELLO "), strlen (" HELLO "));

EndPaint (HWND, & amp; Ps);
}

break;
}




//the return value with the default handling
Return DefWindowProc (HWND, uMsg wParam, lParam);
}

//program entrance function

//WINAPI representative __stdcall parameter order: order into the stack from right to left, and before the function returns the empty stack
Int WINAPI WinMain (
HINSTANCE HINSTANCE,//the application instance handle
HINSTANCE hPrevInstance,//handle to an application, under the win32 environment parameters in general is NULL, do not work
LPSTR lpCmdLine,//char * argv []
Int nShowCmd)//show command to maximize, minimize normal
{

//1, the design window
//2, registration window
//3, create window
//4, display and update
//5, cycle through taking message
//6, process the message (window procedure)

//1, the design window
WNDCLASS wc.
Wc. CbClsExtra=0;//class additional memory
Wc. CbWndExtra=0;//window extra memory
Wc. HbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH);//set the background
Wc. HCursor=LoadCursor (NULL, IDC_HAND);//set the cursor if the first parameter is NULL, on behalf of the use of the system to provide the cursor
Wc. HIcon=LoadIcon (NULL, IDI_ERROR);//icon if the first parameter is NULL, on behalf of the use of the system to provide the cursor
The wc. The hInstance=hInstance;//application instance handle incoming in WinMain parameter can
Wc. LpfnWndProc=WindowProc;//callback function window procedure
Wc. LpszClassName=TEXT (" WIN ");//specified window class name
Wc. LpszMenuName=NULL;//the menu name
Wc. Style=0;//show 0 (the default style

//2, a registered window class
RegisterClass (& amp; Wc);

//3, create window
/*
LpClassName, class name
LpWindowName, title name
DwStyle, WS_OVERLAPPEDWINDOW style
Coordinates x, show CW_USEDEFAULT default
Y,
NWidth, wide high
NHeight,
HWndParent, parent window NULL
HMenu, menu NULL
Handle hInstance hInstance, instance
LpParam) added value NULL
*/

HWND HWND=CreateWindow (wc) lpszClassName, TEXT (" WINDOWS "), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

//4, display and update
ShowWindow (HWND, SW_SHOWNORMAL);
UpdateWindow (HWND);

//5, cycle through taking message
/*
The HWND HWND; The main window handle
UINT message; The specific message name
WPARAM WPARAM. Additional messages keyboard
LPARAM LPARAM; Additional messages mouse
DWORD time; Message generated time
POINT pt; Additional messages the mouse x y
*/
MSG MSG.

While (GetMessage (& amp; MSG, NULL, 0, 0))
{
/*
_Out_ LPMSG LPMSG, message
_In_opt_ HWND HWND, filling capture window NULL represent captures all window
_In_ UINT wMsgFilterMin,//the news of the minimum and maximum filtering generally fill 0
_In_ UINT wMsgFilterMax)//fill 0 (capture all messages
*/
//if (GetMessage (& amp; MSG, NULL, 0, 0)==FALSE)
//{
//break;
//}

//translation message
TranslateMessage (& amp; MSG);

//not to false
//distribute news
DispatchMessage (& amp; MSG);

}





return 0;
}

CodePudding user response:

What is the problem?
If simple window procedure with MFC directly or QT

It is not necessary to use winmain to painting,

CodePudding user response:

You just began to learn the basic winmain to write, understand the operation mechanism,

CodePudding user response:

I want to know what's the use of MFC specific, basic knowledge of what is required

CodePudding user response:

MFC in WINDOWS powerful, need a c + +, WINDOWS operating mechanism...
"Yi jin jing" slowly practice

CodePudding user response:

refer to the second floor ruan1978 response:
you have just started to learn, this foundation winmain to write or understand the operating mechanism,

I prefer the WIN32 (Windows desktop application), as well as your code, WIN32 is mainly message mechanism, such as a case, such as XX, MFC WIN32 can do it, you can do is just a matter of time, WIN32 than MFC will be a lot of freedom, the complexity is also low, medium/large development if they choose to do the WIN32.
Purely for small development (efficiency), using MFC, after all, can't spend a lot of time on small projects, MFC is to give you write good interface, only need to be concerned with the function, but the interface are ugly than WIN32.
If you really want to into the MFC WIN32 is a very important foundation, the people who is good at MFC WIN32 basis is certainly very good, so the WIN32 or start learning better.
Document:
https://docs.microsoft.com/zh-cn/cpp/mfc/mfc-desktop-applications? View=v - 2019
Notice: Microsoft has to a certain extent, to abandon the MFC, some content will not be updated,

CodePudding user response:

Has processed the message to return (not break)) don't calldef
  • Related