自定义控件 设计界面

idea_yuye 2013-10-14 03:11:02
自定义控件如何在拖到窗体后,不运行,修改属性就能在设计界面实时显示当前属性对应的样式?我自己自定义的控件不论怎么设置,样式都不发生变化。我想知道要怎么做才能和系统带的控件一样随属性变化而在设计界面实时显示相应样式。

我的自定义控件代码如下,求大神指定下:

#region 属性
private bool IsPress = false;
private Color borderColor = Color.Black;
/// <summary>
/// 边框颜色
/// </summary>
public Color BorderColor
{
get { return borderColor; }
set {
borderColor = value;
Invalidate();
}
}
private int borderSize = 1;
/// <summary>
/// 边框大小
/// </summary>
public int BorderSize
{
get { return borderSize; }
set {
borderSize = value;
Invalidate();
}
}
private int roundCornerSize = 0;
/// <summary>
/// 圆角大小
/// </summary>
public int RoundCornerSize
{
get { return roundCornerSize; }
set {
roundCornerSize = value;
Invalidate();
}
}

private Color impressBackColor = Color.Black;
/// <summary>
/// 点击下颜色
/// </summary>
public Color ImpressBackColor
{
get {
return impressBackColor;
}
set {
impressBackColor = value;
}
}
private Color impressForeColor = Color.Black;
/// <summary>
/// 点击下前景色
/// </summary>
public Color ImpressForeColor
{
get
{
return impressForeColor;
}
set
{
impressForeColor = value;
}
}

#endregion

protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
if (!IsPress)
{
Pen pen = new Pen(BorderColor);
pen.Width = BorderSize;
GraphicsExtension.DrawRoundedRect(g, pen, BackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
//渐变
//GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
}
else
{
Pen pen = new Pen(BorderColor);
pen.Width = BorderSize;
GraphicsExtension.DrawRoundedRect(g, pen, impressBackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
//渐变
//GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, new SolidBrush(this.ImpressForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
}
base.OnPaint(pe);
}

private void ExButton_MouseDown(object sender, MouseEventArgs e)
{
IsPress = true;
Invalidate();
}

private void ExButton_MouseUp(object sender, MouseEventArgs e)
{
IsPress = false;
Invalidate();
}
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
智商余额不足 2013-10-16
  • 打赏
  • 举报
回复
楼主上面的方法不行的?我已经复制你的代码测试过了,没问题的
烙饼 2013-10-16
  • 打赏
  • 举报
回复
g.Dispose(); 用完之后不释放掉,会出现一些意想不到的错误
idea_yuye 2013-10-16
  • 打赏
  • 举报
回复
自己顶下 还有大神有别的看法啊?
智商余额不足 2013-10-14
  • 打赏
  • 举报
回复
引用 4 楼 hwenycocodq520 的回复:
[quote=引用 3 楼 ideayuye 的回复:] [quote=引用 2 楼 hwenycocodq520 的回复:] 属性改变时使控件区域无效 Invalidate(); 把base.OnPaint(pe);放在绘制代码之前,放到后面的话就由系统绘制覆盖你之前绘制的...
放在后面也试了,没变化[/quote] 2楼代码....[/quote]1楼不是2楼~~
智商余额不足 2013-10-14
  • 打赏
  • 举报
回复
引用 3 楼 ideayuye 的回复:
[quote=引用 2 楼 hwenycocodq520 的回复:] 属性改变时使控件区域无效 Invalidate(); 把base.OnPaint(pe);放在绘制代码之前,放到后面的话就由系统绘制覆盖你之前绘制的...
放在后面也试了,没变化[/quote] 2楼代码....
idea_yuye 2013-10-14
  • 打赏
  • 举报
回复
引用 2 楼 hwenycocodq520 的回复:
属性改变时使控件区域无效 Invalidate(); 把base.OnPaint(pe);放在绘制代码之前,放到后面的话就由系统绘制覆盖你之前绘制的...
放在后面也试了,没变化
智商余额不足 2013-10-14
  • 打赏
  • 举报
回复
属性改变时使控件区域无效 Invalidate(); 把base.OnPaint(pe);放在绘制代码之前,放到后面的话就由系统绘制覆盖你之前绘制的...
智商余额不足 2013-10-14
  • 打赏
  • 举报
回复

        public Color ImpressBackColor
        {
            get {
                return impressBackColor; 
            }
            set {
                impressBackColor = value;
                Invalidate();
            }
        }

        public Color ImpressForeColor
        {
            get
            {
                return impressForeColor;
            }
            set
            {
                impressForeColor = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            if (!IsPress)
            {
                Pen pen = new Pen(BorderColor);
                pen.Width = BorderSize;
                GraphicsExtension.DrawRoundedRect(g, pen, BackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
                //渐变
                //GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
            }
            else
            {
                Pen pen = new Pen(BorderColor);
                pen.Width = BorderSize;
                GraphicsExtension.DrawRoundedRect(g, pen, impressBackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
                //渐变
                //GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                g.DrawString(this.Text, this.Font, new SolidBrush(this.ImpressForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
            }
        }

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧