Книга: 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.

Оглавление книги


Генерация: 0.619. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз