Home > Software engineering >  High marks for help, MFC wizard to create default program has several threads?
High marks for help, MFC wizard to create default program has several threads?

Time:11-25

I understand it is only a single thread, which is the main thread, but the internal code is very much, no uncertainty, so to come over to ask,

Below is my understanding: create global object theApp, then call the object in the afxwmain InitInstance method, in this method creates a dialog box (here with the dialog box procedure for example),

Domodal dialog operation, stop in there (block), from the point of this order, is the main thread all the way to perform these actions, but why the run can also run news cycle?
This problem is very confused, foundation is poor, cannot understand,


The second big question, CWinApp inherited from CWinThread, and CWinThread should belong to the thread class, is with the thread of a special class, I don't understand is, from the above analysis, as a main thread running, does not seem to be open new thread (if yes), why CWinApp to inherit CWinThread? The inheritance has what meaning?

For 50 points, if answered carefully and completely, thank you,



CodePudding user response:

DLG program is a thread,

CodePudding user response:

 
#include

//callback function: receiving and processing messages
LRESULT a CALLBACK WndProc (HWND HWND, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{
The switch (the message)
{
Case WM_CREATE message handler:
return 0;
Case WM_DESTROY:
The PostQuitMessage (0);
return 0;
}
Return DefWindowProc (HWND, message, wParam, lParam);
}

Int WINAPI WinMain (HINSTANCE HINSTANCE, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
MSG MSG.
The HWND HWND;
The static TCHAR szAppName []=TEXT (" HelloWindow ");

WNDCLASS WNDCLASS;
Wndclass. Style=CS_HREDRAW | CS_VREDRAW;
Wndclass. CbClsExtra=0;
Wndclass. CbWndExtra=0;
Wndclass. HInstance=hInstance;
Wndclass. HIcon=LoadIcon (NULL, IDI_APPLICATION);
Wndclass. HCursor=LoadCursor (NULL, IDC_ARROW);
Wndclass. HbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH);
Wndclass. LpszMenuName=NULL;
Wndclass. LpfnWndProc=WndProc;
Wndclass. LpszClassName=szAppName;
if (! RegisterClass (& amp; Wndclass))//registration form
{
MessageBox (NULL, TEXT (" This program requires Windows NT!" ), szAppName, MB_ICONERROR);
return 0;
}

//Creates an overlapped, pop - up, or child window
HWnd=CreateWindow (
SzAppName,//A null - terminated string or A class atom created by A previous call to the RegisterClass or RegisterClassEx function
The TEXT (" The Hello Windows Program "),//The Window name
WS_OVERLAPPEDWINDOW,//The style of The window being created
CW_USEDEFAULT,//The initial horizontal position of The window
CW_USEDEFAULT,//The initial vertical position of The window
CW_USEDEFAULT,//The width, in device units, of The window
CW_USEDEFAULT,//The height, in device units, of The window
NULL,//A handle to the parent or owner window of the window being created
NULL,//A handle to A menu, or specifies A child window identifier - depending on the window style
HInstance,//program instance handle
NULL);

ShowWindow (hWnd, iCmdShow);
UpdateWindow (hWnd);

While (GetMessage (& amp; MSG, NULL, 0, 0))
{
TranslateMessage (& amp; MSG);
DispatchMessage (& amp; MSG);
}

return 0;
}


This code is useless MFC can also create a window, the code is simpler, can be simple to understand, to pay special attention to the following:
 
LRESULT a CALLBACK WndProc (HWND HWND, UINT message, WPARAM WPARAM, LPARAM LPARAM)

Wndclass. LpfnWndProc=WndProc;//set the callback function that receives the message processing

While (GetMessage (& amp; MSG, NULL, 0, 0))
{
While (GetMessage (& amp; MSG, NULL, 0, 0))
DispatchMessage (& amp; MSG);
}


MFC simple encapsulation of WINAPI
Guide the common code was generated according to choose
Familiar with MFC, especially among them "message map", "event-driven", use rise more convenient and visual development efficiency is high, but the self-study up can be a bit difficult,

Can try to learn about this code, simple to see the MFC code generated by the wizard, it may easily the
Don't know what to say right, for reference only

CodePudding user response:

refer to the second floor CppCoder response:

[code=c]#include

//callback function: receiving and processing messages
LRESULT a CALLBACK WndProc (HWND HWND, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{

Your reply is too basic, also didn't see I ask the question, but very enthusiastic thank you,

CodePudding user response:

CWinThread encapsulation is bad, say a word, the MFC overall packaging is not enough grace,
So you have this kind of question is normal,
Still have a little familiar with win32 programming, process created a default thread, running the main function is even if one thread,


Get a dialog box procedure, get the MFC into static link, you can step into the code,

CodePudding user response:





CodePudding user response:

Suggest the landlord to familiarize yourself with the MFC debug, and see a problem to see the nature
Domal message blocking, how is it blocked, you have to understand, not a little knowledge,

CodePudding user response:

Mechanism under the Win32 application, be familiar with the news

CodePudding user response:

Win32 application, usually is: the main thread + n auxiliary thread

CodePudding user response:

The process of task manager label, one of the column can display the number of threads... If can't see the point of view above - & gt; Select columns - & gt; Tick the number of threads

CodePudding user response:

nullnullnullnullnullnull
  • Related