MFC開發常見問題的回答2 (轉)

worldblog發表於2007-12-29
MFC開發常見問題的回答2 (轉)[@more@]

第一輯在/develop/read_article.?id=6676">http://www.csdn.net/develop/read_article.asp?id=6676

1.如何讓我的執行的時候最大化?

(1)在appwizard第4步選擇“advanced"從中選擇Mainframe的Maximized

(2)對於MDI程式,在CWinApp::InitInstance() 中做下面改動

// Create main MDI Frame window.
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
    return FALSE;

m_nCmdShow = SW_SHOWMAXIMIZED;  // 注意新增此行!!!

pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
m_pMainWnd = pMainFrame;
(3)對於SDI程式,在CWinApp::InitInstance() 中的OnFileNew()之前

m_nCmdShow = SW_SHOWMAXIMIZED;
//  下面建立空文件
OnFileNew();

2,如何給其他執行緒發訊息?

用SendNotifyMessage() 。


3,如何讓我的程式只執行一次?

const char* MyMainWndClassName = "MyMainWndXQW"
BOOL CMyApp::InitApplication()
{
    // Call base class. Default version does nothing.
    CWinApp::InitApplication();
    WNDCLASS wndcls;
    // Start with NULL defaults.
    memset(&wndcls, 0, sizeof(WNDCLASS)); 
    // Get class information for default window class.
    ::GetClassInfo(AfxGetInstanceHandle(),"AfxFrameOrView",&wndcls);
    // Substitute unique class name for new class.
    wndcls.lpszClassName = MyMainWndClassName;
    // Register new class and return the result code.
    return ::RegisterClass(&wndcls);
}
BOOL CMyApp::FirstInstance()
{
    CWnd *PrevCWnd, *ChildCWnd;
    // Detene if another window with our class name exists.
    PrevCWnd = CWnd::FindWindow(MyMainWndClassName, NULL);
    if (PrevCWnd != NULL)
    {
        // If so, does it have any pop-ups?
        ChildCWnd=PrevCWnd->GetLastActivePopup();
        // Bring the main window to the top.
        PrevCWnd->BringWindowToTop();
        // If iconic, restore the main window.
        if (PrevCWnd->IsIconic())
            PrevCWnd->ShowWindow(SW_RESTORE);
        // If there are pop-ups, bring them along too!
        if (PrevCWnd != ChildCWnd)
            ChildCWnd->BringWindowToTop();
        // Return FALSE. This isn't the first instance
        // and we are done activating the previous one.
        return FALSE;
    }
    else
        // First instance. Proceed as normal.
        return TRUE;
}
CMyApp::InitInstance()
{
    if (!FirstInstance())
        return FALSE;
    // ...
}




4,MDI程式,關閉子視窗同時關閉父視窗,該如何做?

在子視窗的OnClose函式里新增
ASSERT(AfxGetMainWnd() != NULL);
AfxGetMainWnd()->SendMessage(WM_CLOSE);



選單問題:

1,我在程式中用了MenuBar,結果找不到選單了,我的方法是:
CMenu *menu;
menu = GetMenu()->GetSubMenu(0);

答:

AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(0);





2,如何動態修改MainFrame的選單?

CMenu newMenu;
newMenu.LoadMenu (IDR_MENU1);
AfxGetMainWnd()->SetMenu( &newMenu );
AfxGetMainWnd()->DrawMenuBar();
newMenu.Detach ();




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-996104/,如需轉載,請註明出處,否則將追究法律責任。

相關文章