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)
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:

0 comments:

Post a Comment

 

Privacy Policy for programmer-brienne.blogspot.com

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

At programmer-brienne.blogspot.com, 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 programmer-brienne.blogspot.com and how it is used.

Log Files Like many other Web sites, programmer-brienne.blogspot.com 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 programmer-brienne.blogspot.com does not use cookies.

DoubleClick DART Cookie

.:: Google, as a third party vendor, uses cookies to serve ads on programmer-brienne.blogspot.com. .:: Google's use of the DART cookie enables it to serve ads to your users based on their visit to programmer-brienne.blogspot.com 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 - http://www.google.com/privacy_ads.html

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 programmer-brienne.blogspot.com 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.

programmer-brienne.blogspot.com 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. programmer-brienne.blogspot.com'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