Results 1 to 6 of 6

Thread: detecting when another window has been selected

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183
    I've been puzzling over this one for a day or so, I want to change the header bar of a window to black, but the only way I've been able to do this is to call the GetSysColor, SetSysColors. Now I want to restore the original colors whenever the user moves off the window. THe code I have catches it when you exit or minimize, but when you just click on another window it doesn't work.. Any suggestions?

    Private Declare Function GetSysColor Lib "user32.dll" (ByVal nIndex As Long) As Long
    Private Declare Function SetSysColors Lib "user32.dll" (ByVal cElements As Long, lpaElements As Long, lpaRgbValues As Long) As Long

    Const COLOR_ACTIVECAPTION = 2
    Const COLOR_GRADIENTACTIVECAPTION = 27
    Const COLOR_GRADIENTINACTIVECAPTION = 28
    Const COLOR_INACTIVECAPTION = 3

    Dim colorNames(0 To 3) As Long ' identifiers of the system colors to change
    Dim colorRGB(0 To 3) As Long ' RGB values of the system colors to change
    Dim colorRGB2(0 To 3) As Long ' RGB values of the system colors to change

    Dim retval As Long ' generic return value

    Private Sub Form_Load()
    CollectColors
    End Sub
    ' Get the RGB values of the colors used in title bars.
    Function CollectColors()
    colorNames(0) = COLOR_ACTIVECAPTION
    colorNames(1) = COLOR_GRADIENTACTIVECAPTION
    colorNames(2) = COLOR_INACTIVECAPTION
    colorNames(3) = COLOR_GRADIENTINACTIVECAPTION

    colorRGB(0) = GetSysColor(COLOR_ACTIVECAPTION)
    colorRGB(1) = GetSysColor(COLOR_GRADIENTACTIVECAPTION)
    colorRGB(2) = GetSysColor(COLOR_INACTIVECAPTION)
    colorRGB(3) = GetSysColor(COLOR_GRADIENTINACTIVECAPTION)
    Dim state As Boolean
    End Function

    Private Sub Form_Resize()

    If Me.WindowState = vbNormal Then
    'Change colors to black
    retval = SetSysColors(4, colorNames(0), colorRGB2(0))
    state = True
    ElseIf Me.WindowState = vbMinimized Then
    'Change colors back
    retval = SetSysColors(4, colorNames(0), colorRGB(0))
    state = False
    End If

    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    'reset colors
    retval = SetSysColors(4, colorNames(0), colorRGB(0))
    End Sub

  2. #2
    New Member
    Join Date
    Apr 2001
    Posts
    14
    Easy solution - not guaranteed to be the best solution.

    GetActiveWindow

    Within a timer event check the handle that this function returns and compare it to the window with the black bar. If it is different, then you know that the window has lost focus and therefor you may change the color back.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    Tried that one too

    yeah I tried that one too, but it was way too system heavy though, what with the checks going on all the time:
    if not the focus window then reset colors
    else set colors black

  4. #4
    New Member
    Join Date
    Apr 2001
    Posts
    14
    Have you tried subclassing the window? Do a debug.print of all the messages and see if you get a message when the window loses focus. If Windows can change the title bar when a window loses focus, then you can just as efficiently.

  5. #5
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    There's an OCX at Merrion Computing that is exactly what you need.

    Add it to your app's main form, and in the form load:
    Private Sub Form_Load()

    Me.VBEventWindow1.ParentForm = Me.hWnd

    End Sub

    Then put the code to turn your app's caption bar back to the previous colour in:
    Private Sub VBEventWindow1_ActiveApplicationChanged(ByVal ActivatingThisApp As Boolean, ByVal hThread As Long, Cancel As Boolean)

    End Sub

    This event is fired whenever the user leaves or enters your application.

    HTH,
    Merrion Computing Ltd

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    yeah that works great thanks

    awesome thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width