在子线程中实现修改文本框内容

来源:互联网 发布:淘宝运营黑科技 编辑:程序博客网 时间:2024/05/22 22:21

目的:

图片处理线程中,每得到一张图片,图片总数++;

当前结果为NG,错误图片总数++;

思路:

利用win32 API函数SetDlgItemText修改文本框内容。

(1)在主线程中完成修改
::SetDlgItemText(::AfxGetMainWnd()->m_hWnd,IDC_EDIT1,strNumberCh1);//输入参数为CString。
AfxGetMainWnd获得窗口句柄。
(2)在子线程中完成修改
查阅网上资料,不能直接调用SetDlgItemText,原因未知。应向窗口类中发消息,在窗口类的消息响应函数
    中添加SetDlgItemText相关代码。核心代码如下:
 #define WM_ALLCH1 WM_USER+1 //自定义消息
::PostMessage(::AfxGetApp()->m_pMainWnd->m_hWnd,WM_ALLCH1,0,0 ); //通过AfxGetApp获取窗口句柄
LRESULT  OnFunc1(WPARAM wParam,LPARAM lParam);
...
      ON_MESSAGE(WM_ALLCH1,OnFunc1)

      . . .

              LRESULT  CMyDlg::OnFunc1(WPARAM wParam,LPARAM lParam)  {}

/////////////////////////////////////////////////////////////

AfxGetMainWnd()得到的是当前线程的主窗口(如果有的话),因为主窗口是属于主线程的,所以想得到主窗口HWND值,
只能在主线程
中用AfxGetMainWnd(),但要不是处在主线程中,AfxGetMainWnd()是从当前线程查询主窗口的。但好像
AfxGetMainWnd()不能跨线程,
要想在线程中使用主窗口的HWND值,可以把主窗口的HWND值传给线程.也
可以用AfxGetApp()先取得主线程,再通过CWinThread的类成员m_pMainWnd获得主窗口(AfxGetApp()->m_pMainWnd->m_hWnd)

If AfxGetMainWnd is called from the application's primary thread, it returns the application'smain window according to the above rules. 
If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that 

made the call.

原创粉丝点击