Home » » Changing Border Color of Controls

Changing Border Color of Controls

 
Existing controls in VB does not provide a property to change the color bordernya. So if you want to change the color bordernya additional code is required.

How to make it as below:

Create a new project with a form and a Module therein.

Module type in the following code:

[ VB 6.0 ]
Private Declare Function CreateRectRgn Lib "gdi32.dll" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32.dll" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Long) As Long
Private Declare Function FrameRgn Lib "gdi32.dll" (ByVal hDC As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long

Public Sub BorderColor(ByVal Ctl As Control, ByVal lColor As OLE_COLOR, Optional ByVal BorderWidth As Integer = 2)
    Dim l As Long
   
    l = CreateRectRgn(BorderWidth, BorderWidth, (Ctl.Width / Screen.TwipsPerPixelX) - BorderWidth, (Ctl.Height / Screen.TwipsPerPixelY) - BorderWidth)
    SetWindowRgn Ctl.hwnd, l, False

    l = CreateRectRgn(Ctl.Left / Screen.TwipsPerPixelX, Ctl.Top / Screen.TwipsPerPixelY, Ctl.Width / Screen.TwipsPerPixelX + (Ctl.Left / Screen.TwipsPerPixelX), Ctl.Height / Screen.TwipsPerPixelY + (Ctl.Top / Screen.TwipsPerPixelY))
    FrameRgn Ctl.Container.hDC, l, CreateSolidBrush(lColor), BorderWidth, BorderWidth
End Sub

[ VB .NET ]
Private Declare Function CreateRectRgn Lib "gdi32.dll" (ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) As Integer
Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Integer, ByVal hRgn As Integer, ByVal bRedraw As Boolean) As Integer
Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Integer) As Integer
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Private Declare Function FrameRgn Lib "gdi32.dll" (ByVal hdc As Integer, ByVal hRgn As Integer, ByVal hBrush As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer

Public Sub BorderColor(ByVal Ctl As Control, ByVal lColor As Color, Optional ByVal BorderWidth As Integer = 1)
        Dim i As Integer

        i = CreateRectRgn(BorderWidth, BorderWidth, Ctl.Width - BorderWidth, Ctl.Height - BorderWidth)
        SetWindowRgn(Ctl.Handle, i, False)

        i = CreateRectRgn(Ctl.Left, Ctl.Top, Ctl.Width + Ctl.Left, Ctl.Height + Ctl.Top)
        FrameRgn(GetDC(Ctl.Parent.Handle), i, CreateSolidBrush(CInt(ColorTranslator.ToWin32(lColor))), BorderWidth, BorderWidth)
End Sub


Now create a TextBox on Form1 and for example you want to change the color to blue bordernya, then write the following code in the 'Form_Paint' of Form1:[ VB 6.0 ]
BorderColor Text1, vbBlue
[ VB .NET ]
BorderColor(TextBox1, Color.Blue)



NOTE:

- For users of VB 6.0, which will be replaced when the control is placed in a PictureBox bordernya color, then the property "AutoRedraw" of PicturBox should value "True".

- For users of VB 6.0, which will be replaced when the control is placed in the color bordernya Frame or Tab, then the above methods can not be used. The solution create a PictureBox (AutoRedraw property = True; BorderStyle = 0) to the Frame or Tab, the new control will replaced bordernya colors placed in the PictureBox.
Jika Anda menyukai Artikel di blog ini, Silahkan klik disini untuk berlangganan gratis via email, dengan begitu Anda akan mendapat kiriman artikel setiap ada artikel yang terbit di Creating Website

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Sanjaya Catur Blogging - All Rights Reserved
Template Modify by Creating Website
Proudly powered by Blogger