MFC中OnInitDialog函数之后是否有调用任何函数?

发布于 2024-10-18 22:39:39 字数 82 浏览 2 评论 0原文

我想在MFC中创建对话框后创建一个线程。 Windows 是否提供了任何函数,并在 OnInitDialog 之后自动调用,以便我可以在其中创建线程?

I want to create a thread after the creation of a dialog box in MFC. Is there any function that Windows has provided and is automatically called after OnInitDialog so that I can create my thread inside it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟─花易冷 2024-10-25 22:39:40

您可以简单地在 OnInitDialog 函数中创建线程。没有理由通过搜索不同的函数或将初始化代码分成两部分来使事情变得过于复杂。 (也没有任何这样的函数,因为没有发送相应的Windows消息。)

如果您想在创建之前在屏幕上显示对话框线程,您可以使用 ShowWindow 函数手动显示它。例如:

ShowWindow(SW_SHOW);
RedrawWindow();

另请参阅 Raymond Chen 的这篇文章: 等待在执行某操作之前显示对话框

You can simply create your thread in the OnInitDialog function. There's no reason to overcomplicate things by going and searching for a different function, or splitting your initialization code up in two pieces. (There also isn't any such function, because there's no corresponding Windows message that is sent.)

If you want to get your dialog box on the screen before you create the thread, you can just show it manually using the ShowWindow function. For example:

ShowWindow(SW_SHOW);
RedrawWindow();

Also see this post by Raymond Chen: Waiting until the dialog box is displayed before doing something

放赐 2024-10-25 22:39:40

OnInitDialog() 是初始化时调用的主函数(响应于WM_CREATE)。

为什么你不能在那里创建你的线程?

OnInitDialog() is the main function called upon initialization (in reaction to WM_CREATE).

Why can't you create your thread in there?

甜点 2024-10-25 22:39:40

我已将线程优先级更改为低于正常优先级,当线程第一次执行时,我将线程设置为正常优先级。这很好用。感谢您的回复。

I have changed the thread priority to below normal and when the thread executes for the first time I set the thread to normal priory. This works fine. Thanks for your response.

何处潇湘 2024-10-25 22:39:40

多年来,我们对在 MFC 对话框应用程序(最喜欢的游乐场)中绘制第一个视图图形的 OnTimer 解决方案感到不满意,这似乎是一个很好的简单解决方案:-

  1. 使用类向导添加 WM_HSCROLL 处理程序。
  2. 在 OnInitDialog 的末尾发布一条带有 NULL LPARAM 的 hscroll 消息,
  3. 在处理程序中检测 NULL,绘制图形。

计时器意味着应用程序在图形发生之前已经存活了一段时间,显然 hscroll 优先发生在 WM_PAINT 消息之后,这会将图片元素擦除到空白状态,删除 initdialog 期间绘制的任何内容。

BOOL CSpecDlg::OnInitDialog()
{
    ...

    PostMessage(WM_HSCROLL,0, (LPARAM)NULL);
    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSpecDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
    if (pScrollBar==NULL)
    {
        plot();
    }
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

After many years of feeling unsatisifed with the OnTimer solution to draw first view graphics in an MFC dialog app (a favorite playground), this seemed like a nice simple solution:-

  1. Add a WM_HSCROLL handler with class wizard.
  2. At the end of OnInitDialog post a hscroll message with a NULL LPARAM
  3. In handler detect the NULL, draw graphics.

a timer meant that the app was alive for some time before graphics happened, and apparently hscroll is prioritized to happen just after the WM_PAINT message which would erase a picture element to its blank state, deleting anything that was drawn during initdialog.

BOOL CSpecDlg::OnInitDialog()
{
    ...

    PostMessage(WM_HSCROLL,0, (LPARAM)NULL);
    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSpecDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
    if (pScrollBar==NULL)
    {
        plot();
    }
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文