奇怪,对话框程序中不能自定义消息

Sunlet 2000-09-07 09:58:00
我想在对话框的程序中定义消息:
#define MYMSG WM_USER+100
.h文件中
afx_msg LRESULT OnMsg(WPARAM w,LPARAM l)

DECLARE_MESSAGE_MAP上面
ON_MESSAGE(MYMSG,OnMsg)

.cpp中
afx_msg LRESULT C...Dlg::OnMsg(WPARAM w,LPARAM l)
{
AfxMessageBox("adsf");
return 1;
}
编译出现七个错误,我用同样的方法在sdi程序中测试,成功.
请问: 1. 是不是在对话框程序中不能这样自定义消息
2. 若是,请问怎样在对话框程序中自定义消息.
谢谢!



...全文
215 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Holly 2000-09-08
  • 打赏
  • 举报
回复
检查testmsgdlg.cpp中引用的头文件的书写,尤其是最后一个include 的头文件!
坎坷的菜贩 2000-09-08
  • 打赏
  • 举报
回复
呵呵,一看就知道是语法错误!检查检查吧(很可能是哪个分号)
iforever 2000-09-08
  • 打赏
  • 举报
回复
不能头疼医头脚疼医脚,
U皮特U 2000-09-08
  • 打赏
  • 举报
回复
To Sunlet: 你程序中第86,89行代码是什么?
To bulehawk: 常规作法在函数的实现中确实不用afx_msg,但加上不会产生错误。错误的原因肯定不在这里。我已经按Sunlet的方式做过测试,没有问题,可能存在其它原因。
bulehawk 2000-09-08
  • 打赏
  • 举报
回复
LRESULT C...Dlg::OnMsg(WPARAM w,LPARAM l)
{
AfxMessageBox("adsf");
return 1;
}
把afx_msg 去掉
Sunlet 2000-09-08
  • 打赏
  • 举报
回复
就在ON_MESSAGE(..)一行,出现了一下七个错误.
Compiling...
testmsgDlg.cpp
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(86) : error C2059: syntax error : ';'
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(86) : error C2143: syntax error : missing ';' before ','
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(86) : error C2143: syntax error : missing ';' before '}'
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(86) : error C2143: syntax error : missing ';' before '}'
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(89) : error C2143: syntax error : missing ';' before '{'
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(89) : error C2447: missing function header (old-style formal list?)
D:\CurrentWork\VotePost\testmsg\testmsgDlg.cpp(89) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

testmsg.exe - 7 error(s), 0 warning(s)
Sunlet 2000-09-08
  • 打赏
  • 举报
回复
一个愚蠢的错误!!!谢谢各位,给你们加分了.
U皮特U 2000-09-08
  • 打赏
  • 举报
回复
#define WM_MSG定义的写法不对,后面不需要分号';'
Holly 2000-09-08
  • 打赏
  • 举报
回复
将#define WM_MSG WM_USER+200;
中的';'去掉!

Sunlet 2000-09-08
  • 打赏
  • 举报
回复
谢谢这么多好心人的关心,我把.h和.cpp文件全贴出来吧
#define AFX_TESTMSGDLG_H__1A23F78D_84EC_11D4_A562_00103C7C7A77__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CTestmsgDlg dialog
#define WM_MSG WM_USER+200;
class CTestmsgDlg : public CDialog
{
// Construction
public:

CTestmsgDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CTestmsgDlg)
enum { IDD = IDD_TESTMSG_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestmsgDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
//{{AFX_MSG(CTestmsgDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg LRESULT OnMsg(WPARAM w,LPARAM l);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TESTMSGDLG_H__1A23F78D_84EC_11D4_A562_00103C7C7A77__INCLUDED_)
// testmsgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testmsg.h"
#include "testmsgDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestmsgDlg dialog

CTestmsgDlg::CTestmsgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestmsgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestmsgDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestmsgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestmsgDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestmsgDlg, CDialog)
//{{AFX_MSG_MAP(CTestmsgDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()

ON_MESSAGE(WM_MSG,OnMsg)
//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestmsgDlg message handlers

BOOL CTestmsgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestmsgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CTestmsgDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CTestmsgDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

LRESULT CTestmsgDlg::OnMsg(WPARAM w, LPARAM l)
{
AfxMessageBox("ok");
return 1;

}

U皮特U 2000-09-07
  • 打赏
  • 举报
回复
没有问题呀!方法都是一样的。你的编译错误是什么?贴出来看看。

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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