📄 mfc.htm
字号:
// button1.cpp<br>
#include <afxwin.h><br>
#define IDB_BUTTON 100<br>
// Declare the application class<br>
class CButtonApp : public CWinApp<br>
{<br>
public:<br>
virtual BOOL InitInstance();<br>
};<br>
// Create an instance of the application class<br>
CButtonApp ButtonApp; <br>
// Declare the main window class<br>
class CButtonWindow : public CFrameWnd<br>
{ <br>
CButton *button;<br>
public:<br>
CButtonWindow();<br>
};<br>
// The InitInstance function is called once<br>
// when the application first executes<br>
BOOL CButtonApp::InitInstance()<br>
{<br>
m_pMainWnd = new CButtonWindow();<br>
m_pMainWnd->ShowWindow(m_nCmdShow);<br>
m_pMainWnd->UpdateWindow();<br>
return TRUE;<br>
}<br>
// The constructor for the window class<br>
CButtonWindow::CButtonWindow()<br>
{ <br>
CRect r;<br>
// Create the window itself<br>
Create(NULL, <br>
"CButton Tests", <br>
WS_OVERLAPPEDWINDOW,<br>
CRect(0,0,200,200));<br>
// Get the size of the client rectangle<br>
GetClientRect(&r);<br>
r.InflateRect(-20,-20);<br>
// Create a button<br>
button = new CButton();<br>
button->Create("Push me",<br>
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,<br>
r,<br>
this,<br>
IDB_BUTTON);<br>
}<br>
上面的代码与前面介绍的代码几乎相同。CButton类的Create函数共有5个参数。前四个与CStatic的相同。第五个参数为按钮的资源ID。资源ID是用来标识消息映射中按钮的唯一整数值。常数值IDB_BUTTON已经在程序的顶部做了定义。“IDB_”是任选的,只是该常量ID是用来表示按钮的。它的值为100,因为100以内的值都为系统所保留。你可以使用任何大于99的值。<br>
CButton类所允许的样式属性与CStatic类的是不同的。定义了11个不同的“BS”(“Button Style”)常量。完整的“BS”常量列表可在用Search命令查找CButton,并选择“button style”。这里我们要用的是BS_PUSHBUTTON样式,它表示我们要一正常的的按钮方式来显示该按钮。我们还使用了两个熟悉的“WS”属性: WS_CHILD和WS_VISIBLE。我们将在后面介绍其它一些样式。<br>
当你运行代码时,会注意到按钮响应了用户事件。既它加亮了。除此之外它没有做任何事情,因为我们还没有教它怎样去做。我们需要编写消息映射来使按钮做一些感兴趣的事情。<br>
建立消息映射<br>
下面的代码包含有消息映射,也包含有新的处理单击按钮的函数(当用户单击按钮时会响一下喇叭)。它只是前面代码的一个简单的扩充:<br>
// button2.cpp<br>
#include <afxwin.h><br>
#define IDB_BUTTON 100<br>
// Declare the application class<br>
class CButtonApp : public CWinApp<br>
{<br>
public:<br>
virtual BOOL InitInstance();<br>
};<br>
// Create an instance of the application class<br>
CButtonApp ButtonApp; <br>
// Declare the main window class<br>
class CButtonWindow : public CFrameWnd<br>
{ <br>
CButton *button;<br>
public:<br>
CButtonWindow();<br>
afx_msg void HandleButton();<br>
DECLARE_MESSAGE_MAP() <br>
};<br>
// The message handler function<br>
void CButtonWindow::HandleButton()<br>
{<br>
MessageBeep(-1);<br>
}<br>
// The message map<br>
BEGIN_MESSAGE_MAP(CButtonWindow, CFrameWnd)<br>
ON_BN_CLICKED(IDB_BUTTON, HandleButton)<br>
END_MESSAGE_MAP()<br>
// The InitInstance function is called once<br>
// when the application first executes<br>
BOOL CButtonApp::InitInstance()<br>
{<br>
m_pMainWnd = new CButtonWindow();<br>
m_pMainWnd->ShowWindow(m_nCmdShow);<br>
m_pMainWnd->UpdateWindow();<br>
return TRUE;<br>
}<br>
// The constructor for the window class<br>
CButtonWindow::CButtonWindow()<br>
{ <br>
CRect r;<br>
// Create the window itself<br>
Create(NULL, <br>
"CButton Tests", <br>
WS_OVERLAPPEDWINDOW,<br>
CRect(0,0,200,200));<br>
// Get the size of the client rectangle<br>
GetClientRect(&r);<br>
r.InflateRect(-20,-20);<br>
// Create a button<br>
button = new CButton();<br>
button->Create("Push me",<br>
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,<br>
r,<br>
this,<br>
IDB_BUTTON);<br>
}<br>
主要修改了三个方面: <br>
CButtonWindow的类说明现在包含了一个新的成员函数和一个新的表示消息映射的宏。HandleButton函数是正常的C++函数,它通过afx_msg标签确定为消息处理函数。该函数需要一些特殊的约束,例如,它必须是void型并且它不能接收任何参数。DECLARE_MESSAGE_MAP宏建立了消息映射。函数和宏都必须是public型的。 <br>
HandleButton函数作为成员函数以同样的方式来建立。在该函数中,我们调用了Windows API中的MessageBeep函数。 <br>
用宏来建立消息映射。在代码中,你可以看见BEGIN_MESSAGE_MAP宏接收两各参数。第一个指定了使用消息映射的类的名称。第二个是基类。然后是ON_BN_CLICKED宏,接受两个参数控制的ID和该ID发送命令消息时所调用的函数。最后,消息映射用END_MESSAGE_MAP来结束。 <br>
当用户单击按钮时,它向其包含该按钮的父窗口发送了一个包含其ID的命令消息。那是按钮的缺省行为,这就是该代码工作的原因。按钮向其父窗口发送消息,是因为它是子窗口。父窗口截取该消息并用消息映射来确定所要调用的函数。MFC来安排,只要指定的消息一出现,相应的函数就会被调用。<br>
ON_BN_CLICKED消息是CButton发送的唯一感兴趣的消息。它等同于CWnd中的ON_COMMAND消息,只是一个更简单方便的同义词而已。<br>
改变大小的消息<br>
在上面的代码中,由于有了消息映射,从CFrameWnd继承来的应用程序窗口认出按钮有按钮产生的单击消息并响应之。加入消息映射的ON_BN_CLICKED宏指定了按钮的ID和窗口在接收到来自按钮的命令消息时应调用的函数。因为只要用户单击了按钮,按钮就会自动把其ID发送父窗口,这样才能允许代码正确地处理按钮事件。<br>
作为该应用程序的主窗口的框架窗口自己也有传递消息的能力。大约有100不同的消息可用,它们都是从CWnd类继承来的。从MFC帮助文件中浏览CWnd类的成员函数,你就会看到所有的这些消息。查看所有以“On”开头的成员函数。<br>
你可能已经注意到了,至今为止所有的代码都不能很好地处理尺寸变化。当窗口变化大小时,窗口的框架会做相应的调整,但是窗口中调的内容仍原处不动。可以通过处理尺寸变化的事件来更好的处理这
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -