如何在 Visual C++ 中显示和隐藏窗体?

发布于 2024-09-06 19:38:09 字数 1761 浏览 5 评论 0原文

大家好,我是 Visual C++ 的新手,但不是 C++。我在尝试弄清楚如何显示/隐藏表单时遇到问题。

假设我有一个表单 Form1 和另一个表单 TestForm。在 Form1.h 的按钮单击函数中,我有代码:

Form1::Hide();
TestForm^ form = gcnew TestForm();
form->Show();

并且它工作正常。我单击按钮,Form1 消失,TestForm 出现。但是,如果我在 TestForm.h 中执行相同的操作(仅更改设置为显示/消失的表单),我会在 Form1.h(曾经有效)和 TestForm.h 中收到大量编译器错误

  Form1.cpp
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(86): error C2065: 'Form1' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(86): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(86): error C2061: syntax error : identifier 'Form1'
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(87): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(87): error C2227: left of '->Show' must point to class/struct/union/generic type
          type is ''unknown-type''
  TestForm.cpp
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(103): error C2065: 'TestForm' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(103): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(103): error C2061: syntax error : identifier 'TestForm'
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(104): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(104): error C2227: left of '->Show' must point to class/struct/union/generic type
          type is ''unknown-type''

Hey guys, I'm brand new to Visual C++, but not C++. I'm having issues trying to figure out how to show/hide forms.

Let's say I have a form Form1 and another form TestForm. In a button click function in Form1.h I have the code:

Form1::Hide();
TestForm^ form = gcnew TestForm();
form->Show();

And it works fine. I click the button, and Form1 disappears and TestForm appears. But if I do the same thing in TestForm.h (just changing which forms are set to appear/disappear) I get a plethora of compiler errors in both Form1.h (which used to work) and TestForm.h

  Form1.cpp
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(86): error C2065: 'Form1' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(86): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(86): error C2061: syntax error : identifier 'Form1'
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(87): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\TestForm.h(87): error C2227: left of '->Show' must point to class/struct/union/generic type
          type is ''unknown-type''
  TestForm.cpp
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(103): error C2065: 'TestForm' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(103): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(103): error C2061: syntax error : identifier 'TestForm'
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(104): error C2065: 'form' : undeclared identifier
c:\users\alex\documents\visual studio 2010\projects\test\test\Form1.h(104): error C2227: left of '->Show' must point to class/struct/union/generic type
          type is ''unknown-type''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-09-13 19:38:09

该问题很可能是由于您将标头包含到 .cpp 文件中的顺序造成的。在最初的情况下,当您在 Form1.cpp 中工作时,在包含 Form1.h 之前,“TestForm”是已知类型。一旦您切换头文件将调用您的方法,情况就不再是这样了。

我建议将事件处理程序(按钮单击函数)移至 .cpp 文件中。您的 .cpp 文件可以同时包含两个标头,并且编译器将适当地找到表单定义及其方法,无论您包含标头的顺序如何。

The problem is most likely due to the order in which you're including the headers into the .cpp files. In the original case, when you were working in Form1.cpp, "TestForm" was a known type before Form1.h was included. As soon as you switch the header files will your method calls, this isn't the case anymore.

I recommend moving your event handlers (the button click functions) to your .cpp files. Your .cpp files can both include both headers, and the compiler will find the form definitions, with their methods, appropriately, no matter what order you include the headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文