MFC create a window (PostMessage method) in the sub-thread to create multiple threads establish non-modal dialog with non-modal dialog box based on the use of MFC thread in AfxBeginThread

1, create a child thread

C ++ are more ways to create threads

1) The easiest-to-use <thread> header file, but the child thread created by this method can not be PostMessage messages to the main thread (I may have made a mistake, in short, did not succeed)

Three methods 2) 3) 4) See VC thread creation https://blog.csdn.net/u014568921/article/details/44262645

3 and 4 in the breeding program looks like MFC does not work, I spent several attempts under AfxBeginThread () method is successful

void CMFCDLLTestDlg :: OnBnClickedMessage () 
{ 
    // the TODO: In this addition control notification handler code
     // Start thread websocket 
    the AfxBeginThread ((AFX_THREADPROC) MsgThread, (VOID *) the this , THREAD_PRIORITY_NORMAL, 0 , 0 , NULL); 
}

Here I was in a button click event started a thread websocket, the global thread function MsgThread ()

2, create a custom message through window

In MFC program, called direct child thread Create () method can not create a non-modal window, blocking the cycle looks like a sub-thread creation process, it is necessary to use a custom message to notify the main thread to create a method

2.1 custom message

MFC custom message is not difficult, in three steps

1, define a message ID

My program called MFCDLLTestDlg, so in a defined MFCDLLTestDlg.cpp message ID

#define TEST_SENDMSG 200 is of WM_USER + // to a message ID

2, the definition of message handlers

Message processing function is used to handle custom message received is , there are two ways this can be added by Class Wizard a custom message processing or own handwriting also OK, use the class wizard can bind directly, save a third step

Class Wizard mode: attempting to switch to class ---> --- Class Wizard> message ---> add a custom message, and then enter a custom message ID and the like of the function name

 Class Wizard as handwriting and, anyway, their message ID must be defined, a fixed number WM_USER +, will not be repeated on the line

Then the header file declare a message handler note written in the main dialog class

afx_msg LRESULT OnTestSendmsg(WPARAM wParam, LPARAM lParam);

Then define the cpp

LRESULT CMFCDLLTestDlg::OnTestSendmsg(WPARAM wParam, LPARAM lParam)
{
    switch (wParam)
    {
    case TEST_SENDMSG:
        CMsgWindow * p_MsgWindow = new CMsgWindow();
        p_MsgWindow->SetSkin(MAKEINTRESOURCE(IDB_BITMAP1));
        //p_MsgWindow->SetSkin(MAKEINTRESOURCE(IDB_BITMAP2));
        //p_MsgWindow->SetSkin(MAKEINTRESOURCE(IDB_BITMAP3));

        CString *cmsg = (CString *)lParam;

        if (!p_MsgWindow->Create(m_hWnd, _T("通知")))
        {
            AfxMessageBox(L"! The Create the Failed " ); return - . 1 ; 
        } 
        p_MsgWindow -> SetMsg (L " imitation QQ bottom right news pop " , CMSG *, L " http://blog.csdn.net/jackystudio " ); 
        p_MsgWindow -> show ();
         BREAK ; 
    } 

    return LRESULT (); 
}

Here I was in had a bomb after receiving the message window, interested about this visit  http://blog.csdn.net/jackystudio  blog, I find here a beautiful pop program

Note that this function of the two parameters [WPARAM the wParam, LPARAM the lParam] , this can be converted to their type, may be used such that the first parameter is a message type, the second parameter is a string, an integer, and other parameters here is a string Cstring

These two parameters are passed in PostMessage function, see below

3, add a message handler mapping

With the message ID and processing functions , but also to two associated , which is the message map is also operated in the main category, found MESSAGE_MAP

BEGIN_MESSAGE_MAP(CMFCDLLTestDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_WIN_TEXT, &CMFCDLLTestDlg::OnBnClickedWinText)
    ON_BN_CLICKED(IDC_NEW_DLG, &CMFCDLLTestDlg::OnBnClickedNewDlg)
    ON_BN_CLICKED(IDC_MESSAGE, &CMFCDLLTestDlg::OnBnClickedMessage)

    ON_MESSAGE(TEST_SENDMSG, &CMFCDLLTestDlg::OnTestSendmsg)
END_MESSAGE_MAP()

You can see the system messages and mapping button click events are here, note that the look is the main class of message map  BEGIN_MESSAGE_MAP (CMFCDLLTestDlg, CDialogEx) , I first wrote in the inside About the class

2.2 Send Message

In daughter thread function PostMessage send a message, this is generally used, the SendMessage line, a synchronizing an asynchronous

// omitted other logical thread function
 // ...
 // send message 
:: PostMessage (AfxGetMainWnd () -> GetSafeHwnd (), TEST_SENDMSG, (WPARAM) TEST_SENDMSG, (LPARAM) cmsg);

It should be noted PostMessage function added scope qualifier , otherwise the function has several different calling methods

The first parameter is a handle to the main window, the second parameter is the message ID, the third, two parameters corresponding to the above four parameters message handler

And then when sending a message function is executed, the main thread will receive the message window, the process of creating a window function

Of course, as long as the message ID and parameters for a change, for example, replaced by a button click event ID or message ID system, you can do other things  

3, reference articles

AfxBeginThread to use (code examples) C ++ Part8 MFC in

Creating multiple threads using MFC in AfxBeginThread

The establishment of a non-modal dialog with the establishment of non-modal dialog thread

MFC SendMessage () function is passed a string

Guess you like

Origin www.cnblogs.com/jixiaohua/p/12117147.html