|
|
Title | Give PictureBoxes different border styles (raised, sunken, etc.) in VB .NET |
Description | This example shows how to give PictureBoxes different border styles (raised, sunken, etc.) in VB .NET. |
Keywords | PictureBox, border style, raised, sunken, VB.NET |
Categories | API, Graphics, Controls |
|
|
Thanks to Dipak Auddy for the original VB 6 example.
This program makes a class named BorderStylesPictureBox that inherits from the PictureBox class.
This class overrides the parent class's WndProc routine to watch for WM_NCPAINT messages. When it catches this message, the class calls subroutine DrawBorder, which uses the DrawEdge API function to draw the control's edge in one of the styles: normal, none, sunken, sunken outer, raised, raised inner, bump, or etched.
|
|
Private Class BorderStylesPictureBox
Inherits PictureBox
...
Protected Overrides Sub WndProc(ByRef m As _
System.Windows.Forms.Message)
Const WM_NCPAINT As Int32 = &H85
' Watch for WM_NCPAINT.
If m.Msg = WM_NCPAINT Then
' It's WM_NCPAINT.
DrawBorder(m)
Else
' It's not WM_NCPAINT.
' Process normally.
MyBase.WndProc(m)
End If
End Sub
Private Sub DrawBorder(ByVal m As _
System.Windows.Forms.Message)
Dim mode As Int32
Dim hDC As Int32
Dim window_rect As RECT
' If BorderType is None, do nothing.
If m_BorderStyle = sedBorderStyle.sedNone Then Exit _
Sub
' Get the window's DC.
hDC = GetWindowDC(m.HWnd)
' Get the RECT that contains the window.
GetWindowRect(m.HWnd, window_rect)
With window_rect
.Right = .Right - .Left
.Bottom = .Bottom - .Top
.Left = 0
.Top = 0
End With
' Compose the needed border style.
mode = 0
Select Case m_BorderStyle
Case sedBorderStyle.sedRaised
mode = BDR_RAISED
Case sedBorderStyle.sedRaisedInner
mode = BDR_RAISEDINNER
Case sedBorderStyle.sedSunken
mode = BDR_SUNKEN
Case sedBorderStyle.sedSunkenOuter
mode = BDR_SUNKENOUTER
Case sedBorderStyle.sedEtched
mode = BDR_SUNKENOUTER Or BDR_RAISEDINNER
Case sedBorderStyle.sedBump
mode = BDR_SUNKENINNER Or BDR_RAISEDOUTER
End Select
' Draw the border.
DrawEdge(hDC, window_rect, mode, BF_RECT)
' Release the DC.
ReleaseDC(m.HWnd, hDC)
End Sub
End Class
|
|
The main program creates an instance of this class, positions it on the form, and adds it to the form's Controls collection.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Make the BorderStylesPictureBox.
m_BorderStylesPictureBox = New BorderStylesPictureBox
m_BorderStylesPictureBox.Location = New _
System.Drawing.Point(8, 8)
m_BorderStylesPictureBox.Name = _
"m_BorderStylesPictureBox"
m_BorderStylesPictureBox.Size = New _
System.Drawing.Size(104, 104)
Me.Controls.Add(m_BorderStylesPictureBox)
...
End Sub
|
|
When the user selects a new border style, the program sets the control's BorderStyle property. The control updates its property and redraws itself.
|
|
Private Sub cboStyle_SelectedIndexChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
cboStyle.SelectedIndexChanged
Dim border_style As _
BorderStylesPictureBox.sedBorderStyle
border_style = DirectCast(cboStyle.SelectedItem, _
BorderStylesPictureBox.sedBorderStyle)
m_BorderStylesPictureBox.BorderStyle = border_style
End Sub
|
|
|
|
|
|