欢迎来到老马的领地~ 这是“压风式散热底座”发明者的个人网站:) 本人QQ:80524554,用户群1:562279766
工作需要,在WINCE下写一个程序,但需要图片按钮,VB.NET自带的按钮没有此功能,需要自己写一个自定义控件.

控件本身是直接从MSDN里抄来的,简单几句代码:


Imports System
Imports System.Drawing
Imports System.Windows.Forms

'Button with an image custom control.
Public Class PictureButton
    Inherits UserControl

    Private backgroundImg As Image
    Private pressedImg As Image
    Private pressed As Boolean = False
    Private destRT As Rectangle                             '按钮区域
    Private srcRTBK As Rectangle, srcRTPS As Rectangle      '按下与背景图区域

    ' Property for the background image to be drawn behind the button text.
    Public Property BackgroundImageValue() As Image
        Get
            Return Me.backgroundImg
        End Get
        Set(ByVal Value As Image)
            Me.backgroundImg = Value
            srcRTBK = New Rectangle(0, 0, Me.backgroundImg.Width, Me.backgroundImg.Height)
        End Set
    End Property

    ' Property for the background image to be drawn behind the button text when
    ' the button is pressed.
    Public Property PressedImageValue() As Image
        Get
            Return Me.pressedImg
        End Get
        Set(ByVal Value As Image)
            Me.pressedImg = Value
            srcRTPS = New Rectangle(0, 0, Me.pressedImg.Width, Me.pressedImg.Height)
        End Set
    End Property

    ' When the mouse button is pressed, set the "pressed" flag to true
    ' and ivalidate the form to cause a repaint.  The .NET Compact Framework
    ' sets the mouse capture automatically.
    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        Me.pressed = True
        Me.Invalidate()
        MyBase.OnMouseDown(e)
    End Sub

    ' When the mouse is released, reset the "pressed" flag
    ' and invalidate to redraw the button in the unpressed state.
    Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
        Me.pressed = False
        Me.Invalidate()
        MyBase.OnMouseUp(e)
    End Sub

    ' Override the OnPaint method to draw the background image and the text.
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        If Me.pressed AndAlso (Me.pressedImg IsNot Nothing) Then
            e.Graphics.DrawImage(Me.pressedImg, destRT, srcRTPS, GraphicsUnit.Pixel)
        Else
            e.Graphics.DrawImage(Me.backgroundImg, destRT, srcRTBK, GraphicsUnit.Pixel)
        End If

        ' Draw the text if there is any.
        If Me.Text.Length > 0 Then
            Dim size As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font)

            ' Center the text inside the client area of the PictureButton.
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), _
                (Me.ClientSize.Width - size.Width) / 2, _
                (Me.ClientSize.Height - size.Height) / 2)
        End If

        ' Draw a border around the outside of the   
        ' control to look like Pocket PC buttons.
        e.Graphics.DrawRectangle(New Pen(Color.Black), 0, 0, _
            Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)

        MyBase.OnPaint(e)
    End Sub

    Private Sub PictureButton_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        destRT = New Rectangle(0, 0, Me.Width, Me.Height)
    End Sub
End Class

(出处:  http://msdn.microsoft.com/zh-cn/library/ms172532(v=vs.90).aspx)

用起来功能什么的都正确,但是-----------到了WINCE实机上一跑,点击的时候闪得厉害.

分析了一下闪的原因,应该是绘制新的图像前清除了原来的图像,但清除后的颜色与要显示的颜色反差太大,于是就有闪一下的感觉.

要解决它,参照叶帆的方案就是禁止绘制背景,也就是不清除原来的图像,直接画新的上去盖住,这样反差就不大了.

(叶帆的原文: http://yfsoft.blog.51cto.com/1635641/323439)

原文不是VB.NET,但这不重要,重要的是有思路了.

于是在上面代码中加入这么一段:


    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaintBackground(e)        '禁止绘制背景
    End Sub


再试,不闪了,搞定!lol.gif

感谢叶帆提供的思路!
添加评论

昵称 *

E-mail