Image Button Control

Monday, June 8, 2009 | |

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)
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)
End If
End If
End Sub
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
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)
Me.ImageList.Draw(g, Point.Round(pt), Me.ImageIndex)
End If
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_ITEM = &H10000
End Enum

Public Enum CustomDrawItemState
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:


Post a Comment


Privacy Policy for

If you require any more information or have any questions about our privacy policy, please feel free to contact us by email at

At, the privacy of our visitors is of extreme importance to us. This privacy policy document outlines the types of personal information is received and collected by and how it is used.

Log Files Like many other Web sites, makes use of log files. The information inside the log files includes internet protocol ( IP ) addresses, type of browser, Internet Service Provider ( ISP ), date/time stamp, referring/exit pages, and number of clicks to analyze trends, administer the site, track user’s movement around the site, and gather demographic information. IP addresses, and other such information are not linked to any information that is personally identifiable.

Cookies and Web Beacons does not use cookies.

DoubleClick DART Cookie

.:: Google, as a third party vendor, uses cookies to serve ads on .:: Google's use of the DART cookie enables it to serve ads to your users based on their visit to and other sites on the Internet. .:: Users may opt out of the use of the DART cookie by visiting the Google ad and content network privacy policy at the following URL -

Some of our advertising partners may use cookies and web beacons on our site. Our advertising partners include ....... Google Adsense

These third-party ad servers or ad networks use technology to the advertisements and links that appear on send directly to your browsers. They automatically receive your IP address when this occurs. Other technologies ( such as cookies, JavaScript, or Web Beacons ) may also be used by the third-party ad networks to measure the effectiveness of their advertisements and / or to personalize the advertising content that you see. has no access to or control over these cookies that are used by third-party advertisers.

You should consult the respective privacy policies of these third-party ad servers for more detailed information on their practices as well as for instructions about how to opt-out of certain practices.'s privacy policy does not apply to, and we cannot control the activities of, such other advertisers or web sites.

If you wish to disable cookies, you may do so through your individual browser options. More detailed information about cookie management with specific web browsers can be found at the browsers' respective websites.

Nunag Free and Premium Web Directory