WEB开发网
开发学院软件开发VC Visual C++ MFC简明教程(4) 阅读

Visual C++ MFC简明教程(4)

 2010-03-28 20:34:43 来源:WEB开发网   
核心提示:上面的代码与前面介绍的代码几乎相同,CButton类的Create函数共有5个参数,Visual C++ MFC简明教程(4)(2),前四个与CStatic的相同,第五个参数为按钮的资源ID,我们需要编写消息映射来使按钮做一些感兴趣的事情,建立消息映射下面的代码包含有消息映射,资源ID是用来标识消息映射中按钮的唯一整数

上面的代码与前面介绍的代码几乎相同。CButton类的Create函数共有5个参数。前四个与CStatic的相同。第五个参数为按钮的资源ID。资源ID是用来标识消息映射中按钮的唯一整数值。常数值IDB_BUTTON已经在程序的顶部做了定义。“IDB_”是任选的,只是该常量ID是用来表示按钮的。它的值为100,因为100以内的值都为系统所保留。你可以使用任何大于99的值。

CButton类所允许的样式属性与CStatic类的是不同的。定义了11个不同的“BS”(“Button Style”)常量。完整的“BS”常量列表可在用Search命令查找CButton,并选择“button style”。这里我们要用的是BS_PUSHBUTTON样式,它表示我们要一正常的的按钮方式来显示该按钮。我们还使用了两个熟悉的“WS”属性: WS_CHILD和WS_VISIBLE。我们将在后面介绍其它一些样式。

当你运行代码时,会注意到按钮响应了用户事件。既它加亮了。除此之外它没有做任何事情,因为我们还没有教它怎样去做。我们需要编写消息映射来使按钮做一些感兴趣的事情。

建立消息映射

下面的代码包含有消息映射,也包含有新的处理单击按钮的函数(当用户单击按钮时会响一下喇叭)。它只是前面代码的一个简单的扩充:

// button2.cpp
#include
#define IDB_BUTTON 100
// Declare the application class
class CButtonApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// Create an instance of the application class
CButtonApp ButtonApp;
// Declare the main window class
class CButtonWindow : public CFrameWnd
{
CButton *button;
public:
CButtonWindow();
afx_msg void HandleButton();
DECLARE_MESSAGE_MAP()
};
// The message handler function
void CButtonWindow::HandleButton()
{
MessageBeep(-1);
}
// The message map
BEGIN_MESSAGE_MAP(CButtonWindow, CFrameWnd)
ON_BN_CLICKED(IDB_BUTTON, HandleButton)
END_MESSAGE_MAP()
// The InitInstance function is called once
// when the application first executes
BOOL CButtonApp::InitInstance()
{
m_pMainWnd = new CButtonWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
// The constructor for the window class
CButtonWindow::CButtonWindow()
{
CRect r;
// Create the window itself
Create(NULL,
"CButton Tests",
WS_OVERLAPPEDWINDOW,
CRect(0,0,200,200));
// Get the size of the client rectangle
GetClientRect(&r);
r.InflateRect(-20,-20);
// Create a button
button = new CButton();
button->Create("Push me",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
r,
this,
IDB_BUTTON);
}

上一页  1 2 3 4 5 6  下一页

Tags:Visual MFC 简明教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接