locked
How to make a bitmap "button" active ? RRS feed

  • Question

  • Hi

    I have a bitmap displayed on the screen.
    Could someone share with code examples on how to make this bitmap "button" active ?
    This is a non-MFC button.
    I have created a button window for this bitmap.

    Any help is appreciated.

    Thanks

    Saturday, May 2, 2009 4:04 AM

Answers

  • The theory is same with raw win32 and .net.

    You can change  bitmap and redraw it when your program received a message such as lbuttondown and lbuttonup of your button.

    Also, you can create a thread to alternate  bitmap periodly for your button .

    Hope this may be help..
    • Marked as answer by warrentang Friday, May 8, 2009 2:41 AM
    Wednesday, May 6, 2009 10:12 AM

All replies

  • Hi chz013,


    In .net CF you have to change bitmap when key down or mouse down event occur, OnPaint Event.



    Hope this may be help..



    Pavan Pareta | If you find my post is helpful for you then mark as a answer.
    • Proposed as answer by WmDev Saturday, May 2, 2009 7:55 PM
    Saturday, May 2, 2009 7:55 PM
  • try it
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    
    Public Class PictureButtonDemo
        Inherits System.Windows.Forms.Form
        
        Friend WithEvents PictureButton1 As New PictureButton
    
        Public Sub New()
    
            ' Display the OK close button.
            Me.MinimizeBox = False
            Me.Text = "Picture Button Demo"
            
            ' Create an instance of the PictureButton custom control.
            With PictureButton1
                .Parent = Me
                .Bounds = New Rectangle(10, 30, 150, 30)
                .ForeColor = Color.White
                .BackgroundImageValue = MakeBitmap(Color.Blue, _
                    PictureButton1.Width, PictureButton1.Height)
                .PressedImageValue = MakeBitmap(Color.LightBlue, _
                    PictureButton1.Width, PictureButton1.Height)
                .Text = "Click Me"
            End With
        End Sub
    
        ' Clean up any resources being used.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub
    
        ' Create a bitmap object and fill it with the specified color.   
        ' To make it look like a custom image, draw an ellipse in it.
        Function MakeBitmap(ByVal ButtonColor As Color, ByVal width As Integer, _
            ByVal height As Integer) As Bitmap
    
            Dim bmp As New Bitmap(width, height)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.FillRectangle(New SolidBrush(ButtonColor), 0, 0, bmp.Width, bmp.Height)
            g.DrawEllipse(New Pen(Color.LightGray), 3, 3, width - 6, height - 6)
            g.Dispose()
    
            Return bmp
        End Function
    
        Shared Sub Main()
            Application.Run(New PictureButtonDemo)
        End Sub
    
        ' Because PictureButton inherits from Control, 
        ' you can use the default Click event.
        Private Sub PictureButton1_Click(ByVal sender As Object, ByVal e As EventArgs) _
            Handles PictureButton1.Click
    
            'Add code here to respond to button click. 
       
        End Sub
    
    End Class
    
    'Button with an image custom control.
    Public Class PictureButton
        Inherits Control
    
        Private backgroundImg As Image
        Private pressedImg As Image
        Private pressed As Boolean = False
    
        ' 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
            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
            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, 0, 0)
            Else
                e.Graphics.DrawImage(Me.backgroundImg, 0, 0)
            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
    End Class

    • Proposed as answer by 試用版已過期 Monday, May 4, 2009 1:01 AM
    • Marked as answer by warrentang Friday, May 8, 2009 2:40 AM
    • Unmarked as answer by warrentang Friday, May 8, 2009 2:41 AM
    Sunday, May 3, 2009 1:38 AM
  • Hi KYLIN, WmDev

    Thanks for the response.
    Unfortunately I am not using .NET for my development.
    Raw Win32 is what help I need here.

    Thanks
    • Marked as answer by warrentang Friday, May 8, 2009 2:40 AM
    • Unmarked as answer by warrentang Friday, May 8, 2009 2:40 AM
    Tuesday, May 5, 2009 8:38 PM
  • The theory is same with raw win32 and .net.

    You can change  bitmap and redraw it when your program received a message such as lbuttondown and lbuttonup of your button.

    Also, you can create a thread to alternate  bitmap periodly for your button .

    Hope this may be help..
    • Marked as answer by warrentang Friday, May 8, 2009 2:41 AM
    Wednesday, May 6, 2009 10:12 AM