📄 controls.html
字号:
<html>
<head>
<title>Controls</title>
<meta name="description" content="Reliable Software Win32 Tutorial: Window Controls">
<meta name="keywords" content="reliable, software, windows, cplusplus, source code, example, tutorial, controls, dialog box, edit control, object oriented">
</head>
<body background="../images/grid.gif" bgcolor="white" text="black">
<table cellpadding=10 width="100%">
<tr>
<td width=100 align=center valign=middle>
<a href="../index.htm">
<img src="../images/rsbullet.gif" alt="RS" border=0 width=39 height=39>
<br>Home</a>
<td><font face="arial" color="#009966">
<h1 align=center>Window Controls</h1>
</font>
</table>
<p>
<table width="100%">
<tr>
<td width=80> <!-- Left margin -->
<td> <!-- Middle column, there is also the right margin at the end -->
<table cellpadding=10 cellspacing=0 width="100%">
<tr>
<td bgcolor="#ffffff">
<hr>
<font size="+1"><b>Controls can be added</b></font> 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.
<p>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.
<p>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.
<p>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.
<hr>
<pre><font face="courier">class <font color="#cc0066"><b>SimpleControl</b></font>
{
public:
SimpleControl (HWND hwndParent, int id)
: _hWnd (<font color="#000099"><b>GetDlgItem</b></font> (hwndParent, id))
{}
void SetFocus ()
{
<font color="#000099"><b>::SetFocus</b></font> (_hWnd);
}
HWND Hwnd () const { return _hWnd; }
protected:
HWND _hWnd;
};
</font></pre>
<hr>
Here's an example of an edit control
<hr>
<pre><font face="courier">class <font color="#cc0066"><b>Edit</b></font>: public <font color="#cc0066"><b>SimpleControl</b></font>
{
public:
Edit (HWND hwndParent, int id)
: SimpleControl (hwndParent, id)
{}
void SetString (char* buf)
{
<font color="#000099"><b>SendMessage</b></font> (Hwnd (), WM_SETTEXT, 0, (LPARAM) buf);
}
// code is the HIWORD (wParam)
static BOOL IsChanged (int code)
{
return code == EN_CHANGE;
}
int GetLen ()
{
return <font color="#000099"><b>SendMessage</b></font> (Hwnd (), WM_GETTEXTLENGTH, 0, 0);
}
void GetString (char* buf, int len)
{
<font color="#000099"><b>SendMessage</b></font> (Hwnd (), WM_GETTEXT,
(WPARAM) len, (LPARAM) buf);
}
void Select ()
{
<font color="#000099"><b>SendMessage</b></font> (Hwnd (), EM_SETSEL, 0, -1);
}
};</font></pre>
<hr>
This is how the edit control may be used:
<hr>
<pre><font face="courier">class <font color="#cc0066"><b>Controller</b></font>
{
public:
Controller(HWND hwnd);
...
private:
Edit _edit;
char _string [maxLen];
};
<font color="#cc0066"><b>Controller::Controller</b></font> (HWND hwnd)
: _edit (hwnd, IDC_EDIT)
{
_edit.SetFocus ();
...
}
void <font color="#cc0066"><b>Controller::Command</b></font> (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case IDC_EDIT:
if (_edit.IsChanged(HIWORD (wParam)))
{
_edit.GetString (_string, maxLen);
}
break;
...
}
}
</font></pre>
<hr>
But, of course, the most likely place to use controls is in a <a href="windlg.html">Dialog Box</a>.
<hr>
</table>
<td width=60>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -