无法从VC ++表单显示MessageBox [英] Trouble displaying MessageBox from VC++ form

查看:83
本文介绍了无法从VC ++表单显示MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题(或者至少对我来说似乎很奇怪)试图在VC ++ .NET表单应用程序中显示一个MessageBox。


如果我把调用MessageBox :: Show放在'.h。文件的格式中,它就可以了。

罚款。


如果我把调用放在.cpp文件中,我得到以下两个错误:


错误C2653:''MessageBoxA'':不是类或命名空间名称

错误C2660:''System :: Windows :: Forms :: Control :: Show'':函数不带

2个参数


我已按如下方式对此进行了测试:


创建一个.NET窗体应用程序,使用默认窗体Form1,然后

添加两个按钮,button1和button2。双击每个按钮以

添加默认事件处理程序。在Form1.h的类声明中,我添加了

这个:


// .h文件中的声明和定义

private:System :: Void button1_Click(System :: Object * sender,

System :: EventArgs * e)

{

MessageBox: :显示(S" Clicked Button 1",S" Title");

}


//仅在.h文件中声明,在。中定义。 cpp文件

private:System :: Void button2_Click(System :: Object * sender,

System :: EventArgs * e);


然后在Form1.cpp的类定义中,我添加了:


// .hpp文件中与.h文件中的声明对应的定义

System :: Void Form1 :: button2_Click(System :: Object * sender,

System :: EventArgs * e)

{

MessageBox: :显示(S" Clicked Button 2",S" Title");

}


当我输入代码时,我得到了预期

后自动完成MessageBox ::所以看起来System :: Windows :: Forms命名空间是已知的,

和特定的MessageBox对象一样。 System.Windows.Forms作为项目中的

引用包含在内。我还尝试明确地包括


#using< System.Windows.Forms.dll>

使用命名空间System :: Windows :: Forms;
,但这没有任何效果。


我试图弄清楚在<}中对MessageBoxA的引用是怎么回事br />
第一条错误消息。在MessageBoxA的帮助中搜索会显示对宏和预处理器的/宏和预处理器的引用。其中说


#ifdef UNICODE

#define MessageBox MessageBoxW

#else

#define MessageBox MessageBoxA

#endif //!UNICODE


显然UNICODE没有在我的.cpp文件中定义,但它在我的.h

档案?


继续这个我试验 - 添加


#include< windows.h>


到.h文件(不是你通常会做的事情 - 这是一个实验)

导致.h中出现相同的两个错误文件在.cpp文件中。那么现在好像这就是发生的事情:在

..cpp文件中包含windows.h会导致MessageBox扩展为MessageBoxA,这是

未知。从.h文件中缺少windows.h会导致MessageBox被扩展为MessageBoxW,这可以工作。但是,尝试从.cpp文件中调用

MessageBoxW明确表示再次获得相同的两个错误,

除了不同的类:


错误C2653:''MessageBoxW'':不是类或命名空间名称

错误C2660:''System :: Windows :: Forms :: Control :: Show'':function不需要

2个参数


简而言之 - 似乎没有任何方法可以显示来自
$ b的MessageBox $ b表单的.cpp代码。这留下了从

..h文件中显示它的选项,这在很多方面都是错误的,但是完成了工作。


建议?在此先感谢。

解决方案

如果你正在做#include< windows.h",MessageBox是一个扩展的宏。

到MessageBoxA或MessageBoxW,具体取决于你是否定义了UNICODE

。所以你的MessageBox :: Show然后扩展到MessageBoxA :: Show和

编译器说正确的.NET中没有MessageBoxA类或命名空间,

这是正确的。作为一种解决方法,在你的
之后尝试#undef MessageBox #include< windows.h> 。


Peter E. Granger写道:

我有一个奇怪的问题(或者至少我觉得很奇怪)
尝试在VC ++ .NET表单应用程序中显示MessageBox。

如果我将调用MessageBox :: Show放在表单的'.h文件中,它就可以了。
就好了。<如果我把调用放在.cpp文件中,我会收到以下两个错误:

错误C2653:''MessageBoxA'':不是类名或命名空间名<错误C2660:''System :: Windows :: Forms :: Control :: Show'':函数确实不接受2个参数



" Edward Diener" < ED ****** @ tropicsoft.com>在消息中写道

新闻:Ol ************** @ TK2MSFTNGP12.phx.gbl ...

如果你在做#include< windows.h",MessageBox是一个宏,
扩展到MessageBoxA或MessageBoxW,具体取决于你是否定义了UNICODE。所以你的MessageBox :: Show然后扩展到MessageBoxA :: Show和
编译器说正确的
..NET中没有MessageBoxA类或命名空间,这是正确的。作为一种解决方法,在你的#include< windows.h>之后尝试#undef MessageBox。 。




谢谢,爱德华,确认我已经确定了什么,以及解决方法。

做#undef MessageBox有效,虽然它似乎真的是违反b,b取决于我想要定义的特定事物。 (我知道,我只是删除之前的重新定义,但它似乎仍然很奇怪。)


为了彻底,我也是检查在项目配置中将

字符集切换为Unicode时发生了什么。正如预期的那样,

MessageBox现在扩展到MessageBoxW,这也是未知的。


你能否说明这背后的原因?看起来奇怪的是,在windows.h头文件中定义的

宏会阻止使用MessageBox

,因为它是Windows应用程序中的常见对象。或者是否可能有一个不同的类可以用来做同样的工作,这个问题没有?
?我在工具箱中看到了不同的对话框控件,

但没有简单的消息框控件。


再次感谢。


- Peter


Peter E. Granger写道:

" Edward Diener" < ED ****** @ tropicsoft.com>在消息中写道
新闻:Ol ************** @ TK2MSFTNGP12.phx.gbl ...

如果你正在做#include< windows.h",MessageBox是一个宏,它扩展到MessageBoxA或MessageBoxW,具体取决于你是否定义了UNICODE。所以你的MessageBox :: Show然后扩展到了MessageBoxA :: Show,并且编译器正确地说在.NET中没有
MessageBoxA类或命名空间,这是正确的。作为一种解决方法,在#include< windows.h>之后尝试#undef MessageBox。 。



感谢Edward,确认我已经确定了什么,以及
解决方法。做#undef MessageBox有效,虽然它看起来确实违反直觉,但却没有定义我想要的特定内容。 (我知道,我只是删除了之前的重新定义,但是它仍然看起来很奇怪。)

为了彻底,我还检查了如果我切换了
在项目配置中将字符集设置为Unicode。正如预期的那样,消息框现在已经扩展到MessageBoxW,这也是未知的。

你能否说明这背后的原因?




C ++中的宏在编译之前得到文本替换

MessageBox :: Show。那么编译器看到的是,如果你在调用MessageBox :: Show之前#include< windows.h>

就是MessageBoxA :: Show。由于没有

MessageBoxA ::显示编译失败。如果您使用VC ++预编译器来预编译代码以生成.i文件并查看.i文件,您将自己看到



I''m having a strange problem (or at least it seems strange to me) trying to
display a MessageBox in a VC++ .NET forms application.

If I put the call to MessageBox::Show in the form''s .h file, it works just
fine.

If I put the call in the .cpp file, I get the following two errors:

error C2653: ''MessageBoxA'': is not a class or namespace name
error C2660: ''System::Windows::Forms::Control::Show'': function does not take
2 arguments

I have tested this out as follows:

Create a .NET Windows Forms application with the default form, Form1, and
add two buttons to it, button1 and button2. Double-click on each button to
add the default event handlers. In the class declaration in Form1.h, I added
this:

// Declaration and definition together in .h file
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(S"Clicked Button 1",S"Title");
}

// Declaration only in .h file, definition in the .cpp file
private: System::Void button2_Click(System::Object * sender,
System::EventArgs * e);

Then in the class definition in Form1.cpp, I added:

// Definition in .cpp file corresponding to declaration in .h file
System::Void Form1::button2_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(S"Clicked Button 2",S"Title");
}

When I''m typing the code, I get the expected auto-completion after the
MessageBox:: so it seems that the System::Windows::Forms namespace is known,
as is the specific MessageBox object. System.Windows.Forms is included as a
reference in the project. I have also tried explicitly including

#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;

in the .cpp file, but that has no effect.

I tried to figure out what is going on with the reference to MessageBoxA in
the first error message. Searching in the help for MessageBoxA turns up a
reference to "Macros and the Preprocessor" which says

#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif // !UNICODE

So apparently UNICODE is not defined in my .cpp file, but it is in my .h
file?

Going on this I experimented -- adding

#include <windows.h>

to the .h file (not something you''d normally do -- it''s an experiment)
causes the same two errors to occur in the .h file as in the .cpp file. So
now it looks like this is what''s happening: Inclusion of windows.h in the
..cpp file causes MessageBox to be expanded into MessageBoxA which is
unknown. Absence of windows.h from the .h file causes MessageBox to be
expanded into MessageBoxW, which works. However, attempting to call
MessageBoxW explicity from the .cpp file gets me the same two errors again,
except for a different class:

error C2653: ''MessageBoxW'': is not a class or namespace name
error C2660: ''System::Windows::Forms::Control::Show'': function does not take
2 arguments

In short -- there doesn''t seem to be any way to display a MessageBox from
the .cpp code of a form. Which leaves the option of displaying it from the
..h file, which is just wrong in so many ways, but gets the job done.

Suggestions? Thanks in advance.

解决方案

If you are doing a #include <windows.h", MessageBox is a macro which expands
to either MessageBoxA or MessageBoxW depending in whether you have UNICODE
defined. So your MessageBox::Show then expands to MessageBoxA::Show and the
compiler says correctly there is no MessageBoxA class or namespace in .NET,
which is correct. As a workaround try an #undef MessageBox after your
#include <windows.h> .

Peter E. Granger wrote:

I''m having a strange problem (or at least it seems strange to me)
trying to display a MessageBox in a VC++ .NET forms application.

If I put the call to MessageBox::Show in the form''s .h file, it works
just fine.

If I put the call in the .cpp file, I get the following two errors:

error C2653: ''MessageBoxA'': is not a class or namespace name
error C2660: ''System::Windows::Forms::Control::Show'': function does
not take 2 arguments



"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...

If you are doing a #include <windows.h", MessageBox is a macro which expands to either MessageBoxA or MessageBoxW depending in whether you have UNICODE
defined. So your MessageBox::Show then expands to MessageBoxA::Show and the compiler says correctly there is no MessageBoxA class or namespace in ..NET, which is correct. As a workaround try an #undef MessageBox after your
#include <windows.h> .



Thanks, Edward, for confirming what I''d determined, and for the workaround.
Doing the #undef MessageBox works, although it seems really
counterintuitive, undefining the specific thing that I want defined. (I
know, I''m just removing a prior redefinition of it, but it still seems odd.)

Just to be thorough, I also checked what happened if I switched the
character set to Unicode in the project configuration. As expected,
MessageBox is now expanded to MessageBoxW, which is also unknown.

Can you shed any light on the reason behind this? It seems strange that the
macro that''s defined in the windows.h header prevents use of the MessageBox
when it''s such a common object in Windows applications. Or is there perhaps
a different class that can be used to do the same job, that doesn''t have
this problem? I see quite a few different dialog controls in the toolbox,
but no simple message box control.

Thanks again.

- Peter


Peter E. Granger wrote:

"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...

If you are doing a #include <windows.h", MessageBox is a macro which
expands to either MessageBoxA or MessageBoxW depending in whether
you have UNICODE defined. So your MessageBox::Show then expands to
MessageBoxA::Show and the compiler says correctly there is no
MessageBoxA class or namespace in .NET, which is correct. As a
workaround try an #undef MessageBox after your #include <windows.h> .



Thanks, Edward, for confirming what I''d determined, and for the
workaround. Doing the #undef MessageBox works, although it seems
really counterintuitive, undefining the specific thing that I want
defined. (I know, I''m just removing a prior redefinition of it, but
it still seems odd.)

Just to be thorough, I also checked what happened if I switched the
character set to Unicode in the project configuration. As expected,
MessageBox is now expanded to MessageBoxW, which is also unknown.

Can you shed any light on the reason behind this?



Macros in C++ get textually substituted before the compile sees
MessageBox::Show. So what the compiler sees, if you #include <windows.h>
before invoking MessageBox::Show is MessageBoxA::Show. Since there is no
MessageBoxA::Show the compilation fails. If you use the VC++ precompiler to
precompile your code to produce a .i file and view the .i file, you will see
this yourself.


这篇关于无法从VC ++表单显示MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆