Windows NT Systems Programming: Spring 1999

[ Home | Syllabus | Course Notes | Assignments | Search]


Basic Windows Controls

Motivation: I want a simple GUI for the NOMS system
Something with a few buttons and areas to add text
Let's skip to Chapter 5 and learn about controls


Controls

Controls are special windows which take care of:


Six Basic Controls

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 and Dialog Box

Controls are frequently used in various dialog boxes

Can 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.


Push Buttons: by Example

Examples based on Message object editing program which uses the basic controls listed above:

Compilation instructions for homegrown msgedit

It contains:


CButton

// 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 hptrd_left.gif (955 bytes))
	ON_COMMAND(IDC_SAVE, HandleSave)
END_MESSAGE_MAP()

// Handle the save button 
void CMainWindow::HandleSave()
// Saves the message
{
// . . .

CStatic: Static text for labels

// 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

CEdit: Powerful Text Edit Object

// 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 hptrd_left.gif (955 bytes)

void CMainWindow::HandleClear()
// Handles Clearing of edit areas
{
	SetDlgItemText(IDC_TOPIC,""); // clear string in edit fields
	SetDlgItemText(IDC_TEXT,"");
}


Cedit with Scroll Bars

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


Screen Dump

msgCdlg.gif (9189 bytes)



Copyright chris wild 1999.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 08, 1999.