如何创建继承自form.control类的自定义控件
正如标题所说,我想从控件类创建一个自定义控件。
但它不会显示在窗口窗体应用程序上。
当我将基类从控件更改为用户控件时它起作用。
任何人都可以帮助我,谢谢。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论