Label control border color

  • Thread starter Thread starter jcrouse
  • Start date Start date
J

jcrouse

Is there a quick and easy way to change the color of a label controls border
from the default black to white?

Thank you,
John
 
Hi,

If you want to change the border color you have to override the
WM_NCPAINT message.


Public Class BorderLabel
Inherits Label

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


Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
Const WM_NCPAINT = &H85

If m.Msg = WM_NCPAINT Then
Dim penBorder As New Pen(Color.White, 3)
Dim hdc As IntPtr = GetWindowDC(m.HWnd)
Dim g As Graphics = Graphics.FromHdc(hdc)
Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width,
Me.Height)

g.DrawRectangle(penBorder, rDraw)

ReleaseDC(Me.Handle, hdc)
Else
MyBase.WndProc(m)

End If

End Sub

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

Public Sub New()
Me.BorderStyle = BorderStyle.FixedSingle
End Sub
End Class

Ken
---------------------------
 
* Ken Tucker said:
Dim penBorder As New Pen(Color.White, 3)
Dim hdc As IntPtr = GetWindowDC(m.HWnd)
Dim g As Graphics = Graphics.FromHdc(hdc)
Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width,
Me.Height)

g.DrawRectangle(penBorder, rDraw)

ReleaseDC(Me.Handle, hdc)

Don't forget to dispose the pen 'penBorder' there.
 
Ken,
What do I do with this code or where does it need to go. I created a new
project and added one label to the form. Here is the entire code:

Public Class Form1
Inherits System.Windows.Forms.Form



#Region " Windows Form Designer generated code "



Public Sub New()

MyBase.New()



'This call is required by the Windows Form Designer.

InitializeComponent()



'Add any initialization after the InitializeComponent() call



End Sub



'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub



'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer



'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'Label1

'

Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle

Me.Label1.Location = New System.Drawing.Point(48, 56)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(136, 48)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)



End Sub



#End Region


I added it on the end but got nothing. Do I need to call something somehow
or mix pieces of the code in with the above code?

Thanks,
John
 
Hi John,

Using Kens code I made this class, you can use it by creating a new item
(usercontrol), name it UserLabel, delete all the code, build, and now you
can drag it from your toolbox usercontrols on your form.

Public Class UserLabel
Inherits System.Windows.Forms.Label
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.BorderStyle = BorderStyle.FixedSingle
End Sub
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
Protected Overrides Sub WndProc(ByRef m _
As System.Windows.Forms.Message)
Const WM_NCPAINT As Integer = &H85
If m.Msg = WM_NCPAINT Then
Dim penBorder As New Pen(Color.White, 3)
Dim hdc As IntPtr = GetWindowDC(m.HWnd)
Dim g As Graphics = Graphics.FromHdc(hdc)
Dim rDraw As Rectangle = _
New Rectangle(0, 0, Me.Width, Me.Height)
g.DrawRectangle(penBorder, rDraw)
ReleaseDC(Me.Handle, hdc)
g.Dispose()
Else
MyBase.WndProc(m)
End If
End Sub
End Class

I hope this helps?
(The code is changed from Ken not original from me).

Cor
 
Hi,

Compile it into a control and add it to your form.
http://www.onteorasoftware.com/downloads/labelwithwhiteborder.zip

Ken
-------------

jcrouse said:
Ken,
What do I do with this code or where does it need to go. I created a new

project and added one label to the form. Here is the entire code:

Public Class Form1
Inherits System.Windows.Forms.Form



#Region " Windows Form Designer generated code "



Public Sub New()

MyBase.New()



'This call is required by the Windows Form Designer.

InitializeComponent()



'Add any initialization after the InitializeComponent() call



End Sub



'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub



'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer



'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'Label1

'

Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle

Me.Label1.Location = New System.Drawing.Point(48, 56)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(136, 48)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)



End Sub



#End Region


I added it on the end but got nothing. Do I need to call something somehow

or mix pieces of the code in with the above code?

Thanks,
John

"Ken Tucker [MVP]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
 
Hi Herfried,

You can be glad that Armin is not active some days I think.
... but in this case not necessary. The value will be treated as
'Int32' automatically.

Do you think I saw this, I was just making a sample for John, while I
thought Ken was not active, when I saw that Ken was I had already made it
and found it a pity not to send it.

Cor
 
* "Cor Ligthert said:
You can be glad that Armin is not active some days I think.
Why?!


Do you think I saw this, I was just making a sample for John, while I
thought Ken was not active, when I saw that Ken was I had already made it
and found it a pity not to send it.

\\\
Const WM_NCPAINT = &H85
MsgBox(WM_NCPAINT.GetType().ToString())
///
 
Option Strict On requires all variable declarations to have an 'As' clause.
As Armin would point out, we should all have Option Strict On

;-)
 
* "Mick Doherty said:
Option Strict On requires all variable declarations to have an 'As' clause.
As Armin would point out, we should all have Option Strict On

Yep. I worked with 'Option Strict Off'. The error message is
misleading because it's a constant, not a variable.
 
Herfried K. Wagner said:
Yep. I worked with 'Option Strict Off'. The error message is
misleading because it's a constant, not a variable.

I agree. Especially since if you put the Constant in an Enum it is
automatically an Int32 unless you specify differently.
 
Mick,

* "Mick Doherty said:
I agree. Especially since if you put the Constant in an Enum it is
automatically an Int32 unless you specify differently.

.... there is no error message if there is no type specified for a
constant inside an enum.

;-)
 
Herfried K. Wagner said:
... there is no error message if there is no type specified for a
constant inside an enum.

That's what I said.
"if you put the Constant in an Enum it is automatically an Int32 unless you
specify differently"
i.e.
\\\
Enum ByteEnum As Byte
SomeVar = 2
End Enum

Enum Int32Enum
SomeVar = 2
End Enum
///
 
Mick,

* "Mick Doherty said:
That's what I said.
"if you put the Constant in an Enum it is automatically an Int32 unless you
specify differently"
i.e.
\\\
Enum ByteEnum As Byte
SomeVar = 2
End Enum

Enum Int32Enum
SomeVar = 2
End Enum
///

OK, yes, you can only specify the type of the enum, not for the
constants (as expected). I wonder too why there is no compile time
error with 'Option Strict On' when implicitly using 'Int32' as base
type.
 
Well, I finally got a chance to try it out. Nice control. However, I already have two forms and 52 labels and probably 60 pages of code and don't really want to start from scratch. I tried to delete a label, add one of yours and rename it to the original name but quite a bit of the code didn't work. I am actually moving the control at runtime and a few other things.

Thanks alot,
John
 
Hi John,

After sending my message I got the idea of using flatstyle buttons withouth
using the events (maybe a little bit dificult in your situation because we
made those dynamic buttons, however I do not know how far you did implement
that). You can use a label as a button so why not a button as a label.

Cor
Well, I finally got a chance to try it out. Nice control. However, I
already have two forms and 52 labels and probably 60 pages of code and don't
really want to start from scratch. I tried to delete a label, add one of
yours and rename it to the original name but quite a bit of the code didn't
work. I am actually moving the control at runtime and a few other things.
 
Sorry if this posts twice. I thought I added this a day or two ago but don't see it. I have 30 some controls and would like to change the existing ones instead of adding new controls. I have LOTS of code to consider. Any ideas how?

Thank you,
John
 
Back
Top