MFC學習記錄2
一、建立基于對話框工程,改變UI對象
1.改變菜單、工具欄
在資源欄中編輯菜單、工具欄,注意ID



編輯ID
【Pen/Thick Line】
ID : ID_PEN_THICK_OR_THIN
prompt : "Toggles the line thickness between thin and thick\nToggle pen"
【Pen/Pen Widths】
ID : ID_PEN_WIDTHS
prompt : "Sets the size of the thin and thick pen\nPen thickness"
【Edit/Clear All】
ID : ID_EDIT_CLEAR_ALL
prompt : "Clears the drawing\nErase All"
2.連接ID與處理函數

①添加成員函數
void CScribbleDoc::OnEditClearAll()
{
DeleteContents();
SetModifiedFlag(); // Mark the document as having been modified, for
// purposes of confirming File Close.
UpdateAllViews(NULL);
}
void CScribbleDoc::OnPenThickOrThin()
{
// Toggle the state of the pen between thin or thick.
m_bThickPen = !m_bThickPen;
// Change the current pen to reflect the new user-specified width.
ReplacePen();
}
void CScribbleDoc::ReplacePen()
{
m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
// Change the current pen to reflect the new user-specified width.
m_penCur.DeleteObject();
m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
}
②申明函數
protected:
void ReplacePen();
③添加成員變量
protected:
// The document keeps track of the current pen width on
// behalf of all views. We'd like the user interface of
// Scribble to be such that if the user chooses the Draw
// Thick Line command, it will apply to all views, not just
// the view that currently has the focus.
UINT m_nPenWidth; // current user-selected pen width
BOOL m_bThickPen; // TRUE if current pen is thick
UINT m_nThinWidth;
UINT m_nThickWidth;
CPen m_penCur; // pen created according to
④初始化函數
void CScribbleDoc::InitDocument()
{
m_bThickPen = FALSE;
m_nThinWidth = 2; // default thin pen is 2 pixels wide
m_nThickWidth = 5; // default thick pen is 5 pixels wide
ReplacePen(); // initialize pen according to current width
}
3.維護UI對象狀態

Enable:灰色或正常
SetCheck:打勾或沒打勾
void CScribbleDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_strokeList.IsEmpty());
}
void CScribbleDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bThickPen);
}
二、MFC與對話框
1.新建對話框,編輯對話框控件:Edit控件、Static控件、按鈕


注意對話框的屬性ID IDD_PEN_WIDTHS及控件屬性ID
2.連接對話框與專用類

生成對話框.cpp和.h文件
3.對話框消息處理函數

4.對話框數據交換與校驗(DDX&DDV)
DDX :是讓我們把對話框類別中的成員變量與對話框中的控制組件產生關聯,于是
當對話框結束時,控制組件的內容會自動傳輸到這些成員變量上。
DDV: 是允許我們設定對話框控制組件的內容類型以及資料(數值) 范圍。


5.調用對話框
【Pen Widths】對話框是一個所謂的Modal 對話框,意思是除非它關閉(結束),否則
它會緊抓住這個程序的控制權,但不影響其它程序。相對于Modal 對話框,有一種
Modeless 對話框就不會影響程序其它動作的進行;通常你在文字處理軟件中看到的文字
搜尋對話框就是Modeless 對話框。
我們希望Step3 的命令項【Pen/Pen Widths】被按下時,【Pen Widths】對話框能夠執行
起來。要喚起此一對話框,得做到兩件事情:
1. 產生一個CPenWidthsDlg 對象,負責管理對話框。
2. 顯示對話框窗口。這很簡單,調用DoMoal 即可辦到。

編輯函數,注意頭文件名稱,與之前設置對應。
// SCRIBDOC.CPP
#include "pendlg.h"
...
void CScribbleDoc::OnPenWidths()
{
CPenWidthsDlg dlg;
// Initialize dialog data
dlg.m_nThinWidth = m_nThinWidth;
dlg.m_nThickWidth = m_nThickWidth;
// Invoke the dialog box
if (dlg.DoModal() == IDOK)
{
// retrieve the dialog data
m_nThinWidth = dlg.m_nThinWidth;
m_nThickWidth = dlg.m_nThickWidth;
// Update the pen that is used by views when drawing new strokes,
// to reflect the new pen width definitions for "thick" and "thin".
ReplacePen();
}
}

浙公網安備 33010602011771號