我必须为富文本框提供选择支持,因此我创建了单独的控件并尝试使用paint()
方法绘制它。我已经完成了,但是一旦完成绘图,我就无法编辑rtbox
。请找到我使用的以下代码并分享您的想法。
注意:我怀疑由于将selectioncontrol
停靠方式设置为Fill
而导致问题,但如果我删除或更改了未选择的选择。
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private GraphicCellControl graphiccell;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
#region picturebox
this.BackColor = Color.Aquamarine;
var selectBtn = new Button();
selectBtn.Size = new Size(100, 30);
selectBtn.Location = new Point(10, 10);
selectBtn.Text = "Click";
selectBtn.Click += selectBtn_Click;
//var picturebox = new PictureBox();
//picturebox.Size = new Size(140, 110);
//picturebox.Location = new Point(4, 4);
//picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
//picturebox.Image = new Bitmap(@"..\..\Data\picture.png");
var richtextbox = new RichTextBox();
richtextbox.Size = new Size(140, 110);
richtextbox.Location = new Point(4, 4);
richtextbox.Text = "Texting information";
graphiccell = new GraphicCellControl();
graphiccell.Location = new Point(50, 200);
graphiccell.Size = new Size(150, 120);
graphiccell.Controls.Add(richtextbox);
this.Controls.Add(graphiccell);
this.Controls.Add(selectBtn);
#endregion
}
void selectBtn_Click(object sender, EventArgs e)
{
graphiccell.IsSelected = !graphiccell.IsSelected;
}
#endregion
}
public class GraphicCellControl : Control
{
private SelectionControl selectionControl;
public GraphicCellControl()
{
selectionControl = new SelectionControl();
}
private bool isselected;
public bool IsSelected
{
get { return isselected; }
set
{
isselected = value;
if (isselected && !this.Controls.Contains(selectionControl))
{
this.Controls.Add(selectionControl);
selectionControl.BringToFront();
}
else if (!isselected && this.Controls.Contains(selectionControl))
this.Controls.Remove(selectionControl);
}
}
}
public class SelectionControl : Control
{
public SelectionControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint | ControlStyles.Opaque |
ControlStyles.SupportsTransparentBackColor, true);
Dock = DockStyle.Fill;
// Anchor = AnchorStyles.Left | AnchorStyles.Top;
BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(new Pen(Color.Gray, 1), 4, 4, this.Size.Width - 10, this.Size.Height - 10);
e.Graphics.FillEllipse(Brushes.White, 0, 0, 8, 8);
e.Graphics.FillEllipse(Brushes.White, 0, (this.Size.Height - 10) / 2, 8, 8);
e.Graphics.FillEllipse(Brushes.White, 0, this.Size.Height - 10, 8, 8);
e.Graphics.FillEllipse(Brushes.White, (this.Size.Width - 10) / 2, 0, 8, 8);
e.Graphics.FillEllipse(Brushes.White, (this.Size.Width - 10) / 2, this.Size.Height - 10, 8, 8);
e.Graphics.FillEllipse(Brushes.White, this.Size.Width - 10, 0, 8, 8);
e.Graphics.FillEllipse(Brushes.White, this.Size.Width - 10, (this.Size.Height - 10) / 2, 8, 8);
e.Graphics.FillEllipse(Brushes.White, this.Size.Width - 10, this.Size.Height - 10, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), 0, 0, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), 0, (this.Size.Height - 10) / 2, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), 0, this.Size.Height - 10, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), (this.Size.Width - 10) / 2, 0, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), (this.Size.Width - 10) / 2, this.Size.Height - 10, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), this.Size.Width - 10, 0, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), this.Size.Width - 10, (this.Size.Height - 10) / 2, 8, 8);
e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 2), this.Size.Width - 10, this.Size.Height - 10, 8, 8);
}
}