Visual C ++ 2005:如何在.cpp文件中访问Win32表单控件值?

时间:2022-02-11 00:07:06

I've recently started poking around in Visual Studio 2005, and I'm mucking about in Visual C++. When I double click on a control in the designer, it opens the .h file, which I've understood to be for prototype declarations, and if I put all the guts to the functions in there, I can get my program to work, but I don't like having the code in the .h file.

我最近开始在Visual Studio 2005中开玩笑,我在Visual C ++中苦苦挣扎。当我双击设计器中的控件时,它会打开.h文件,我已经知道它是用于原型声明的,如果我把所有的内容都放到那里的函数中,我可以让我的程序工作,但我不喜欢在.h文件中使用代码。

However, when I put things in a .cpp file, I can't seem to access the controls at all. I get a lot of compiler errors and such, and I'm wondering what VC++ is expecting, as I'm more used to a GCC/MinGW environment.

但是,当我把东西放在.cpp文件中时,我似乎无法访问控件。我得到了很多编译错误等等,我想知道VC ++的期望是什么,因为我更习惯于GCC / MinGW环境。

The errors I receive the most are:

我收到的错误最多的是:

error C2228: left of '.trackBar1/.value/.etc' must have class/struct/union
error C2227: left of '->trackBar1/->Value/->etc' must point to class/struct/union/generic type

错误C2228:'。trackBar1 / .value / .etc'左边必须有class / struct / union错误C2227:左边的' - > trackBar1 / - > Value / - > etc'必须指向class / struct / union / generic类型

I've tried the following to access the control:

我尝试了以下访问控件:

Junk::Form1::trackBar1->value
Junk::Form1::trackBar1.value
Junk::Form1->trackBar1
Junk->Form1->trackBar1
this->trackBar1->value //This is legal in the .h file, and how I can get it to work there
trackBar1->value

And a few others that are just attempts out of desperation. I've tried specifying the same namespace and everything as present in the .h file, and I still cannot seem to access the control.

还有一些只是出于绝望的尝试。我已经尝试指定相同的命名空间和.h文件中存在的所有内容,我似乎仍然无法访问该控件。

I'm using Visual Studio 2005, with a Visual C++ CLR Win32 form application. The code that Visual Studio uses to create instances of the controls is:

我正在使用Visual Studio 2005,使用Visual C ++ CLR Win32表单应用程序。 Visual Studio用于创建控件实例的代码是:

this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());  

In Dev-C++ or code::blocks, I'm used to putting the class declarations in the .h file, and then specifying the functions in the .cpp file by doing class::function, but in Visual Studio, I just cannot seem to figure out why I can't do the same, or what I'm doing horribly wrong and stupid.

在Dev-C ++或code :: blocks中,我习惯将类声明放在.h文件中,然后通过执行class :: function指定.cpp文件中的函数,但在Visual Studio中,我只是不能似乎弄清楚为什么我不能这样做,或者我正在做的可怕的错误和愚蠢。

Thank you.

2 个解决方案

#1


Thanks for clarifying the question, here's some ideas

谢谢你澄清这个问题,这里有一些想法

  • Ensure the .h file is included in the corresponding cpp, for example make sure that if you've defined
  • 确保.h文件包含在相应的cpp中,例如,如果已定义,请确保

foo.h

class
{
...
};

be sure that foo.cpp has foo.h included, like this:

确保foo.cpp包含foo.h,如下所示:

#include "foo.h"

... definitions ...
  • It looks like that within the context of your class that referencing trackBar1 should be valid, that is your last example above. If you are outside of the scope of your class, you will need to declare an instance of the enclosing class and reference its instance of trackBar.
  • 看起来在您的类的上下文中,引用trackBar1应该是有效的,这是您上面的最后一个示例。如果您不在类的范围内,则需要声明封闭类的实例并引用其trackBar实例。

Also, like I said before:

另外,就像我之前说过的那样:

You need to know if your "Form1" is a value or instance of an object or a class or a namespace. If its a namespace/class latter than using :: to clarify the namespace is appropriate. From the error it looks like its likely not a namespace/class/struct. You seem to also be trying to treat it like a pointer. Is it a pointer? How is it newed?

您需要知道“Form1”是对象的值或实例,还是类或命名空间。如果它的名称空间/类后面比使用::来澄清名称空间是合适的。从错误看起来它可能不是命名空间/类/结构。你似乎也试图把它当成一个指针。它是指针吗?它是如何新的?

I would simplify to experimenting with an extremely simple C++ header/cpp example. Confirm this works. Slowly copy-paste in your functionality and see at what stage it breaks. Make a hello world program is VS 2005 and add in your code.

我将简化为试验一个非常简单的C ++ header / cpp示例。确认这是有效的。慢慢复制粘贴您的功能,看看它破坏的阶段。制作一个hello world程序是VS 2005并添加你的代码。

When you find where it breaks, and its still broken, edit your question with your simplified set of sample code and we can better help you debug.

当您发现它中断的地方,它仍然破碎时,请使用简化的示例代码编辑您的问题,我们可以更好地帮助您进行调试。

#2


This may have happened because the controls were changed (i.e. double clicking). Go back to the control (in design view) and change the name. Then change it back. Then recompile.

这可能是因为控件被更改(即双击)。返回控件(在设计视图中)并更改名称。然后改回来。然后重新编译。

Cheers!

#1


Thanks for clarifying the question, here's some ideas

谢谢你澄清这个问题,这里有一些想法

  • Ensure the .h file is included in the corresponding cpp, for example make sure that if you've defined
  • 确保.h文件包含在相应的cpp中,例如,如果已定义,请确保

foo.h

class
{
...
};

be sure that foo.cpp has foo.h included, like this:

确保foo.cpp包含foo.h,如下所示:

#include "foo.h"

... definitions ...
  • It looks like that within the context of your class that referencing trackBar1 should be valid, that is your last example above. If you are outside of the scope of your class, you will need to declare an instance of the enclosing class and reference its instance of trackBar.
  • 看起来在您的类的上下文中,引用trackBar1应该是有效的,这是您上面的最后一个示例。如果您不在类的范围内,则需要声明封闭类的实例并引用其trackBar实例。

Also, like I said before:

另外,就像我之前说过的那样:

You need to know if your "Form1" is a value or instance of an object or a class or a namespace. If its a namespace/class latter than using :: to clarify the namespace is appropriate. From the error it looks like its likely not a namespace/class/struct. You seem to also be trying to treat it like a pointer. Is it a pointer? How is it newed?

您需要知道“Form1”是对象的值或实例,还是类或命名空间。如果它的名称空间/类后面比使用::来澄清名称空间是合适的。从错误看起来它可能不是命名空间/类/结构。你似乎也试图把它当成一个指针。它是指针吗?它是如何新的?

I would simplify to experimenting with an extremely simple C++ header/cpp example. Confirm this works. Slowly copy-paste in your functionality and see at what stage it breaks. Make a hello world program is VS 2005 and add in your code.

我将简化为试验一个非常简单的C ++ header / cpp示例。确认这是有效的。慢慢复制粘贴您的功能,看看它破坏的阶段。制作一个hello world程序是VS 2005并添加你的代码。

When you find where it breaks, and its still broken, edit your question with your simplified set of sample code and we can better help you debug.

当您发现它中断的地方,它仍然破碎时,请使用简化的示例代码编辑您的问题,我们可以更好地帮助您进行调试。

#2


This may have happened because the controls were changed (i.e. double clicking). Go back to the control (in design view) and change the name. Then change it back. Then recompile.

这可能是因为控件被更改(即双击)。返回控件(在设计视图中)并更改名称。然后改回来。然后重新编译。

Cheers!