Changes

Color Constants / Color Forms / Controls / Transparency VB6

5,654 bytes added, 19:33, 10 February 2008
/* All Possible Ways */
The following lines were added (+) and removed (-):
== Table 1 ==== VB / VBA Color Constants == === Table 1 ===== Table 2 ===== Table 2 ===== Examples ===== Table 3 ===The eight members of the VBA Color Constants class: <big>:* vbBlack:* vbWhite:* vbRed:* vbGreen:* vbBlue:* vbYellow :* vbMagenta:* vbCyan</big>&nbsp;=== Examples ===Example 1:This is the same as "Button Highlight"Example 2: This is the same as "Button Highlight"&nbsp;&nbsp;== Setting Form and Control Colors ==* Blue : Me.BackColor = RGB(0, 0, 255)  Me.BackColor = RGB(0, 0, 255)* Green: Me.BackColor = RGB(0, 255, 0)  Me.BackColor = RGB(0, 255, 0)* Red: Me.BackColor = RGB(255, 0, 0)  Me.BackColor = RGB(255, 0, 0)=== All Possible Ways ===1. In HEX, BLUE is first, Green, then Red, the reverse of RGB (actually BGR) is the default VB method  cmd(0).BackColor = &HFF&      ' Red  / &H0000FF&  cmd(1).BackColor = &HFF00&    ' Green / &H00FF00&  cmd(2).BackColor = &HFF0000  ' Blue  / &HFF0000&2. Decimal RGB value  cmd(0).BackColor = RGB(255, 0, 0)  cmd(1).BackColor = RGB(0, 255, 0)  cmd(2).BackColor = RGB(0, 0, 255)3. Using VB Color Constants  cmd(0).BackColor = vbRed  cmd(1).BackColor = vbGreen  cmd(2).BackColor = vbBlue=== Convert RGB Hex (like used in HTML) to VB Hex ===Code Sample: Public Function h2v(wHex As String) As Long  Dim strTmp As String  wHex = Trim("" & wHex)  If Len(wHex) = 7 Then    If Left(wHex, 1) = "#" Then wHex = Right(wHex, 6)  End If  If Len(wHex) = 6 Then    strTmp = "&H" & Right(wHex, 2) & Mid(wHex, 3, 2) & Left(wHex, 2) & "&"  Else    strTmp = "&H000000&"  End If  h2v = Val(strTmp)  Debug.Print h2v End Function&nbsp;&nbsp;== Transparency ===== Entire Form and all controls ===  <nowiki>Option Explicit</nowiki>  <nowiki></nowiki>  <nowiki>Private Const GWL_EXSTYLE = (-20)</nowiki>  <nowiki>Private Const WS_EX_LAYERED = &H80000</nowiki>  <nowiki>Private Const LWA_ALPHA = &H2</nowiki>  <nowiki></nowiki>  <nowiki>Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long</nowiki>  <nowiki>Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long</nowiki>  <nowiki>Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long</nowiki>  <nowiki></nowiki>  <nowiki>Private Sub Form_Load()</nowiki>  <nowiki>  TranslucentForm Me, 255  'Make form 0% translucent</nowiki>  <nowiki>End Sub</nowiki>  <nowiki></nowiki>  <nowiki>Private Sub Slider1_Scroll()</nowiki>  <nowiki>  TranslucentForm Me, Slider1.Value</nowiki>  <nowiki>End Sub</nowiki>  <nowiki></nowiki>  <nowiki>Public Function TranslucentForm(frm As Form, TranslucenceLevel As Byte) As Boolean</nowiki>  <nowiki>  SetWindowLong frm.hWnd, GWL_EXSTYLE, WS_EX_LAYERED</nowiki>  <nowiki>  SetLayeredWindowAttributes frm.hWnd, 0, TranslucenceLevel, LWA_ALPHA</nowiki>  <nowiki>  TranslucentForm = Err.LastDllError = 0</nowiki>  <nowiki>End Function</nowiki>=== By Color + Hollow Form ===  <nowiki>Option Explicit</nowiki>  <nowiki></nowiki>  <nowiki>Public Const LWA_COLORKEY = 1</nowiki>  <nowiki>Public Const LWA_ALPHA = 2</nowiki>  <nowiki>Public Const LWA_BOTH = 3</nowiki>  <nowiki>Public Const WS_EX_LAYERED = &H80000</nowiki>  <nowiki>Public Const GWL_EXSTYLE = -20</nowiki>  <nowiki>Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal bXColor As Long, ByVal x As Byte, ByVal a</nowiki>  <nowiki>lpha As Long) As Boolean</nowiki>  <nowiki>Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong</nowiki>  <nowiki> As Long) As Long</nowiki>  <nowiki>Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long</nowiki>  <nowiki>Public g_nTransparency As Integer</nowiki>  <nowiki>Public bXColor As Long</nowiki>  <nowiki>Public flag As Byte</nowiki>  <nowiki></nowiki>  <nowiki></nowiki>  <nowiki></nowiki>  <nowiki>Option Explicit</nowiki>  <nowiki></nowiki>  <nowiki>Private Sub cmdExit_Click()</nowiki>  <nowiki>  Unload Me</nowiki>  <nowiki>End Sub</nowiki>  <nowiki></nowiki>  <nowiki>Private Sub Form_Load()</nowiki>  <nowiki>  initFormControls</nowiki>  <nowiki>  initTransParency</nowiki>  <nowiki>End Sub</nowiki>  <nowiki></nowiki>  <nowiki>Private Sub initFormControls()</nowiki>  <nowiki>  Me.Width = Screen.Width: Me.Height = Screen.Height: Me.Top = 0: Me.Left = 0</nowiki>  <nowiki>  cmdExit.Left = Screen.Width - 800: cmdExit.Top = 200</nowiki>  <nowiki>End Sub</nowiki>  <nowiki></nowiki>  <nowiki>Private Sub initTransParency()</nowiki>  <nowiki>  bXColor = RGB(1, 2, 3)</nowiki>  <nowiki>  Me.BackColor = bXColor</nowiki>  <nowiki>  g_nTransparency = 150</nowiki>  <nowiki>  flag = 0 Or LWA_COLORKEY</nowiki>  <nowiki>  SetTranslucent Me.hwnd, bXColor, g_nTransparency, flag</nowiki>  <nowiki>End Sub</nowiki>  <nowiki>Option Explicit</nowiki>  <nowiki></nowiki>  <nowiki>Sub SetTranslucent(ThehWnd As Long, bXColor As Long, nTrans As Integer, flag As Byte)</nowiki>  <nowiki>    Dim attrib As Long</nowiki>  <nowiki>    On Error GoTo ErrorRtn</nowiki>  <nowiki>    attrib = GetWindowLong(ThehWnd, GWL_EXSTYLE)</nowiki>  <nowiki>    SetWindowLong ThehWnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED</nowiki>  <nowiki>    SetLayeredWindowAttributes ThehWnd, bXColor, nTrans, flag</nowiki>  <nowiki>    Exit Sub</nowiki>  <nowiki>ErrorRtn:</nowiki>  <nowiki>    MsgBox Err.Description & " Source : " & Err.Source</nowiki>  <nowiki>End Sub</nowiki>  <nowiki></nowiki> 
Bureaucrat, administrator
16,192
edits