绘制焦点选择框
最近要为一个项目准备一个自定义控件,需要了解更多绘图的一些知识,幸好CodeProject有这么一篇文章DrawTools,为我提供了相当多的范例代码去学习。
学习的第一件事的绘制焦点选择框。
原以为像绘制一个方框去绘制,不想GDI有专门的方法供使用
以下是责任绘制工作的UserControl的部分代码
#region 组件设计器生成的代码
/**/
private void InitializeComponent()
![](https://www.cnblogs.com/Images/dot.gif)
#endregion
![](/Images/OutliningIndicators/None.gif)
private Point startPoint;
private Rectangle focusRectangle;
private bool bDrawFocusRectangle;
![](/Images/OutliningIndicators/None.gif)
public void Init(bool isFill)
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
if(isFill)
this.Dock = DockStyle.Fill;
}
![](/Images/OutliningIndicators/None.gif)
/// <summary>
/// 获取绘制的方框。
/// 我们选择时,可能是由左到右,由上到下,也有可能由下到上,由右到左。
/// 为些程序需要判断一下。
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)
{
if ( x2 < x1 )
{
int tmp = x2;
x2 = x1;
x1 = tmp;
}
![](/Images/OutliningIndicators/InBlock.gif)
if ( y2 < y1 )
{
int tmp = y2;
y2 = y1;
y1 = tmp;
}
![](/Images/OutliningIndicators/InBlock.gif)
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
![](/Images/OutliningIndicators/None.gif)
private void DrawArea_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
this.bDrawFocusRectangle = true;
this.focusRectangle = DrawArea.GetNormalizedRectangle(this.startPoint.X,this.startPoint.Y,e.X,e.Y);
this.Refresh();//重新绘制
}
}
![](/Images/OutliningIndicators/None.gif)
private void DrawArea_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.startPoint = new Point(e.X,e.Y);
}
![](/Images/OutliningIndicators/None.gif)
private void DrawArea_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(this.bDrawFocusRectangle)
ControlPaint.DrawFocusRectangle(e.Graphics, this.focusRectangle, Color.Black, Color.Transparent);
}
![](/Images/OutliningIndicators/None.gif)
private void DrawArea_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.bDrawFocusRectangle = false;
this.Refresh();
}
在Form中使用该控件
private void Form1_Load(object sender, System.EventArgs e)
{
this.drawArea1.Init(true);
}
学习的第一件事的绘制焦点选择框。
原以为像绘制一个方框去绘制,不想GDI有专门的方法供使用
ControlPaint.DrawFocusRectangle(e.Graphics, this.focusRectangle, Color.Black, Color.Transparent);
以下是责任绘制工作的UserControl的部分代码
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
在Form中使用该控件
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)