Most of the time, we access the application control in a global function. Because the global function does not belong to a specific dialog box class, it cannot directly access a dialog box, of course, the dialog box control cannot be accessed. There are several situations and corresponding processing methods.
(1) Use thread parameters to access dialog box controls in global thread Functions
After creating a dialog box, place a progress bar, add the corresponding control variable myprogressct, and add a thread control flag volatile bool m_brun2 in CPP. The progress bar shows the status and is independent from the background data processing. Therefore, multithreading is required. In the dialog box, initialize the default value of the progress bar:
Myprogressct. setrange (0,100); // range
M_brun2 = true;
Createthread (null, 0, (lpthread_start_routine) threadfunc, this, 0, null); // pay attention to the thread parameter This, pointing to the current object
The thread functions are as follows:
Void threadfunc (lpvoid lparam)
{
Cprogressthreaddlg * P = (cprogressthreaddlg *) lparam; // convert this to a dialog box class pointer.
Int I = 0;
While (m_brun2)
{
I ++;
P-> myprogressct. setpos (I); // display the progress bar using the subcontrol of P in the dialog box.
Sleep (200 );
}
}
(2) Use some function access dialog box controls with window handles in global functions
Update the time display in the edit box of the dialog box continuously in a thread
Void threadfunc ()
{
Ctime time;
Cstring strtime;
M_brun = true;
While (m_brun)
{
Time = ctime: getcurrenttime ();
Strtime = time. Format ("% H: % m: % s ");
: Setdlgitemtext (afxgetmainwnd ()-> m_hwnd, idc_time, strtime );
Sleep (1000 );
}
}
Afxgetmainwnd () Description: Use the Wizard to create a single/Multi-document program. The outermost frame is frame, including titles, Max. Minimization buttons, menus, toolbar, and view. In the frame, the white part in the middle is occupied by the view.Afxgetmainwnd () is the pointer to the frame, and m_hwnd is the window handle of the frame.
The frame mentioned above is named cmainframe by default in your project. To get this frame pointer, you can operate the frame (that is, the outermost window, for example, hiding, moving, minimizing, and maximizing windows. In fact, cmainframe is derived from cframewnd, and cframewnd is derived from cwnd. Cmainframe is essentially a window. For example:
Afxgetmainwnd ()-> centerwindow (); // center the window
Afxgetmainwnd ()-> showwindow (sw_maximize); // maximize
M_hwnd is a member variable of the base class cwnd. It is defined as hwnd m_hwnd, which is a window handle. Therefore, the display statement in the above function can be written in another form: afxgetmainwnd ()-> setdlgitemtext (idc_time, strtime );
(3) using afxgetapp ()
Afxgetapp () can be used to obtain the pointer of the current application process, which is of the cwinapp * type. Through this pointer, you can access the objects in this process.
For example, in a global function, you need to write data to the list in the dialog box.
Void writestring (char * pstring)
{
Cwnd * pwnd = afxgetapp ()-> getmainwnd ();
Cmydlg * pdlg;
Pdlg = (cmydlg *) pwnd;
Pdlg-> showmsg (pstring );
}
Afxgetapp () can be used to obtain the currently referenced pointer cwinapp *, which can be used to access objects in the process.
(4) global variables
Http://topic.csdn.net/t/20050421/12/3954786.html ()
Http://blog.csdn.net/tianmeshi/archive/2009/05/22/4209904.aspx ()