如何创建从form.control类继承的自定义控件

时间:2011-10-28 03:21:10

标签: visual-c++ c++-cli

如标题所述,我想从控件类创建一个自定义控件 但它不会显示在窗体申请表上 当我将基类从控件更改为usercontrol时,它可以工作。

任何人都可以帮助我,谢谢。

#pragma once

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace cc {
    public ref class Class1 : public System::Windows::Forms::Control
    {
        // TODO: Add your methods for this class here.

    public:
        Class1()
        {
            this->SetStyle(ControlStyles::ResizeRedraw, true);
            this->SetStyle(ControlStyles::UserPaint, true);
            this->SetStyle(ControlStyles::AllPaintingInWmPaint, true);
        }


    protected:
        virtual void OnPaint(  PaintEventArgs^ e ) override
        {
            __super::OnPaint(e);
            System::Drawing::Rectangle rcRect = this->ClientRectangle;

            // Create a local version of the graphics object for the PictureBox.
            Graphics^ g = e->Graphics;

            // Draw a string on the PictureBox.
            g->DrawString("This is a diagonal line drawn on the control",
                gcnew System::Drawing::Font("Arial",10), System::Drawing::Brushes::Blue, Point(30,30));
            // Draw a line in the PictureBox.
            g->DrawLine(System::Drawing::Pens::Red, this->Left, this->Top,
                this->Right, this->Bottom);
        }
    };
}

0 个答案:

没有答案