Home » » warna border control

warna border control


Mengganti Warna Border dari Kontrol



Kontrol-kontrol yang ada di VB tidak menyediakan properti untuk mengganti warna bordernya. Jadi jika Anda ingin mengubah warna bordernya diperlukan kode tambahan.
Cara pembuatannya seperti di bawah ini :

Buat Project baru dengan sebuah Form dan sebuah Module didalamnya.
Di Module ketikkan kode berikut :

[ 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



Sekarang buatlah sebuah TextBox di Form1 dan misalnya Anda ingin diubah warna bordernya menjadi warna biru, maka tuliskan kode berikut ini di bagian 'Form_Paint' dari Form1 :
[ VB 6.0 ]
BorderColor Text1, vbBlue
[ VB .NET ]
BorderColor(TextBox1, Color.Blue)



CATATAN :
- Bagi pengguna VB 6.0,  apabila Kontrol yang akan diganti warna bordernya ditempatkan di PictureBox, maka properti "AutoRedraw" dari PicturBox tersebut harus bernilai "True".
- Bagi pengguna VB 6.0,  apabila Kontrol yang akan diganti warna bordernya ditempatkan di Frame atau Tab, maka cara diatas tidak dapat digunakan. Solusinya buatlah sebuah PictureBox (properti AutoRedraw=True; BorderStyle=0) ke dalam Frame atau Tab tersebut, baru Kontrol yang akan diganti warna bordernya ditempatkan di PictureBox tersebut.

Semoga Berhasil
Share this article :

0 komentar:

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2012. Q Hanya Butuh Kesetia'an - All Rights Reserved
Template Modify by Raikage Wahyu
Powered by Blogger