首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

wince+C#上,重载OnPaint方法

2013-01-02 
wince+C#下,重载OnPaint方法想弄一个位图按钮,用PictureBox做,处理单击事件很慢。所以想到从Button类继承,

wince+C#下,重载OnPaint方法
想弄一个位图按钮,用PictureBox做,处理单击事件很慢。
所以想到从Button类继承,发现重载OnPaint无效。
下面这段代码PC上正常的,能看到效果。 

class ImageButton:Button
    {
        private Image image;

        public Image Image
        {
            get
            {
                return image;
            }
            set
            {
                image = value;
            }
        }
             
        protected override void OnPaint(PaintEventArgs pe)
        {
             Graphics g = pe.Graphics;
             Rectangle rect = pe.ClipRectangle;

             if (image != null)
             {
                 g.DrawImage(image, 0, 0, rect, GraphicsUnit.Pixel);
             }
             else
             {
                 base.OnPaint(pe);
             }
        }
    }

[解决办法]
把 Rectangle rect = pe.ClipRectangle  =>    Rectangle rect = this.ClientRectangle.试试
[解决办法]
从Control继承是比较可行的办法,所有的背景、文字等都自绘。
需要处理MouseDown、MouseUp等事件。
我这么做过,没发现4楼说的慢的问题,项目到目前还没有人反映说按钮单击响应慢。
个人认为楼主可以放心的按这个思路做下去。
[解决办法]
顶楼上!!
[解决办法]
引用:
奇怪的是,重载OnPaint为什么不生效呢?
而PC上的win from程序就生效。

有些控件是不会自动调用OnPaint的,之前我只研究过重写TextBox,重载OnPaint后就没有用。
只能通过用WinProc拦截Windows消息,调用Onpaint才行。
PC和WM区别还是挺大的,建议别再弄这个了,耽误时间。
挺麻烦的,建议楼主还是继承Control做吧,并不复杂。
[解决办法]
Button是没有OnPaint重载的   OnBackGroudPaint也没有     根本不支持重绘   
现在流行的做法就是继承Control     如果你觉得太慢   那就想问   你用这个Button不就为了点击一下么
------解决方案--------------------


引用:
大侠你写过从Control继承的位图按钮吗?
没出现单击事件慢的情况吗

在WM上,一般用MouseDown事件代替click,不知道是不是这个原因给你感觉慢的。

[解决办法]
建议继承Control
[解决办法]
关注,帮顶
[解决办法]
试了一下出现个错误。
错误1部署和/或注册失败,错误为: 0x8973190e。 写入文件“%csidl_program_files%\smartdevice\system.drawing.dll”时出错。错误 0x80070070: 磁盘空间不足。
  Device Connectivity Component
[解决办法]
ImageButton继承自Control,CF3.5下不慢:

public partial class ImageButton : Control
    {
        public ImageButton()
        {
        }

        Image backgroundImage;
        bool pressed = false;

        // Property for the background image to be drawn behind the button text.
        public Image BackgroundImage
        {
            get
            {
                return this.backgroundImage;
            }
            set
            {
                this.backgroundImage = value;
            }
        }

        // When the mouse button is pressed, set the "pressed" flag to true 
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
        // sets the mouse capture automatically.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.pressed = true;
            this.Invalidate();
            base.OnMouseDown(e);
        }

        // When the mouse is released, reset the "pressed" flag 
        // and invalidate to redraw the button in the unpressed state.
        protected override void OnMouseUp(MouseEventArgs e)
        {


            this.pressed = false;
            this.Invalidate();
            base.OnMouseUp(e);
        }

        // Override the OnPaint method to draw the background image and the text.
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle);

            if (this.backgroundImage != null)
            {
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);

                if (this.pressed)
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
                else
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);

            }

            base.OnPaint(e);
        }
    }


[解决办法]
是呀,我做的时候,也是用MouseDown和MouseUp来实现Click事件的。
[解决办法]
散个分哈
[解决办法]
wince+C#上,重载OnPaint方法

namespace MyPocketPc
{
    public partial class MyImageButton:Control
    {
        public MyImageButton()
        {
 
        }
        private Image _image;

        public Image Image
        {
            get { return _image; }
            set { _image = value; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {


            if (this._image != null)
            {
                e.Graphics.DrawImage(this._image, 0, 0, e.ClipRectangle, GraphicsUnit.Pixel);
            }
            base.OnPaint(e);
        }
    }
}


[解决办法]
 哇  学过一点点  也可以说是一窍不通哈
[解决办法]
Mark!
[解决办法]
引用:
to sohighthesky:
我也是这样写的,连续点击(时间很短)还是慢。

另外我的是WINCE平台 。

我是WinCE5.0没发现你说问题
[解决办法]
mark
[解决办法]
mark
[解决办法]
顶一下。

[解决办法]
顶一下。 
[解决办法]
路过。
[解决办法]
顶顶
[解决办法]
学习一下哈
[解决办法]
引用:
ImageButton继承自Control,CF3.5下不慢:
C# codepublicpartialclass ImageButton : Control
    {public ImageButton()
        {
        }

        Image backgroundImage;bool pressed=false;// Property for the background image to be drawn behind the button text.public Image BackgroundImage
        {get
            {returnthis.backgroundImage;
            }set
            {this.backgroundImage= value;
            }
        }// When the mouse button is pressed, set the "pressed" flag to true// and invalidate the form to cause a repaint.  The .NET Compact Framework// sets the mouse capture automatically.protectedoverridevoid OnMouseDown(MouseEventArgs e)
        {this.pressed=true;this.Invalidate();base.OnMouseDown(e);
        }// When the mouse is released, reset the "pressed" flag// and invalidate to redraw the button in the unpressed state.protectedoverridevoid OnMouseUp(MouseEventArgs e)
        {this.pressed=false;this.Invalidate();base.OnMouseUp(e);


        }// Override the OnPaint method to draw the background image and the text.protectedoverridevoid OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle);if (this.backgroundImage!=null)
            {
                ImageAttributes attr=new ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);if (this.pressed)
                    e.Graphics.DrawImage(this.backgroundImage,this.ClientRectangle,0,0,this.backgroundImage.Width,this.backgroundImage.Height, GraphicsUnit.Pixel, attr);else
                    e.Graphics.DrawImage(this.backgroundImage,this.ClientRectangle,0,0,this.backgroundImage.Width,this.backgroundImage.Height, GraphicsUnit.Pixel, attr);

            }base.OnPaint(e);
        }
    }


请问一下,我通过继承Control来实现图片按钮,怎么才能响应GotFocus事件?采用button可以响应,可通过以上方法继承之后按TAB键就没反映了。

热点排行