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

发布于 2024-12-12 11:54:21 字数 1520 浏览 0 评论 0原文

正如标题所说,我想从控件类创建一个自定义控件。
但它不会显示在窗口窗体应用程序上。
当我将基类从控件更改为用户控件时它起作用。

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

#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);
        }
    };
}

as title said, i want to create a custom control from control class.
but it doesn't display on the window form application.
it works when i change the base class from control to usercontrol.

anyone can help me, thanks.

#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);
        }
    };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文