Winform自定义控件基础(一)

1.设置图像和文字以抗锯齿的方式呈现

1     g.SmoothingMode = SmoothingMode.AntiAlias;
2     g.TextRenderingHint = TextRenderingHint.AntiAlias;

2.指定区域绘图(常见于OnPaint函数中:g.DrawImage(...)

1     // 参数: 
2     //   image:
3     //     要绘制的 System.Drawing.Image。
4     //
5     //   rect:
6     //     System.Drawing.Rectangle 结构,它指定所绘制图像的位置和大小。
7     public void DrawImage(Image image, Rectangle rect);

 1    // 参数: 
 2    //   image:
 3    //     要绘制的 System.Drawing.Image。
 4    //   destRect:
 5    //     System.Drawing.Rectangle 结构,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
 6    //   srcRect:
 7    //     System.Drawing.Rectangle 结构,它指定 image 对象中要绘制的部分。
 8    //   srcUnit:
 9    //     System.Drawing.GraphicsUnit 枚举的成员,它指定 srcRect 参数所用的度量单位。
10    public void DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit);

3.指定区域绘制文本:

1     TextFormatFlags flags = TextFormatFlags.VerticalCenter |
2                             TextFormatFlags.Left |
3                             TextFormatFlags.SingleLine;
4     TextRenderer.DrawText(g, this.Text, this.Font, this.TextRect, foreColor, flags);

或者

1     g.DrawString(....)

4.文本测量

1     //测量文本占用空间大小    
2     g.MeasureString(this.Text, this.Font);

5.程序效果只在运行时显示

1     if (!this.DesignMode)
2     {
3         //代码
4     }

6.更新控件的显示

1     //更新 ***Rect 区域的显示效果
2     this.Invalidate(this.***Rect);

 更新整个控件:

1     //更新整个控件
2     this.Invalidate();

 7.修改控件的布局:

1     this.pnlMain.SuspendLayout();
2     /*
3     添加控件,
4     设置控件的Position、Size、Name等属性
5     */
6     this.pnlMain.ResumeLayout(false);
7     this.pnlMain.PerformLayout();

8.属性

  • Category("****"):将该属性进行分类,组名为“****”;
  • Description("****"):对属性进行描述,描述信息为“****”;
  • Browsable(***):属性是否可见,是否可以在视图面板可见
原文地址:https://www.cnblogs.com/imstrive/p/6088374.html