如何使用 MFC 从控制台正确弹出非模式对话框

发布于 2024-08-26 04:10:05 字数 1407 浏览 3 评论 0原文

我需要创建一个具有 ma​​in() 函数的控制台应用程序并弹出一个无模式对话框,因此控制台仍然可以与无模式对话框并行工作(做其他工作,就像与非模式对话框通信一样)。

无论我尝试什么,我都只能弹出一个模式对话框。 (控制台处于保持状态,直到模式对话框自行关闭)。

当使用 Create() 和 ShowWindow() 切换到无模式对话框时,该对话框将在没有控件的情况下显示,并且冻结/阻止(您可以看到沙漏光标)。

1) 我尝试从 main() 函数中弹出非模式对话框:

void main()
{
    AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

    TestGUI * gui;
    gui = new TestGUI();
    gui->Create(TestGUI::IDD);
    gui->ShowWindow(SW_SHOW);

    // just to see if the modeless dialog responses
    Sleep(10000);
}

2) 我尝试从 CWinApp 派生类的 InitInstance() 中弹出非模式对话框:

extern int AFXAPI AfxWinMain(HINSTANCE hInstance,
                             HINSTANCE hPrevInstance,
                             LPTSTR lpCmdLine, int nCmdShow);

class MyApp : public CWinApp
{
public:
    virtual BOOL InitInstance()
    {
        gui = new TestGUI();
        gui->Create(TestGUI::IDD);
        gui->ShowWindow(SW_SHOW);

        return TRUE;
    }

private:
    TestGUI * gui;
};

MyApp my_app;

void main()
{
    AfxWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

    // just to see if the modeless dialog responses
    Sleep(10000);
}

在所有情况下,非模式对话框都会冻结。

我相信这是一个单行解决方案。
请帮忙。

TNX,
Vertilka

I need to create a console application that has a main() function and pop a modeless dialog, so the console can still work in parallel to the modeless dialog (do other work, like communicating with the modeless dialog).

Whatever i tried, i could only pop a modal dialog. (where the console is in hold till the modal dialog close itself).

When switching to modeless dialog using Create() and ShowWindow() the dialog is displayed without its controls and it freeze / block (you can see the hourglass cursor).

1) I tried to pop the modeless dialog from the main() function:

void main()
{
    AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

    TestGUI * gui;
    gui = new TestGUI();
    gui->Create(TestGUI::IDD);
    gui->ShowWindow(SW_SHOW);

    // just to see if the modeless dialog responses
    Sleep(10000);
}

2) I tried to pop the modeless dialog from the InitInstance() of a CWinApp derived class:

extern int AFXAPI AfxWinMain(HINSTANCE hInstance,
                             HINSTANCE hPrevInstance,
                             LPTSTR lpCmdLine, int nCmdShow);

class MyApp : public CWinApp
{
public:
    virtual BOOL InitInstance()
    {
        gui = new TestGUI();
        gui->Create(TestGUI::IDD);
        gui->ShowWindow(SW_SHOW);

        return TRUE;
    }

private:
    TestGUI * gui;
};

MyApp my_app;

void main()
{
    AfxWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

    // just to see if the modeless dialog responses
    Sleep(10000);
}

In all cases the modeless dialog freeze.

I believe this is a one line solution.
Please help.

TNX,
Vertilka

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

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

发布评论

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

评论(1

谁对谁错谁最难过 2024-09-02 04:10:05

以下代码片段解决了问题:

#include "stdafx.h"
#include "TestGUI.h"

DWORD WINAPI ModelessThreadFunc(LPVOID)
{
  TestGUI gui;
  gui.Create(TestGUI::IDD);
  gui.ShowWindow(SW_SHOW);

  HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, L"CloseModelessDialog");

  MSG msg;
  while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
  {
    while(::GetMessage(&msg, NULL, 0, 0))
    {
      ::TranslateMessage(&msg);
      ::DispatchMessage(&msg);
    }
  }

  // event cleanup
  CloseHandle(hEvent);

  return 0;
}

void main()
{
  // initialize MFC
  AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

  // create thread for the modeless dialog
  CreateThread(NULL, 0, ModelessThreadFunc, NULL, 0, NULL);

  // wait for the modeless dialog to close itself
  HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, L"CloseModelessDialog");
  while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
  {
    // do other job
  }

  // event cleanup
  CloseHandle(hEvent);
}

另请参阅以下链接:

Following code snippet solves the problem:

#include "stdafx.h"
#include "TestGUI.h"

DWORD WINAPI ModelessThreadFunc(LPVOID)
{
  TestGUI gui;
  gui.Create(TestGUI::IDD);
  gui.ShowWindow(SW_SHOW);

  HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, L"CloseModelessDialog");

  MSG msg;
  while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
  {
    while(::GetMessage(&msg, NULL, 0, 0))
    {
      ::TranslateMessage(&msg);
      ::DispatchMessage(&msg);
    }
  }

  // event cleanup
  CloseHandle(hEvent);

  return 0;
}

void main()
{
  // initialize MFC
  AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);

  // create thread for the modeless dialog
  CreateThread(NULL, 0, ModelessThreadFunc, NULL, 0, NULL);

  // wait for the modeless dialog to close itself
  HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, L"CloseModelessDialog");
  while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
  {
    // do other job
  }

  // event cleanup
  CloseHandle(hEvent);
}

Also look at the following link: microsoft newsgroups

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文