The program idea is the external input and output control card issued by the acquisition of the image signal, after the camera acquisition image to obtain the image data pointer, receive image data pointer created as an image finally displayed to the MFC dialog box application of the picture control control, and in order to calibrate the camera position, In the OnPaint function of the main dialog class CMyDlg, there are drawing operations on picture control (do not change image data, draw lines, draw rectangles, etc.).
The design takes into account when the I/O card emits the acquisition signal or when the camera gets the image data pointer is indeterminate (without using ontime), taking into account the I/O card and the camera's callback function and the data exchange between the main program is more difficult (not in the callback function to process the results), So creating a multithreaded dectect function, the I/O card or the camera's callback function is only responsible for sending the global image data pointer to the main program, calling the SetEvent function of the custom event to notify the Dectect thread to work.
Dectect threads cannot directly call CMyDlg OnPaint functions, invalidate, and UpdateData functions after acquiring an image data pointer.
Functions are implemented indirectly using custom messages. There will certainly be more of this in the future at work. The implementation is divided into the following seven steps.
First step: Add a custom message macro to StdAfx.h
#define Wm_user_postinvalidate wm_user+500
Step two: Declare a member function of the public type in MyDlg.h (declare our own handler function for defining the message)
afx_msg LRESULT mymessage (WPARAM WPARAM, LPARAM LPARAM);
Step three: Add a mapping entry for the custom message in the CMyDlg class (MyDlg.cpp) between Begin_message_map (Cee8000dlg, CDialog) and End_message_map () (That is, the class's Message map table)
On_message (Wm_user_postinvalidate,mymessage)
Fourth step: Add a handler function for the custom message in the CMyDlg class's implementation file MyDlg.cpp
LRESULT cmydlg::mymessage (WPARAM WPARAM, LPARAM LPARAM) {
Fifth step: The purpose of the previously added custom message function is to be able to invoke the PostMessage function in the dectect thread, postmessage the function is to send a specified message to the thread created by the specified window (window handle) in the message queue. Now that the message is available, you need to get the window handle of the main dialog box in Dectect.
1. Get the window handle of the main dialog box. General question, add in CMyDlg's OnInitDialog function
CWnd *pmainwnd=AfxGetMainWnd (); HWND Hmainwnd=pmainwnd->getsafehwnd ();
2. Add Hmainwnd to Dectect. More trouble, the initial attempt to define Hmainwnd as a global variable, debug run found Hmainwnd in OnInitDialog correctly assigned value, run to dectect thread function value becomes null again. Also try in the Dectect function
CWnd *pmainwnd=afxgetmainwnd (); // Error Method HWND Hmainwnd=pmainwnd->getsafehwnd (); // Error Method
The debug result runs to the dectect thread function Hmainwnd and then becomes null. The correct method is to go back to the dectect thread creation process.
3. Dectect Thread creation function
HANDLE Hthreaddectect=createthread (NULL,NULL,&DECTECT,NULL,NULL,&DWTHREADID1); // Error Method
Modified Dectect Thread creation function
HANDLE Hthreaddectect=createthread (NULL,NULL,&DECTECT,HMAINWND,NULL,&DWTHREADID1); // correct, the fourth parameter passes Hmain as a parameter to the callback function
After 3 steps, the window handle of the main dialog box is passed to the dectect thread.
Sixth step: Dectect the thread image data processing operation to send the completion message to the main dialog thread, my program is equivalent to notifying the main dialog to redraw the operation. Dectect the appropriate location for the thread function:
PostMessage (HWND) pparam,wm_user_postinvalidate,0,0); // Pparam is the window handle passed to Dectect in CreateThread
The seventh step: in the function implementation part of the fourth step, we can call CMyDlg's OnPaint function, invalidate and updatedata function directly.
The record of self-feeling is very detailed, convenient for later review. At the same time, welcome the great God to make more valuable comments.
Original Link: MFC sub-thread changes image data after updating the main window image display method
Reference Documents: MFC new Thread Control progress bar
Go Update main window image display method after MFC child thread changes image data