为什么我用VS2019 创建的默认MFC 对话框程序,给控件添加变量会出现“找不到具有指定的对话框类?”
1、类视图是空的
2.双击Button 控件不是添加响应函数
3.为button添加变量出现找不到
都是默认创建的对话框程序!
你先用classwizard创建一下对话框类看看
你可能改了对话框的ID
原因是,VS在修改了对话框ID后,会同步修改到Resource.h和 .cpp文件,但是在.h的类声明中,还是原来的ID,导致该错误。
解决:
1.检查.cpp文件中类构造函数中,绑定的ID是否正确
2.检查.h文件中对话框数据部分,对话框ID属性,修改为当前的ID,而不是默认的IDD_DIALOG1这种
创建一个基于对话框的MFC工程,在对话框中放置一个静态文本控件Static Text,一个按钮控件Button,通过属性窗口修改默认显示的名称和文本,如图:
现在我们可以通过通过消息机制实现单击改变字体大小的操作。
MFC提供了一种便捷实现消息映射的操作方法,在界面设计窗口,双击按钮,此时自动完成了一个完整的消息映射就完成了,我们只需要在消息响应函数中实现我们想要完成的操作就可以了。
void CMFCDemoDlg::OnBnClickedButton1() {
// TODO: 在此添加控件通知处理程序代码
static int size = 50;
if (size <= 500) {
size += 50;
// 修改控件ID为IDC_STATIC1的字体(此函数在上一篇已给出)
changeFont(IDC_STATIC1, L"黑体", size);
}
else size = 50;
}
运行程序,可以看到每单击一次按钮,字体就会变大,代表消息响应成功。
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
这个问题可能是由于控件ID对应的资源没有被正确导入或者命名空间不一致导致的。可以尝试以下几个步骤:
检查资源文件是否已正确导入。在Solution Explorer中找到资源文件,单击右键并选择“属性”。在属性窗口中检查资源ID是否正确,并确认资源文件已被正确地导入到项目中。
检查命名空间是否一致。打开您的对话框头文件,找到类定义,确保它与rc文件中的类名称匹配。
如果在资源文件中添加了新的控件,可能需要将其ID添加到对话框头文件中。打开对话框头文件,找到控件的声明,确保它们与资源文件中的ID匹配。
以下是一个默认的MFC对话框程序中的代码示例,其中包括添加控件和响应函数的步骤:
// MyDlg.h
#pragma once
class CMyDlg : public CDialogEx
{
public:
CMyDlg(CWnd* pParent = nullptr); // standard constructor
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MY_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
// Add member variable for new control
CString m_strEdit;
public:
afx_msg void OnBnClickedButton();
// Add handler for new control
afx_msg void OnEnChangeEdit();
CEdit m_EditCtrl; // Add control variable for new control
};
// MyDlg.cpp
#include "pch.h"已黑化的小白 #include "My.h"已黑化的小白 #include "MyDlg.h"已黑化的小白 #include "afxdialogex.h"已黑化的小白
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MY_DIALOG, pParent)
, m_strEdit(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// Add DDX line for new control
DDX_Text(pDX, IDC_EDIT1, m_strEdit);
// Add DDX line for existing control
DDX_Control(pDX, IDC_EDIT1, m_EditCtrl);
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
// Add message handler for new control
ON_EN_CHANGE(IDC_EDIT1, &CMyDlg::OnEnChangeEdit)
// Add message handler for existing control
ON_BN_CLICKED(IDC_BUTTON1, &CMyDlg::OnBnClickedButton)
END_MESSAGE_MAP()
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CMyDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
// Add handler for new control
void CMyDlg::OnEnChangeEdit()
{
UpdateData(TRUE); // Get the value of the edit control and update member variable
}
// Add handler for existing control
void CMyDlg::OnBnClickedButton()
{
m_EditCtrl.SetWindowText(_T("Button clicked")); // Change the caption of the edit control
}
希望这些步骤和代码片段能够帮助您解决问题。
如果我的回答解决了您的问题,请采纳!