[ Home | Syllabus |Course Notes]
Controls are special windows which take care of:
Control Type | WNDCLASS | MFC Class |
Buttons | "BUTTON" | CButton |
List Boxes | "LISTBOX" | CListBox |
Edit controls | "EDIT" | CEdit |
Combo Boxes | "COMBOBOX" | CComboBox |
Scroll bars | "SCROLLBAR" | CScrollBar |
Static controls | "STATIC" | CStatic |
Controls are frequently used in various dialog boxes
Cna be created using the resource compiler
Common dialog boxes available (file opening, fonts, etc).
We'll cover these later = for now build them by hand.
Examples based on Message object editing program which uses the basic controls listed above:
It contains:
// from main.h const int IDC_SAVE=103; // Save button CButton m_save; // control buttons (instead of using menu for this purpose // from main.cpp . . . in CMainWindow // . . . m_save.Create("Save", // Label for this button WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, // style is visible child window CRect(CPoint(0,600),CSize(100,50)), // located at position 0,600 of size 100x50 this, // parent is "this" window IDC_SAVE); // ID is IDC_SAVE (a unique constant) BEGIN_MESSAGE_MAP( CMainWindow, CWnd ) ON_COMMAND(IDC_SAVE, HandleSave) END_MESSAGE_MAP() // Handle the save button void CMainWindow::HandleSave() // Saves the message { // . . .
// from main.h CStatic m_topicTitle; // Put titles on each edit area // from main.cpp . . . in CMainWindow // . . . m_topicTitle.Create("Message Topic", // label WS_CHILD|WS_VISIBLE, // the usual CRect(CPoint(0,60),CSize(150,24)), // its location this); // parent window
// from main.h const int IDC_TOPIC=100; // Topic edit object CEdit m_topic; // for editing the topic // from main.cpp . . . in CMainWindow // . . . // topic edit area m_topic.Create( WS_CHILD|WS_VISIBLE|WS_BORDER, // also put a border around it CRect(CPoint(50,90),CSize(440,24)), this, // parent IDC_TOPIC); // Unique ID m_topic.LimitText(SIZE_TOPIC); // size limit is property of this object // Handle the save button void CMainWindow::HandleSave() // Saves the message { CString topic; // . . . m_topic.GetWindowText(topic); // gets text user entered in topic msgApp.msg.SetTopic((LPCTSTR)topic); // and puts it in message object
void CMainWindow::HandleClear() // Handles Clearing of edit areas { SetDlgItemText(IDC_TOPIC,""); // clear string in edit fields SetDlgItemText(IDC_TEXT,""); }
// from main.h const int IDC_TEXT=101; // Text edit object CEdit m_text; // for editing the text // from main.cpp . . . in CMainWindow // . . . m_text.Create( WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|ES_MULTILINE, CRect(CPoint(50,150),CSize(440,450)), this, IDC_TEXT); m_text.LimitText(SIZE_TEXT);