Книга: Windows API Tutorials
Windows Controls
Windows Controls
Controls can be added to the main window or to any dialog box in your program. Controls are best picked and positioned using a graphical resource editor. Such an editor will also let you pick names or symbolic id's for your controls. You will then use these id's to identify the controls in your program.
Most controls can be encapsulated in objects that are either embedded in the appropriate Controller (you can have a separate Controller objects for every dialog box in your program) or, for static controls, in the View.
Controller objects are created in response to WM_CREATE or, for dialog boxes, WM_INITDIALOG messages. Constructors of controls embedded in these Controllers are executed at that time.
The base class for most controls is SimpleControl. It obtains and stores the window handle of the particular control. To obtain this handle, you need the parent window handle and the control's id.
class SimpleControl {
public:
SimpleControl (HWND hwndParent, int id) : _hWnd (GetDlgItem (hwndparent, id)) {}
void SetFocus () {
::SetFocus (_hwnd);
}
HWND Hwnd () const { return _hWnd; }
protected:
HWND _hWnd;
};
Here's an example of an edit control
class Edit: public SimpleControl {
public:
Edit (HWND hwndParent, int id) : SimpleControl (hwndParent, id) {}
void SetString (char* buf) {
SendMessage (Hwnd (), WM_SETTEXT, 0, (LPARAM) buf);
}
// code is the HIWORD (wParam)
static BOOL IsChanged (int code) {
return code == EN_CHANGE;
}
int GetLen () {
return SendMessage (Hwnd (), WM_GETTEXTLENGTH, 0, 0);
}
void GetString (char* buf, int len) {
SendMessage (Hwnd (), WM_GETTEXT, (WPARAM) len, (LPARAM) buf);
}
void Select () {
SendMessage (Hwnd (), EM_SETSEL, 0, –1);
}
};
This is how the edit control may be used:
class Controller {
public:
Controller(HWND hwnd);
…
private:
Edit _edit;
char _string [maxLen];
};
Controller::Controller (hwnd hwnd) : _edit (hwnd, IDC_EDIT) {
_edit.SetFocus ();
…
}
void Controller::Command (HWND hwnd, WPARAM wParam, LPARAM lParam) {
switch (LOWORD(wParam)) {
case IDC_EDIT:
if (_edit.IsChanged(HIWORD (wParam))) {
_edit.GetString (_string, maxLen);
}
break;
…
}
}
But, of course, the most likely place to use controls is in a Dialog Box.
- Windows API Tutorials
- The Simplest Windows Program
- The Generic Windows Program
- Windows Controls
- Program with a Dialog Box as the Main Window
- Dialog Box
- Canvas, or Windows Device Context
- Drawing with Pens and Painting with Brushes
- Using Threads
- When Folders Change
- Using Windows95 Shell and COM — A. K. A. OLE
- What's Wrong with OLE? The Insider's story
- Rationalizing OLE Building smart OLE on top of, you know, the other ole.
- OLE Automation
- Splitter Bar
- Bitmaps
- Direct Draw
- Joining the Tutorial Project
- Сноски из книги
- Содержание книги
- Популярные страницы
- Windows API Tutorials
- The Simplest Windows Program
- InterBase Super Server для Windows
- Интеграция с платформой Windows NT
- Часы в Windows показывают неправильное время
- Классическая архитектура на Windows NT (Yaffil CS)
- 4.2. Центр уведомлений Windows 10
- Загрузочные дискеты и Live CD для Windows
- 6.8. Разгон видеокарты на Windows 10
- Интеграция Windows SharePoint и Microsoft Office
- Наик Дайлип Системы хранения данных в Windows
- Глава 10 Возможности подсистемы хранения данных в различных версиях Windows NT