5

我编写了一个基于 MFC 对话框的应用程序,该应用程序由另一个应用程序启动。目前,我还没有添加任何代码。这只是我得到的默认文件。其他应用程序可以成功启动我的应用程序。

当另一个应用程序启动它时,我试图隐藏我的应用程序的窗口。

BOOL CMyApp::InitInstance()
{
    CMyAppDlg dlg;
    m_pMainWnd = &dlg;        

    INT_PTR nResponse = dlg.DoModal();

    if (nResponse == IDOK)
    {
    }
    else if (nResponse == IDCANCEL)
    { 
    }

    return FALSE;
}

我尝试使用:

dlg.ShowWindow(SW_HIDE) 

但它仍然没有隐藏窗口。

我怎样才能完成这项任务?

4

6 回答 6

3

我建议你在某个地方遇到另一个问题。

如果您创建一个全新的空白 MFC 应用程序 (Visual Studio 2010),那么在 App::InitInstance 中,设置 SW_HIDE 而不是 SW_SHOW确实会导致结果窗口被隐藏。

BOOL CProj1App::InitInstance()
{

// boilerplate code
      . . . 

// The one and only window has been initialized, so show and update it
m_pMainWnd->ShowWindow(SW_HIDE);   // WORKS!
m_pMainWnd->UpdateWindow();

return TRUE;
}
于 2012-10-12T10:30:16.817 回答
2

一旦你打电话,DoModal你的对话框就注定要显示出来。只有一种解决方法可以成功避免焦点/闪烁问题。在这里查看我的答案:隐藏 MFC 对话框

因此,您的代码应如下所示:

BOOL CMyApp::InitInstance() 
{ 
    CMyAppDlg dlg;
    dlg.SetVisible(FALSE); // Sets m_visible flag to FALSE.

    m_pMainWnd = &dlg;         

    INT_PTR nResponse = dlg.DoModal(); 

    if (nResponse == IDOK) 
    { 
    } 
    else if (nResponse == IDCANCEL) 
    {  
    } 

    return FALSE; 
} 
于 2012-10-13T13:03:55.570 回答
1

解决上述问题。InitInstance 代码应如下所示:

BOOL CMyApp::InitInstance()
{
    CWinApp::InitInstance();
    AfxEnableControlContainer();

    CMyAppDlg dlg;
    dlg.Create(IDD_MyAppUI_DIALOG,NULL);
    dlg.ShowWindow(SW_HIDE);
    dlg.UpdateWindow();
    m_pMainWnd = &dlg;

    return TRUE;
}
于 2012-10-12T13:49:49.810 回答
1

您必须从内部隐藏对话框。

  1. 重载 OnInitDialog
  2. 调用 CDialogEx::OnInitDialog()
  3. 隐藏你的窗口并返回

这是代码

BOOL CMyAppDlg::OnInitDialog()
{
    BOOL result = CDialogEx::OnInitDialog();

    this->ShowWindow(SW_HIDE);

    return result;  // return TRUE  unless you set the focus to a control
}

还有另一种带有标记值的方法,YMMV。

于 2012-10-12T16:22:04.337 回答
1

首先让我解决以前解决方案的一些问题。

chintan s: 当函数超出范围时,确实对话框将被终止。如果对话框被声明为应用程序类的成员变量,这将是一个有效的解决方案。

Vikky: 不需要调用 Windows API,因为对话框是从 CWnd 派生的,它继承了 ShowWindow 成员,它只接受一个参数:show 命令。

ixe013: 此解决方案将起作用,但是,在对话框隐藏之前,它会闪烁,因为在调用 OnInitDialog 之前调用了 ShowWindow。

Pete: 这行不通,因为模态对话框在 m_pMainWnd 分配任何值之前就开始了。

解决方案由 ixe013 指出。

这是迄今为止唯一可行的解​​决方案,但您必须在对话框类中声明成员变量,如文章中所述。

于 2012-10-13T13:44:38.113 回答
0

showWindow 方法有 2 个变量。

  • 窗户把手
  • nCmdShow(控制窗口的显示方式)

    BOOL WINAPI ShowWindow( HWND hWnd 中, int nCmdShow 中);

    HWND hWnd = GetSafeHwnd();

    ShowWindow(hWnd,SW_HIDE);

这里

于 2012-10-12T10:19:57.687 回答