c++ - 如何从另一个类访问Form组件

标签 c++ winforms

我有WinForm和一个类,我试图将文本设置为来自类的Form组件,但是即使从该类声明为public,它也允许我从类调用时访问任何组件。任何想法为什么这不起作用?会非常感激的,如果这个愚蠢的问题对不起,我有点菜鸟。谢谢!
OpenGL_on_a_Windows_Form.cpp

#include "stdafx.h"
#include "Form1.h"


using namespace OpenGL_on_a_Windows_Form;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}
Form1.h
#pragma once

#include "OpenGL.h"
#include <iomanip>
#include <msclr/marshal_cppstd.h>

namespace OpenGL_on_a_Windows_Form {

    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 OpenGLForm;
    using namespace System::Diagnostics;
    using namespace System::IO;


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

    public:
        Form1(void)
        {
            InitializeComponent();
            OpenGL = gcnew COpenGL(splitContainer1->Panel2, splitContainer1->Panel2->Width, splitContainer1->Panel2->Height);

        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::ComponentModel::IContainer^ components;
    protected:
    private: System::Windows::Forms::Timer^ timer1;
    public: System::Windows::Forms::TextBox^ MessageLog;

    private:

        OpenGLForm::COpenGL^ OpenGL;

#pragma region Windows Form Designer generated code
        

        void InitializeComponent(void)
        {
            this->components = (gcnew System::ComponentModel::Container());
            System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
            this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
            this->splitContainer1 = (gcnew System::Windows::Forms::SplitContainer());
            this->splitContainer2 = (gcnew System::Windows::Forms::SplitContainer());

            this->MessageLog = (gcnew System::Windows::Forms::TextBox());
            //....etc

            // MessageLog
            // 
            this->MessageLog->Dock = System::Windows::Forms::DockStyle::Fill;
            this->MessageLog->Location = System::Drawing::Point(0, 0);
            this->MessageLog->Multiline = true;
            this->MessageLog->Name = L"MessageLog";
            this->MessageLog->ReadOnly = true;
            this->MessageLog->Size = System::Drawing::Size(140, 261);
            this->MessageLog->TabIndex = 0;
            this->MessageLog->Text = L"Message Log:";
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(1183, 674);
            //....etc

        }
#pragma endregion
    private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
    {
        UNREFERENCED_PARAMETER(sender);
        UNREFERENCED_PARAMETER(e);
        OpenGL->Render();

    }


};
}
OpenGL.h
#pragma once

#include <windows.h>

#include <iostream>
#include <cstdlib> 
#include "lib.h"
#include <algorithm>
#include <array>
#include <vector>
#include <GL/gl.h>
#include <glut.h>
#include <msclr/marshal_cppstd.h>



using namespace System::Windows::Forms;
using namespace System::Diagnostics;



namespace OpenGLForm
{
    public ref class COpenGL : public System::Windows::Forms::NativeWindow
    {



    public:


        COpenGL(System::Windows::Forms::SplitterPanel^ parentForm, GLsizei iWidth, GLsizei iHeight)
        {
            CreateParams^ cp = gcnew CreateParams;

            cp->X = 5;
            cp->Y = 5;
            cp->Width = parentForm->Height - 5;
            cp->Height = parentForm->Width - 5;


            cp->Parent = parentForm->Handle;

            cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED;

            this->CreateHandle(cp);

            m_hDC = GetDC((HWND)this->Handle.ToPointer());

            if (m_hDC)
            {
                MySetPixelFormat(m_hDC);
            }

        }


        System::Void Render(System::Void)
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

        }


    private:
        HDC m_hDC;
        HGLRC m_hglrc;
        GLfloat rtri;               
        GLfloat rquad;          

    protected:
        ~COpenGL(System::Void)
        {
            this->DestroyHandle();
        }

        GLint MySetPixelFormat(HDC hdc)
        {
            return 1;
        }

        void set_text()
        {
            Form1::MessageLog::Text = "some text"; //---Problem is here, Form1 is not a class or numspace name

        }



    };

    
}

最佳答案

set_text函数需要使用特定的Form 对象,例如

    void set_text()
    {
        mainForm->MessageLog->Text = "some text"; 
    }
为了使此工作有效,mainForm必须是COpenGL类型的System::Windows::Forms::Form^的成员变量,并且该成员应在构造函数中初始化(导致像这样的构造函数调用):
    OpenGL = gcnew COpenGL(this, splitContainer1->Panel2, splitContainer1->Panel2->Width, splitContainer1->Panel2->Height);
或者,不将整个Form对象传递给COpenGL对象,只需传递对消息日志的引用并将其存储在成员变量中即可。遵循以下原则
public ref class COpenGL : public System::Windows::Forms::NativeWindow
{

   System::Windows::Forms::TextBox^ messageLog;

   public:
   
   COpenGL( System::Windows::Forms::TextBox^ messageLog,
       System::Windows::Forms::SplitterPanel^ parentForm, 
       GLsizei iWidth, GLsizei iHeight)
    {
        this->messageLog=messageLog;
        // ...
    }

    void set_text()
    {
       messageLog->Text = "some text"; 
    }
}

关于c++ - 如何从另一个类访问Form组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63084140/

相关文章:

c++ - 为什么在 C++ 中将类分解为多个源文件?

c++ - 解码位图时宽度不正确

.net - 更改表单不透明度而不更改容器控件的不透明度

c# - Windows 窗体卡在多个异步任务上

c# - Winforms 中的 CheckedListbox 奇怪行为

c# - 重新利用现有的 GUI,同时保留大部分相同的功能

c++ - 容器内的Gdb被137终止

c++ - vector< vector<Point3f>> 中的最高值

c++ - OpenCV:如何将图像保存在一个大矩阵中然后分别调用它们?

vb.net - 自动滚动到多行文本框的底部