提问:关于 “改变窗口大小的程序时,按钮不能正确地改变大小” 的问题

ysysbaobei 2009-01-05 01:44:12
题目要求:
当你执行上面改变窗口大小的程序时,你就会发现按钮自己能正确地改变大小。

问题:
发现未能实现,窗口大小改变。按钮大小跟着改变的功能


// button3.cpp

#include <afxwin.h>
#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();
afx_msg void Onsize(UINT, int, int);
DECLARE_MESSAGE_MAP()
};

// A message handler function
void CButtonWindow::HandleButton()
{
MessageBeep(-1);
}

// A message handler function
void CButtonWindow::Onsize(UINT nType, int cx, int cy)
{
CRect r;
GetClientRect(&r);
r.InflateRect(-20, -20);
button->MoveWindow(&r);
}

// The message map
BEGIN_MESSAGE_MAP(CButtonWindow, CFrameWnd)
ON_BN_CLICKED(IDB_BUTTON, HandleButton)
ON_WM_SIZE()
END_MESSAGE_MAP()

// The InitInstace 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);
}
...全文
103 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2009-01-05
  • 打赏
  • 举报
回复
发现了问题了
afx_msg void Onsize(UINT, int, int);
void CButtonWindow::Onsize(UINT nType, int cx, int cy)
其中的Onsize 应该为 OnSize,改过来后,就正确了
结贴了
ysysbaobei 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dahua010 的回复:]
你的
button
实例化对象了么?换句话说,和界面的BUTTON关联了么
[/Quote]

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);
}

这段代码实例化 和 关联了吧
dahua010 2009-01-05
  • 打赏
  • 举报
回复
你的
button
实例化对象了么?换句话说,和界面的BUTTON关联了么
ysysbaobei 2009-01-05
  • 打赏
  • 举报
回复
顶上去

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧