Label control border color

  • Thread starter Thread starter jcrouse
  • Start date Start date
Hi,

Here is a class that uses nativewindows to change the border to white.
Create an instance for each label you want to have a white border.


Public Class WhiteBorderChanger
Inherits NativeWindow

Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC"
(ByVal hwnd As IntPtr) _
As IntPtr
Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal
hwnd As IntPtr, _
ByVal hdc As IntPtr) As Integer

Private ctrl As Control

Public Sub New(ByVal ctrl As Control)

AssignHandle(ctrl.Handle)
Me.ctrl = ctrl
End Sub


Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages

Const WM_NCPAINT As Integer = &H85

If m.Msg = WM_NCPAINT Then
Dim hdc As IntPtr = GetWindowDC(m.HWnd)
Dim g As Graphics = Graphics.FromHdc(hdc)

Dim pXp As Pen = New Pen(Color.White, 3)

g.DrawRectangle(pXp, 0, 0, ctrl.Width - 1, ctrl.Height -
1)
ReleaseDC(Me.Handle, hdc)
Else
MyBase.WndProc(m)

End If
End Sub

Protected Overrides Sub Finalize()
ReleaseHandle()
MyBase.Finalize()
End Sub
End Class



Ken
-------------------
 
This is ask due to my VB ignorance, sorry. Where should I put the code?
Also, if I need an instance for each label, what is the piece the associates
an instance with a particular label?

Thank you very much,
John
 
Hi,

Sorry should have posted some code.

Dim arLabelBorderChanger As New ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddLabel(Me.Controls)
End Sub

Private Sub AddLabel(ByVal ctrls As Control.ControlCollection)
For Each ctrl As Control In ctrls
If TypeOf ctrl Is Label Then
DirectCast(ctrl, Label).BorderStyle =
BorderStyle.FixedSingle
arLabelBorderChanger.Add(New WhiteBorderChanger(ctrl))
End If
AddLabel(ctrl.Controls) ' add a child controls to list
Next
End Sub

Ken
 
Back
Top