Pages

Search

Monday, June 8, 2009

Image Button Control

This is an alternative button control, that can handle images even its flatstyle is set to System, this control is for VB.Net 2003 users.

To use this code, create new project and select windows controls libary, and add new control and name it Imagebutton, just copy this code and paste. Thats all.

Imports System.Runtime.InteropServices
Public Class ImageButton
Inherits System.Windows.Forms.Button
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = (NativeMethods.WM_REFLECT Or NativeMethods.WM_NOTIFY) Then
Dim CustomDrawHeader As NativeMethods.NMCUSTOMDRAW = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(NativeMethods.NMCUSTOMDRAW)), NativeMethods.NMCUSTOMDRAW)
If CustomDrawHeader.dwDrawStage = NativeMethods.CustomDrawDrawStage.CDDS_PREPAINT Then
Dim g As Graphics = Graphics.FromHdc(CustomDrawHeader.hdc)
Dim pe As New PaintEventArgs(g, Me.Bounds)
OnPaint(pe)
pe.Dispose()
g.Dispose()
End If
End If
End Sub
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
DrawImage(pevent.Graphics)
End Sub
Protected Overridable Sub DrawImage(ByVal g As Graphics)
If Me.Image Is Nothing Then Return

Dim pt As PointF

Select Case Me.ImageAlign
Case ContentAlignment.TopLeft
pt.X = 5
pt.Y = 5
Case ContentAlignment.TopCenter
pt.X = Convert.ToSingle(Width - Me.Image.Width) / 2
pt.Y = 5
Case ContentAlignment.TopRight
pt.X = Width - Me.Image.Width - 5
pt.Y = 5
Case ContentAlignment.MiddleLeft
pt.X = 5
pt.Y = Convert.ToSingle(Height - Me.Image.Height) / 2
Case ContentAlignment.MiddleCenter
pt.X = Convert.ToSingle(Width - Me.Image.Width) / 2
pt.Y = Convert.ToSingle(Height - Me.Image.Height) / 2
Case ContentAlignment.MiddleRight
pt.X = Width - Me.Image.Width - 5
pt.Y = Convert.ToSingle(Height - Me.Image.Height) / 2
Case ContentAlignment.BottomLeft
pt.X = 5
pt.Y = Height - Me.Image.Height - 5
Case ContentAlignment.BottomCenter
pt.X = Convert.ToSingle(Width - Me.Image.Width) / 2
pt.Y = Height - Me.Image.Height - 5
Case Else
pt.X = Width - Me.Image.Width - 5
pt.Y = Height - Me.Image.Height - 5
End Select

If Me.Enabled Then
If Me.ImageList Is Nothing Then
g.DrawImage(Me.Image, pt.X, pt.Y, Me.Image.Width, Me.Image.Height)
Else
Me.ImageList.Draw(g, Point.Round(pt), Me.ImageIndex)
End If
Else
Dim p As Point = Point.Round(pt)
ControlPaint.DrawImageDisabled(g, Me.Image, p.X, p.Y, Me.BackColor)
End If

End Sub

End Class

Friend Class NativeMethods
Private Sub New()
'Uninstantiable Class
End Sub

Public Const WM_USER As Int32 = &H400&
Public Const WM_NOTIFY As Int32 = &H4E&
Public Const WM_REFLECT As Int32 = WM_USER + &H1C00&
Public Const NM_FIRST As Int32 = 0
Public Const NM_CUSTOMDRAW As Int32 = NM_FIRST Or -12

Public Enum CustomDrawDrawStage
CDDS_PREPAINT = &H1
CDDS_POSTPAINT = &H2
CDDS_PREERASE = &H3
CDDS_POSTERASE = &H4
CDDS_ITEM = &H10000
CDDS_ITEMPREPAINT = (CDDS_ITEM Or CDDS_PREPAINT)
CDDS_ITEMPOSTPAINT = (CDDS_ITEM Or CDDS_POSTPAINT)
CDDS_ITEMPREERASE = (CDDS_ITEM Or CDDS_PREERASE)
CDDS_ITEMPOSTERASE = (CDDS_ITEM Or CDDS_POSTERASE)
CDDS_SUBITEM = &H20000
End Enum

Public Enum CustomDrawItemState
CDIS_SELECTED = &H1
CDIS_GRAYED = &H2
CDIS_DISABLED = &H4
CDIS_CHECKED = &H8
CDIS_FOCUS = &H10
CDIS_DEFAULT = &H20
CDIS_HOT = &H40
CDIS_MARKED = &H80
CDIS_INDETERMINATE = &H100
CDIS_SHOWKEYBOARDCUES = &H200
End Enum

_
Public Structure NMHDR
Public HWND As Int32
Public idFrom As Int32
Public code As Int32
Public Overloads Function ToString() As String
Return String.Format("Hwnd: {0}, ControlID: {1}, Code: {2}", HWND, idFrom, code)
End Function
End Structure
_
Public Structure NMCUSTOMDRAW
Public hdr As NMHDR
Public dwDrawStage As CustomDrawDrawStage
Public hdc As IntPtr
Public rc As RECT
Public dwItemSpec As IntPtr
Public uItemState As CustomDrawItemState
Public lItemlParam As IntPtr
End Structure
_
Public Structure RECT
Public left, top, right, bottom As Int32
End Structure
End Class


I got this in some websites, Im sorry I forget the site, If your the original owner of the code please pm me:

No comments:

Post a Comment