MFC启动dlg.DoModal自动界面更新

betterjeff 2018-04-10 05:35:08
本人新学MFC,通过模板创建dlg对话框程序。
在界面上,我有很多方框显示解析的串口接收数据,之前一直是需要触动一个按键,在按键响应中设置线程启动,该线程会不断postmessage给主线程,带动界面上数据变化。
现在想一打开界面,不需要触动按键就可以显示数据变化,关键就是如何启动这个Postmessage的线程呢?
...全文
754 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
l357630798 2018-04-14
  • 打赏
  • 举报
回复
引用 15 楼 jeffupright 的回复:
[quote=引用 13 楼 l357630798 的回复:] 最好是SetTimer启动一个定时器,在定时器WM_TIMER响应OnTimer中处理. 如果你在OnInitDialog中启动线程肯定不行,因为这个时候窗口并没有 show 出来,你这时给窗口发消息,窗口根本收不到。或者直接线程中使用主窗口对象设置控件还可能会引起程序崩溃。
启动定时器应该在哪里写 比较合适呢[/quote] 就在OnInitDialog即可,然后响应函数里面开辟启动线程。
schlafenhamster 2018-04-14
  • 打赏
  • 举报
回复
想等 窗口 显示 后,做事, 可以 使用 一次性 OnTimer 即 ,在 init 启动 定时器 ,而在 定时器 响应中先 KillTimer ,然后 做你 一次性 事项。
betterjeff 2018-04-13
  • 打赏
  • 举报
回复
引用 9 楼 schlafenhamster 的回复:
如 if((dwEventMask & EV_RXCHAR)==EV_RXCHAR) { // some chars received. ResetEvent(pUload->m_hPostEventRead);// first reset pUload->PostMessage(WM_COMMNOTIFY, (WPARAM)1, // EV_RXCHAR (LPARAM)0); // not used // wait answer WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF); ////////////////////////////////////////////////////////////////////////// volatile HANDLE m_hPostEventRead;
谢谢您的回复,你说的我理解了。可能我没说清楚,串口接收数据是在点开按键后触发的,我现在就是想在不点开触发串口接收按键之前(不按任何button之前),UI界面上能显示时间的动态变化信息之类的。
betterjeff 2018-04-13
  • 打赏
  • 举报
回复
引用 6 楼 zgl7903 的回复:
基于对话框的APP,建议在对话框OnInitDialog中启动线程,在DestroyWindow中关闭线程; 或者更简单的在打开端口后SetTimer启动一个定时器,在定时器WM_TIMER响应OnTimer中处理
OnInitDialog这个被MFC封装了 是不?dlg.DoModal
betterjeff 2018-04-13
  • 打赏
  • 举报
回复
引用 13 楼 l357630798 的回复:
最好是SetTimer启动一个定时器,在定时器WM_TIMER响应OnTimer中处理. 如果你在OnInitDialog中启动线程肯定不行,因为这个时候窗口并没有 show 出来,你这时给窗口发消息,窗口根本收不到。或者直接线程中使用主窗口对象设置控件还可能会引起程序崩溃。
启动定时器应该在哪里写 比较合适呢
betterjeff 2018-04-13
  • 打赏
  • 举报
回复
引用 12 楼 schlafenhamster 的回复:
“UI界面上能显示时间的动态变化信息之类的。” 说 详细 点。
额 就是 一打开执行这个对话框程序 不需要按任何键 其界面上的时钟信息就能自动更新 这个时候我还没有出发读取串口数据
l357630798 2018-04-13
  • 打赏
  • 举报
回复
最好是SetTimer启动一个定时器,在定时器WM_TIMER响应OnTimer中处理. 如果你在OnInitDialog中启动线程肯定不行,因为这个时候窗口并没有 show 出来,你这时给窗口发消息,窗口根本收不到。或者直接线程中使用主窗口对象设置控件还可能会引起程序崩溃。
schlafenhamster 2018-04-13
  • 打赏
  • 举报
回复
“UI界面上能显示时间的动态变化信息之类的。” 说 详细 点。
schlafenhamster 2018-04-11
  • 打赏
  • 举报
回复
如 if((dwEventMask & EV_RXCHAR)==EV_RXCHAR) { // some chars received. ResetEvent(pUload->m_hPostEventRead);// first reset pUload->PostMessage(WM_COMMNOTIFY, (WPARAM)1, // EV_RXCHAR (LPARAM)0); // not used // wait answer WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF); ////////////////////////////////////////////////////////////////////////// volatile HANDLE m_hPostEventRead;
schlafenhamster 2018-04-11
  • 打赏
  • 举报
回复
对话框中 , 还 需要 一个 message 处理 完成的 消息, 以便线程 到底 消息 处理完成
worldy 2018-04-11
  • 打赏
  • 举报
回复
你的接收线程本身就是一个循环,循环读入串口是数据,如果读到数据,就PostMessage,如果没有读到,就不要Post,简单清晰明了
zgl7903 2018-04-11
  • 打赏
  • 举报
回复
基于对话框的APP,建议在对话框OnInitDialog中启动线程,在DestroyWindow中关闭线程; 或者更简单的在打开端口后SetTimer启动一个定时器,在定时器WM_TIMER响应OnTimer中处理
betterjeff 2018-04-10
  • 打赏
  • 举报
回复
我用了简单粗暴的方法,请各位看看。功能实现了,但是可能有什么副作用 BOOL CDllTestApp::InitInstance() { // InitCommonControlsEx() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // Set this to include all the common control classes you want to use // in your application. InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); AfxEnableControlContainer(); ::CreateThread(NULL, 0, ReadThreadFunc0, this, 0, &m_dwReadThreadID0); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need // Change the registry key under which our settings are stored // TODO: You should modify this string to be something appropriate // such as the name of your company or organization SetRegistryKey(_T("Local AppWizard-Generated Applications")); CDllTestDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; } static DWORD WINAPI ReadThreadFunc0(LPVOID lparam) { CSerial *pSerial = (CSerial*)lparam; while (TRUE) { Sleep(30); ::PostMessage(AfxGetApp()->m_pMainWnd->m_hWnd, WM_COMM_RXCHAR, 0, 0); } return 0; }
千寻港生惠香 2018-04-10
  • 打赏
  • 举报
回复
开线程的时候可以传句柄过去
赵4老师 2018-04-10
  • 打赏
  • 举报
回复
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspx
betterjeff 2018-04-10
  • 打赏
  • 举报
回复
我尝试在初始化initinstance里面去开线程,但是这里开的线程如何让SendMessage中的形参指向Dlg类。
千寻港生惠香 2018-04-10
  • 打赏
  • 举报
回复
是否可以在初始化方法里面开个线程去完成

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧