C++ 未能安装按钮消息处理程序
我一直在尝试编写一个基于帧的MFC应用程序,其中包含一个按钮,对点击进行简单响应。不幸的是,该按钮似乎对我的操作没有反应。以下是应用程序的代码:C++ 未能安装按钮消息处理程序,c++,mfc,C++,Mfc,我一直在尝试编写一个基于帧的MFC应用程序,其中包含一个按钮,对点击进行简单响应。不幸的是,该按钮似乎对我的操作没有反应。以下是应用程序的代码: 1) OS.cpp: 2) OS.h: 3) button.h(包含自定义按钮类): 你能告诉我有什么问题吗?如何让按钮在点击后改变文本?提前谢谢 您的btnHelloWorld\u t::GetID()是静态方法!!创建第一个按钮时,GetID()返回201。然后,当调用消息映射时,GetID()返回202。然后,如果再次调用消息映射,则GetID(
1) OS.cpp: 2) OS.h: 3) button.h(包含自定义按钮类): 你能告诉我有什么问题吗?如何让按钮在点击后改变文本?提前谢谢
1.抱歉,我忘记了静态方法的这种行为。我打算使用它来允许新创建的按钮具有不同的ID。您对静态
btnHelloWorld\u t::GetID()的问题我不明白您当时为什么这样做。。。这段代码只是一场灾难。
#include "stdafx.h"
#include "mfc_includes.h" // some general includes like afxwin.h
#include "OS.h"
#include "MainFrm.h"
#include "button.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(COSApp, CWinApp)
END_MESSAGE_MAP()
COSApp::COSApp() {}
COSApp theApp;
btnHelloWorld_t my_button;
BOOL COSApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
CMainFrame* pFrame = new CMainFrame;
if (!pFrame)
return FALSE;
m_pMainWnd = pFrame;
pFrame->Create(L"", L"The application", WS_OVERLAPPEDWINDOW, CRect(100, 100, 500, 500));
my_button.Create(L"Hello World!", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(30, 30, 150, 80), pFrame, btnHelloWorld_t::GetID());
HFONT font = CreateFont(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Times New Roman");
SendMessage(my_button.m_hWnd, WM_SETFONT, (WPARAM)font, true);
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
class COSApp : public CWinApp
{
public:
virtual BOOL InitInstance();
afx_msg void OnAppAbout();
DECLARE_MESSAGE_MAP()
};
extern COSApp theApp;
#pragma once
#include "mfc_includes.h"
class btnHelloWorld_t : public CButton
{
static const int is_button = 0x200;
static int id;
public:
btnHelloWorld_t()
{
id++;
};
static const int GetID()
{
return id;
};
afx_msg void Click();
DECLARE_MESSAGE_MAP()
};
int btnHelloWorld_t::id = 0x200;
afx_msg void btnHelloWorld_t::Click()
{
SetWindowText(L"Hello!");
}
BEGIN_MESSAGE_MAP(btnHelloWorld_t, CButton)
ON_BN_CLICKED(btnHelloWorld_t::GetID(), &btnHelloWorld_t::Click)
END_MESSAGE_MAP()