在c ++ / cli中创建自定义组件

时间:2015-09-22 15:03:02

标签: .net visual-studio user-interface c++-cli

我想创建自己的按钮,可以是模板,因为我想在我的应用程序中创建几个按钮。除了我的模板中的按钮属性,我想要有事件。

我创建了组件类:

#pragma once

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


namespace myApp {

    public ref class GoButton :  public System::Windows::Forms::Button
    {
    public:
        GoButton(int x, int y)
        {
            InitializeComponent(x, y);
        }

    protected:
        ~GoButton()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button;
    protected:

    protected:

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(int x, int y)
        {
            this->button = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button
            // 
            this->button->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
            this->button->Location = System::Drawing::Point(x, y);
            this->button->Name = L"button";
            this->button->Size = System::Drawing::Size(169, 44);
            this->button->TabIndex = 0;
            this->button->Text = L"button";
            this->button->UseVisualStyleBackColor = true;
            this->button->MouseLeave += gcnew System::EventHandler(this, &GoButton::button_MouseLeave);
            this->button->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &GoButton::button_MouseMove);
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void button_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
    {
        this->BackgroundImage = Drawing::Image::FromFile("b2.png");
    }
    private: System::Void button_MouseLeave(System::Object^  sender, System::EventArgs^  e) 
    {
        this->BackgroundImage = Drawing::Image::FromFile("b.png");
    }
    };
}

之后,我创建主窗口并尝试添加此按钮: 头:

#pragma once

#include "GoButton.h"

namespace myApp {

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

    public ref class MainWindow : public System::Windows::Forms::Form
    {
    public:
        MainWindow(void);

    protected:
        ~MainWindow();

    protected:
    private: System::Windows::Forms::Button^  button1;

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void);
#pragma endregion

    };
}

CPP:

#include "MainWindow.h"

namespace myApp
{
    MainWindow::MainWindow(void)
    {
        InitializeComponent();
    }

    MainWindow::~MainWindow()
    {
        if (components)
        {
            delete components;
        }
    }

    void MainWindow::InitializeComponent(void)
    {
        //this->button1 = (gcnew System::Windows::Forms::Button());
        this->button1 = (gcnew GoButton(-1, 50));
        this->SuspendLayout();

        // MainWindow
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->BackColor = System::Drawing::Color::White;
        this->ClientSize = System::Drawing::Size(546, 405);
        this->Location = System::Drawing::Point(37, 162);
        this->Name = L"MainWindow";
        this->Text = L"MainWindow";
    this->Controls->Add(button1);
        this->ResumeLayout(false);

    }

}

该按钮不会显示我在GoButton类中设置的属性,我该怎么做我想要的?

在表单构建器中,当我尝试打开mainwindow时,我有一个错误:无法找到类型GoButton

0 个答案:

没有答案