⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 40则mfc基本问题.txt

📁 mfc使用过程40个常的经典功能实现.文本格式,必有代码
💻 TXT
字号:
虽然有的大家可能会,但很难所有的都会,所以想一想,还是转贴过来了: 

来源:http://feeyoo.51.net/devdoc/docs/other/40mfcpro.htm 

内容: 

我整理的40则MFC基本问题,大家到这里看看吧 

作  者:111222 

视图问答6则: 

1,怎样改变视图的大小? 

一般,你能改变试图大小依靠 MoveWindow() 

MFC应用程序,视图是所围绕其框架的子窗口,获取其框架的指针GetParentFrame(),然后 

依靠MoveWindow()改变框架大小,视图大小自动跟随框架大小改变。 

2,如何改变一个CFormView的大小? 

首先,你要在你的CFormView中重载OnInitialUpdate()函数,该函数声明如下: 

virtual void OnInitialUpdate(); 

在OnInitialUpdate()添加如下代码: 

void ClikethisView::OnInitialUpdate()//ClikethisView从CFormView继承而来 

{ 

    // Make the window the size of the main dialog. 

    CFormView::OnInitialUpdate(); 

    GetParentFrame()->RecalcLayout(); 

    ResizeParentToFit( /*FALSE*/ ); 

} 

3,如何改变一个视图的背景? 

想改变CView、CFrameWnd或者CWnd的背景,需要处理WM_ERASEBKGND 消息。 

像下面演示的这样: 

BOOL CSampleView::OnEraseBkgnd(CDC* pDC) 

{ 

    // 设置背景画刷颜色 

    CBrush backBrush(RGB(255, 128, 128)); 

    // 保存旧画刷 

    CBrush* pOldBrush = pDC->SelectObject(&backBrush); 

    CRect rect; 

    pDC->GetClipBox(&rect);    // 擦掉所要画的区域 

    pDC->PatBlt(rect.left, rect.top, rect.Width(), 

    rect.Height(), PATCOPY); 

    pDC->SelectObject(pOldBrush); 

    return TRUE; 

} 

4,改变对话框 尘把丈 后,如何使控件的背 把丈 和对话框 尘把丈 统一? 

请看下面代码:  重载画控件的消息。 

HBRUSH dlgtest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 

{ 

    switch (nCtlColor) 

    { 

        case CTLCOLOR_BTN://按钮? 

        case CTLCOLOR_STATIC://我们所需要改画的静态 

        { 

            pDC->SetBkMode(TRANSPARENT); 

        } 

        case CTLCOLOR_DLG: 

        { 

            CBrush*    back_brush; 

            COLORREF    color; 

            color = (COLORREF) GetSysColor(COLOR_BTNFACE); 

            back_brush = new CBrush(color); 

            return (HBRUSH) (back_brush->m_hObject); 

        } 

    } 

    return(CFormView::OnCtlColor(pDC, pWnd, nCtlColor)); 

} 

5,如何获得当前视图的指针? 

((CFrameWnd*) AfxGetApp()->m_pMainWnd))->GetActiveDocument(); 

或者 

((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView(); 

6,如何获得MDI程序的所有视图? 

下面函数对你有用: 

CDocument::GetFirstViewPosition(); // DOCCORE.CPP 

CDocument::GetNextView(); // DOCCORE.CPP 

CMultiDocTemplate::GetFirstDocPosition(); // DOCMULTI.CPP 

CMultiDocTemplate::GetNextDoc(); // DOCMULTI.CPP 

文档精彩问答: 

1,如何获得当前文档指针? 

参照:上面“如何获得当前视图指针?” 

2,文档什么时候被破坏? 

单文档程序的文档当程序退出时被破坏。 

多文档程序在最后一个视图关闭时被破坏。 

3,如何获得当前打开文档的列表? 

下面代码,MyApp从CWinApp继承而来,MyApp的成员变量: 

CPtrList m_templateList 

void CMyApp::GetDocumentList(CObList * pDocList) 

{ 

    ASSERT(pDocList->IsEmpty()); 

    POSITION pos = m_templateList.GetHeadPosition(); 

    while (pos) 

    { 

        CDocTemplate* pTemplate = 

            (CDocTemplate*)m_templateList.GetNext(pos); 

        POSITION pos2 = pTemplate->GetFirstDocPosition(); 

        while (pos2) 

        { 

            CDocument * pDocument; 

            if ((pDocument=pTemplate->GetNextDoc(pos2)) != NULL) 

                pDocList->AddHead(pDocument); 

        } 

    } 

} 

4,如何不叫我的程序自动打开文档? 

在InitInstance()函数中 

cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing 

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; 

    // Determine 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 ); 

在子窗口的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 (); 

  

-- 

  

   __@       __@        __@       __@   笑语小园:http://xiaoyuyeah.yeah.net 

 _`\<,_    _`\<,_     _`\<,_    _`\<,_  笑语小园:http://xiaoyuyeah.126.com 

(*)/ (*)  (*)/ (*)   (*)/ (*)  (*)/ (*) 笑语小园:http://xiaoyuyeah.51.net 

              笑语的电子邮件:garden.xiaoyu@263.net 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -