+ -
当前位置:首页 → 问答吧 → 创建自定义消息,提示错误。

创建自定义消息,提示错误。

时间:2011-12-01

来源:互联网

VS2010 MFC Class Wizard 向导创建创建“自定义消息”,这是发生了错误。
1、创建“不注册的”自定义消息“OnHello”。
2、创建“注册的”自定义消息“OnTestmessage”。

创建完成后代码如下:
1、头文件:
// ClassWarzidDlg.h : header file
//

#pragma once


// CClassWarzidDlg dialog
class CClassWarzidDlg : public CDialogEx
{
// Construction
public:
CClassWarzidDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
enum { IDD = IDD_CLASSWARZID_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support


// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
afx_msg LRESULT OnHello(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnTestmessage(WPARAM wParam, LPARAM lParam);
};

2、对应的类文件

// ClassWarzidDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ClassWarzid.h"
#include "ClassWarzidDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();

// Dialog Data
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CClassWarzidDlg dialog




CClassWarzidDlg::CClassWarzidDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CClassWarzidDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClassWarzidDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CClassWarzidDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(hello, &CClassWarzidDlg::OnHello)
错误一:Error 1 error C2065: 'hello' : undeclared identifier c:\users\administrator.pc-20110905onbv\desktop\demo\clr\classwizard\classwarzid\classwarzid\classwarziddlg.cpp 66 1 ClassWarzid

错误二: 2 IntelliSense: identifier "hello" is undefined c:\users\administrator.pc-20110905onbv\desktop\demo\clr\classwizard\classwarzid\classwarzid\classwarziddlg.cpp 66 2 ClassWarzid


ON_REGISTERED_MESSAGE(TestMessage, &CClassWarzidDlg::OnTestmessage)

错误三: 3 IntelliSense: identifier "TestMessage" is undefined c:\users\administrator.pc-20110905onbv\desktop\demo\clr\classwizard\classwarzid\classwarzid\classwarziddlg.cpp 67 2 ClassWarzid


END_MESSAGE_MAP()


// CClassWarzidDlg message handlers

BOOL CClassWarzidDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 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 CClassWarzidDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CClassWarzidDlg::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();
}
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CClassWarzidDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}



afx_msg LRESULT CClassWarzidDlg::OnHello(WPARAM wParam, LPARAM lParam)
{
return 0;
}


afx_msg LRESULT CClassWarzidDlg::OnTestmessage(WPARAM wParam, LPARAM lParam)
{
return 0;
}

问题一:这些自定义消息使用“Class Wizard向导”创建的,怎么还会提示错误信息???

问题二:这些错误是什么原因造成的?应该怎样修正???

问题三:“注册的自定义消息”和“没有注册的自定义消息”的差别和联系???

作者: starrychat   发布时间: 2011-12-01

问题其实很简单,大侠可以用VS2010创建VC项目试试。。。

作者: starryplayer   发布时间: 2011-12-01

ON_MESSAGE(hello, &CClassWarzidDlg::OnHello)
-----------------------------
之前需定义 
C/C++ code
#define hello WM_USER + 0x0001

但一般都大写,如
C/C++ code
#define MSG_HELLO WM_USER + 0x0001

ON_MESSAGE(MSG_HELLO, &CClassWarzidDlg::OnHello)

作者: crybird   发布时间: 2011-12-01

你这里的ON_MESSAGE(hello, &CClassWarzidDlg::OnHello)
中的标示符hello有定义吗?

作者: VisualEleven   发布时间: 2011-12-01

没有自定义消息hello
参见2楼

作者: believe_me   发布时间: 2011-12-01

参见 2楼代码,就应该 没有问题了。。。

作者: shen_wei   发布时间: 2011-12-01

你不注册的消息,应该是自己定义一个ID,然后触发对应的消息响应函数

作者: oyljerry   发布时间: 2011-12-01