?? 40則mfc基本問題.txt
字號:
視圖問答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 );
AfxGetMainWnd()->DrawMenuBar();
newMenu.Detach ();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -