|
Hi all,
I need to pu an image in a button but my application runs in Windows CE so I'm using the Compact Framework and in this context the object Button has no property like Image , BackgroundImage , Bitmap or something similar.
Anybody knows how I can do?
Thanks to all.
|
|
|
|
|
steve_9496613 wrote: Anybody knows how I can do?
Try MSDN[^]
|
|
|
|
|
Well... I can't say that I have understood everything in the MSDN example but I managed to convert it to Visual Studio .NET 2008 and to make it work!!!!
Yes, it would be easier something like Button.BkImage("image.bmp") but ... it seems that the Compact Framework is missing many things that I need!!!
Once again, Thank you Eddy!!
I publish the code of MyImageButton class if it can help someone or anyone has any corrections to suggest.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Imaging
Public Class MyImageButton
Inherits Control
Private image As Image
Private FirstTime As Boolean = True
Private bPushed As Boolean
Private m_bmpOffscreen As Bitmap
Sub New()
bPushed = False
Me.Size = New Size(21, 21)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim gxOff As Graphics
Dim imgRect As Rectangle
Dim backBrush As Brush
If (FirstTime) Then
m_bmpOffscreen = New Bitmap(ClientSize.Width, ClientSize.Height)
End If
gxOff = Graphics.FromImage(m_bmpOffscreen)
gxOff.Clear(Me.BackColor)
If (Not bPushed) Then
backBrush = New SolidBrush(Parent.BackColor)
Else
backBrush = New SolidBrush(Color.LightGray)
End If
gxOff.FillRectangle(backBrush, Me.ClientRectangle)
If (Not image.Equals(DBNull.Value)) Then
Dim imageLeft As Int32 = (Me.Width - image.Width) / 2
Dim imageTop As Int32 = (Me.Height - image.Height) / 2
If (Not bPushed) Then
imgRect = New Rectangle(imageLeft, imageTop, image.Width, image.Height)
Else
imgRect = New Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height)
End If
Dim imageAttr As ImageAttributes = New ImageAttributes()
imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image))
gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr)
End If
If (bPushed) Then
Dim rc As Rectangle = Me.ClientRectangle
rc.Width -= 1
rc.Height -= 1
gxOff.DrawRectangle(New Pen(Color.Black), rc)
End If
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0)
MyBase.OnPaint(e)
End Sub
Private Function BackgroundImageColor(ByVal image As Image) As Color
Dim bmp As Bitmap = New Bitmap(image)
Return bmp.GetPixel(0, 0)
End Function
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(e)
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
bPushed = True
Me.Invalidate()
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
bPushed = False
Me.Invalidate()
MyBase.OnMouseUp(e)
End Sub
Public Property MImage() As Image
Get
Return image
End Get
Set(ByVal value As Image)
image = value
End Set
End Property
End Class
|
|
|
|
|
You're welcome
steve_9496613 wrote: I publish the code of MyImageButton class if it can help someone or anyone has any corrections to suggest.
It might get buried under new messages on the forum here; it'd be more visible if you post it as a "Tip[^]".
|
|
|
|
|
You can use Label instead button and lay text over image.
|
|
|
|
|
Thank you halabella for your answer.
It can be a possibility but in this way I think you lose the behavior of the button (button pressed - button not pressed).
In my particular case I could just use an image because the text I need to write is a symbol of a font type not included in my Windows CE (for this reason I need to put an image in a button) but there would be much difference aesthetically with the other buttons of the form.
...but perhaps I could change the image on the click event... yes, it is a possibility.
Thanks
|
|
|
|
|
Hi steve,
You can use ImageButton control. An attribute called ImageUrl is where you specify the path to your image.
Hope that have you. Happy coding!
|
|
|
|
|
Hi s3275049,
thank you for your replay but unfortunately MSDN says that the ImageButton class is not supported in the Windows CE platform.
|
|
|
|