自定义控件问题

hello_mcu 2013-08-31 03:12:59
在WINCE下,C#,硬件平台是触控机。
有个状态转换的地方想用指示灯来显示,就类似于组态软件中那种很真实的灯一样,状态一绿色,状态二红色。
VS2005中提供的控件里没有灯或者圆的控件。
如果能在绘图工具里画一个圆,在PS里加上点效果,然后利用这个为原型作为控件外形,再在C#里定义属性。
这样怎么实现。
...全文
136 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello_mcu 2013-09-07
  • 打赏
  • 举报
回复
引用 5 楼 pig357 的回复:

public bool Status
{
get{return _status;}
set{
_status = value;
this.BackgroundImage = _status ? _img1 : _img2;
}
后来我也这么觉得,反正只是个调试用的小程序。
hello_mcu 2013-09-07
  • 打赏
  • 举报
回复
引用 8 楼 hwenycocodq520 的回复:
写了一个简单的例子,楼主看看是否有用。

//自定义指示灯控件
public class BulbControl : Control
{
    private BulbStatus status = BulbStatus.Off;
    //自定义属性可在属性编辑窗口编辑
    [Category("UserDefine Attributes")]
    [Description("BulbControl used to indicate a state!")]
    [Browsable(true)]
    public BulbStatus Status
    {
        get { return status; }
        set
        {
            if (value != status)
            {
                status = value;
                Invalidate();
            }
        }
    }

    public BulbControl()
    {
        SetStyle(
            ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
            ControlStyles.UserPaint, true);
        Width = Height = 30;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        DrawLight(g);
        base.OnPaint(e);
    }
    //画灯,用图片绘制效果更佳
    private void DrawLight(Graphics g)
    {
        using (GraphicsPath path = new GraphicsPath())
        {
            path.AddEllipse(0, 0, Width, Height);
            using (PathGradientBrush brush = new PathGradientBrush(path))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.CompositingQuality = CompositingQuality.HighQuality;
                brush.CenterColor = Color.White;
                brush.CenterPoint = new PointF(Width >> 1, Height >> 1);
                brush.SurroundColors = new Color[] { status == BulbStatus.Off ? Color.Red : Color.Green };
                g.FillPath(brush, path);
            }
            Region = new Region(path);
        }
    }
}
//状态枚举
public enum BulbStatus
{
    On,
    Off
}
效果:
很感谢。有用。
智商余额不足 2013-09-02
  • 打赏
  • 举报
回复
写了一个简单的例子,楼主看看是否有用。

//自定义指示灯控件
public class BulbControl : Control
{
private BulbStatus status = BulbStatus.Off;
//自定义属性可在属性编辑窗口编辑
[Category("UserDefine Attributes")]
[Description("BulbControl used to indicate a state!")]
[Browsable(true)]
public BulbStatus Status
{
get { return status; }
set
{
if (value != status)
{
status = value;
Invalidate();
}
}
}

public BulbControl()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
Width = Height = 30;
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawLight(g);
base.OnPaint(e);
}
//画灯,用图片绘制效果更佳
private void DrawLight(Graphics g)
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(0, 0, Width, Height);
using (PathGradientBrush brush = new PathGradientBrush(path))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
brush.CenterColor = Color.White;
brush.CenterPoint = new PointF(Width >> 1, Height >> 1);
brush.SurroundColors = new Color[] { status == BulbStatus.Off ? Color.Red : Color.Green };
g.FillPath(brush, path);
}
Region = new Region(path);
}
}
}
//状态枚举
public enum BulbStatus
{
On,
Off
}

效果:


BenBenBears 2013-09-02
  • 打赏
  • 举报
回复
也可以尝试一下GDI+图形绘制,不过不是自定义控件了,当然效果还是可以的。
kxyzjm62 2013-09-02
  • 打赏
  • 举报
回复
1、可以按你的想法在自定义控件中放一个图片框,注意要把图拼框的大小设成和空间一样大,并能随控件的大小改变。在设置一个属性,可以是布尔值,根据这个属性改变图片框的图像。 2、自定义个控件,在控件上自己画图,再设置一个属性,可以是布尔值,根据这个属性在控件上画出红、绿灯的图像。
pig357 2013-09-02
  • 打赏
  • 举报
回复

public bool Status
{
get{return _status;}
set{
_status = value;
this.BackgroundImage = _status ? _img1 : _img2;
}
pig357 2013-09-02
  • 打赏
  • 举报
回复
自定义控件,添加两个图轮流做背景图,设置背景就OK了
hello_mcu 2013-09-02
  • 打赏
  • 举报
回复
引用 2 楼 tcmakebest 的回复:
放一个PictureBox,以及两个图,轮流显示
这个跟自定义控件的思路不一样。但算是个方法吧。
tcmakebest 2013-08-31
  • 打赏
  • 举报
回复
放一个PictureBox,以及两个图,轮流显示
wo594261 2013-08-31
  • 打赏
  • 举报
回复
不懂 不过帮你顶下 这东西我也想知道

110,533

社区成员

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

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

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