2012-02-29 27 views
0

使用Visual Studio 2010和MFC文檔/查看應用程序我希望我的SDI應用程序啓動完全隱藏,並且在某個時間後或從托盤圖標接收到一些消息時,它會顯示大型機,視圖等。我將m_pMainWnd->ShowWindow(SW_NORMAL);更改爲,但主框架在執行完應用程序後閃爍,然後啓動,我應該怎麼做才能避免此問題,並在需要時保持主框架的顯示功能。啓動文檔/查看應用程序隱藏

回答

1

一般來說,如果你有VC2005或更早下面會做什麼:

// Parse command line for standard shell commands, DDE, file open 
CCommandLineInfo cmdInfo; 
ParseCommandLine(cmdInfo); 

m_nCmdShow = SW_HIDE; 

// Dispatch commands specified on the command line. Will return FALSE if 
// app was launched with /RegServer, /Register, /Unregserver or /Unregister. 
if (!ProcessShellCommand(cmdInfo)) 
    return FALSE; 

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

注意m_nCmdShow應設置ProcessShallCommand不發生閃爍之前SW_HIDE。

它看起來像VC2010中可能有一個錯誤,雖然。由於我在做這件事之前對它感興趣並嘗試了一個新的VC2010項目,但它不起作用。我注意到這個問題在以下MFC函數中很深入。

BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, 
      CWnd* pParentWnd, CCreateContext* pContext) 
{ 
    // only do this once 
    ASSERT_VALID_IDR(nIDResource); 
    ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource); 

    m_nIDHelp = nIDResource; // ID for help context (+HID_BASE_RESOURCE) 

    CString strFullString; 
    if (strFullString.LoadString(nIDResource)) 
     AfxExtractSubString(m_strTitle, strFullString, 0); // first sub-string 

    VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG)); 

    // attempt to create the window 
    LPCTSTR lpszClass = GetIconWndClass(dwDefaultStyle, nIDResource); 
    CString strTitle = m_strTitle; 
    if (!Create(lpszClass, strTitle, dwDefaultStyle, rectDefault, 
     pParentWnd, ATL_MAKEINTRESOURCE(nIDResource), 0L, pContext)) 
    { 
     return FALSE; // will self destruct on failure normally 
    } 

    // save the default menu handle 
    ASSERT(m_hWnd != NULL); 
    m_hMenuDefault = m_dwMenuBarState == AFX_MBS_VISIBLE ? ::GetMenu(m_hWnd) : m_hMenu; 

    // load accelerator resource 
    LoadAccelTable(ATL_MAKEINTRESOURCE(nIDResource)); 

    if (pContext == NULL) // send initial update 
     SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE); 

    return TRUE; 
} 

m_nCmdShow仍然SW_HIDE執行此函數時,但是當if (!Create(lpszClass...行執行它變成SW_SHOWNORMAL。我不知道爲什麼只發生在VC2010項目中,聽起來像是一個bug。

我的示例項目是SDI。

+0

感謝您的信息我正在使用Visual Studio 2010 sp1,我的項目是SDI,並且在那裏不起作用。我將添加VS 2010到問題中,如果有任何事情可以幫助我解決問題,請分享它。 – AMCoded 2012-03-01 17:06:24

0

這來自基於對話框的應用程序,但您應該可以將它轉換爲Doc/View應用程序。您需要處理OnWindowPosChanging事件。關鍵路線是if語句中的一個。這允許我的應用程序從視圖中完全隱藏。

void CIPViewerDlg::OnWindowPosChanging(WINDOWPOS FAR* lpWindowPosition) 
{ 
    if(!m_bVisible) 
    { 
     lpWindowPosition->flags &= ~SWP_SHOWWINDOW; 
    } 

    CDialog::OnWindowPosChanging(lpWindowPosition); 

} 
2

這裏是SDI/MDI的應用程序的解決方案:新的MFC(與VC2010)覆蓋存儲在系統註冊表中的設定的m_nCmdShow值。要改變這種行爲,只需重寫應用程序類中的LoadWindowPlacement虛擬函數即可。

BOOL CAdVisuoApp::LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd) 
{ 
    BOOL b = CWinAppEx::LoadWindowPlacement(rectNormalPosition, nFflags, nShowCmd); 
    nShowCmd = SW_HIDE; 
    return b; 
} 
0

確保您正確關閉中的CMainFrame ::的PreCreateWindow(CREATESTRUCT & CS)的WS_VISIBLE位。像這樣的東西應該工作:

cs.style &= ~WS_VISIBLE; 

我們是被人否定,而不是位將其關閉,而我們VS 6.0逃脫了,因爲這個函數被調用一次。它在Visual Studio的新版本中被調用了兩次,所以在第二次調用中我們再次將它翻轉過來。 :-O

相關問題