0

我需要从另一个函数更改 testAppGUI(testAppGUI 是一个表单)的可见属性。该函数在一个单独的文件中,它不在一个类中。

如果我尝试做

testAppGUI::Visible = false;

我只是得到错误

C2597:非法引用非静态成员 'System::Windows::Forms::Control::Visible'

如果我尝试像这样创建对象的实例

testAppGUI^ formProperty = gcnew testAppGUI;

然后做

formProperty->Visible = false; nothing happens?!

任何人都可以解释如何做到这一点?

提前致谢。

编辑:这是更多代码

在 testApp.cpp

#include "stdafx.h"
#include "testAppGUI.h"

using namespace testApp;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    Application::Run(gcnew testAppGUI());
    return 0;
}

在 testAppGUI.h

#pragma once

#include "HideAndShowGUI.h"

namespace testApp {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::IO;

    public ref class testAppGUI : public System::Windows::Forms::Form
    {

    public:
        testAppGUI(void)
        {
            InitializeComponent();
        }

    protected:
        ~testAppGUI()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    ...

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            ...

        }
#pragma endregion

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             hideGUI();
         }
};
}

HideAndShowGUI.cpp

#include "stdafx.h"

#include "testAppGUI.h"

using namespace testApp;

void hideGUI(){
    //Hide the form, this function should be able to be called by all functions in the program. Not just from forms!

}

void showGUI(){
    //Unhide/Show the form, this function should be able to be called by all functions in the program. Not just from forms!

}

hideGUI 和 showGUI 在 HideAndShowGUI.h 中声明

4

1 回答 1

2

如果您已经有要隐藏的表单实例,则必须将该表单的引用传递给要更改属性的函数。

您可以通过直接提供表单作为函数的参数来做到这一点,或者如果函数是类的成员,您可以将表单传递给类(和实例)(并将其存储为成员变量)。其中哪一个更适合您取决于您​​的特定上下文,如果没有更多代码,我们将无法访问。

注意:您的第一个片段与您的第二个片段冲突:第一个片段form1用作变量,第二个片段用作类型。如果你已经有了变量form1,你可以设置它的属性:

form1->Visible = false;
于 2012-06-16T12:14:46.307 回答