Results 1 to 18 of 18

Thread: Question on this code (title bar button)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161

    Question on this code (title bar button)

    On 11-14-2002 The Hobo was kind enough to post this code, It works great for the most part but I can't seem to get the button on the title bar to do anything. Could somone assist me and point me in the right sirection.

    Thanks

    Here is the post:

    System Tray and Title Bar Button

    This (really long) code is something I wrote awhile ago to demonstrate how to 1) add a button to the title bar, and 2) minimize the form to the system tray.

    VB Code:
    1. 'form code:
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.   Init
    6. End Sub
    7.  
    8. Private Sub Form_Unload(Cancel As Integer)
    9.   RemoveIcon
    10.   Terminate
    11. End Sub
    12.  
    13. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    14. Dim msg As Long
    15.  
    16.   msg = x / Screen.TwipsPerPixelX
    17.   Select Case msg
    18.     'Case WM_LBUTTONDOWN
    19.  
    20.     'Case WM_LBUTTONUP
    21.  
    22.     Case WM_LBUTTONDBLCLK
    23.       Me.Visible = True
    24.       Me.WindowState = 0
    25.     'Case WM_RBUTTONDOWN
    26.            
    27.     'Case WM_RBUTTONUP
    28.  
    29.     'Case WM_RBUTTONDBLCLK
    30.  
    31.   End Select
    32.  
    33. End Sub
    34.  
    35. Public Sub ButtonPressed()
    36.   AddIcon Me, "test"
    37. End Sub
    38.  
    39. 'module code:
    40. Option Explicit
    41.  
    42. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    43.   (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    44. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
    45.   lpRect As Rect) As Long
    46. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    47. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
    48.   ByVal hWndNewParent As Long) As Long
    49. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
    50.   ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx _
    51.   As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    52. Private Declare Function SetWindowsHookEx Lib "user32" Alias _
    53.   "SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal _
    54.   dwThreadId&) As Long
    55. Private Declare Function UnhookWindowsHookEx Lib "user32" _
    56.   (ByVal hHook&) As Long
    57. Private Declare Function CreateWindowEx Lib "user32" Alias _
    58.   "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, _
    59.   ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, _
    60.   ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal _
    61.   hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, _
    62.   lpParam As Any) As Long
    63. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
    64.   ByVal nCmdShow As Long) As Long
    65.  
    66. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias _
    67.   "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) _
    68.   As Boolean
    69.  
    70.  
    71. Private Type Rect
    72.     Left As Long
    73.     Top As Long
    74.     Right As Long
    75.     Bottom As Long
    76. End Type
    77.  
    78. Private Type CWPSTRUCT
    79.     lParam As Long
    80.     wParam As Long
    81.     Message As Long
    82.     hwnd As Long
    83. End Type
    84.  
    85. Public Type NOTIFYICONDATA
    86.    cbSize As Long
    87.    hwnd As Long
    88.    uid As Long
    89.    uFlags As Long
    90.    uCallBackMessage As Long
    91.    hIcon As Long
    92.    szTip As String * 64
    93. End Type
    94.  
    95. Private Const NIM_ADD = &H0
    96. Private Const NIM_MODIFY = &H1
    97. Private Const NIM_DELETE = &H2
    98. Private Const WM_MOUSEMOVE = &H200
    99. Private Const NIF_MESSAGE = &H1
    100. Private Const NIF_ICON = &H2
    101. Private Const NIF_TIP = &H4
    102.  
    103. Public Const WM_LBUTTONDBLCLK = &H203
    104. Public Const WM_LBUTTONDOWN = &H201
    105. Public Const WM_LBUTTONUP = &H202
    106. Public Const WM_RBUTTONDBLCLK = &H206
    107. Public Const WM_RBUTTONDOWN = &H204
    108. Public Const WM_RBUTTONUP = &H205
    109.  
    110. Private NID As NOTIFYICONDATA
    111.  
    112. Const WM_MOVE = &H3
    113. Const WM_SETCURSOR = &H20
    114. Const WM_NCPAINT = &H85
    115. Const WM_COMMAND = &H111
    116.  
    117. Const SWP_FRAMECHANGED = &H20
    118. Const GWL_EXSTYLE = -20
    119.  
    120. Private WHook&
    121. Private ButtonHwnd As Long
    122.  
    123. Public Sub Init()
    124.     'Create the button that is going to be placed in the Titlebar
    125.     ButtonHwnd& = CreateWindowEx(0&, "Button", "-", &H40000000, 50, 50, 14, 14, frmMain.hwnd, 0&, App.hInstance, 0&)
    126.     'Show the button cause it´s invisible
    127.     Call ShowWindow(ButtonHwnd&, 1)
    128.     'Initialize the window hooking for the button
    129.     WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)
    130.     Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80)
    131.     Call SetParent(ButtonHwnd&, GetParent(frmMain.hwnd))
    132. End Sub
    133.  
    134. Public Sub Terminate()
    135.     'Terminate the window hooking
    136.     Call UnhookWindowsHookEx(WHook)
    137.     Call SetParent(ButtonHwnd&, frmMain.hwnd)
    138. End Sub
    139.  
    140. Public Function HookProc&(ByVal nCode&, ByVal wParam&, Inf As CWPSTRUCT)
    141.     Dim FormRect As Rect
    142.     Static LastParam&
    143.     If Inf.hwnd = GetParent(ButtonHwnd&) Then
    144.         If Inf.Message = WM_COMMAND Then
    145.             Select Case LastParam
    146.                 'If the LastParam is cmdInTitlebar call the Click-Procedure
    147.                 'of the button
    148.                 Case ButtonHwnd&: frmMain.ButtonPressed
    149.             End Select
    150.         ElseIf Inf.Message = WM_SETCURSOR Then
    151.             LastParam = Inf.wParam
    152.         End If
    153.         ElseIf Inf.hwnd = frmMain.hwnd Then
    154.         If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then
    155.             'Get the size of the Form
    156.             Call GetWindowRect(frmMain.hwnd, FormRect)
    157.             'Place the button int the Titlebar
    158.             Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)
    159.         End If
    160.     End If
    161. End Function
    162.  
    163. Public Sub AddIcon(TheForm As Form, strT As String)
    164.     NID.cbSize = Len(NID)
    165.     NID.hwnd = TheForm.hwnd
    166.     NID.uid = vbNull
    167.     NID.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    168.     NID.uCallBackMessage = WM_MOUSEMOVE
    169.     NID.hIcon = TheForm.Icon
    170.     NID.szTip = strT & vbNullChar
    171.     Shell_NotifyIcon NIM_ADD, NID
    172.    
    173.     TheForm.WindowState = vbMinimized
    174.     TheForm.Hide
    175. End Sub
    176.  
    177. Public Sub RemoveIcon()
    178.   Shell_NotifyIcon NIM_DELETE, NID
    179. End Sub
    -------------------------
    My name says it all!

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    hmm...

    Maybe I left something out. I'll have to check it when I get back from vacation on Sunday, if somebody hasn't helped you by then.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    That would be great... Thanks a bunch man.
    -------------------------
    My name says it all!

  4. #4
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    he did leave something out.

    most peple I've ever seen do this use a picture box to store the image instead of the form itself, anyhows to get the icon to do something, you need to do code in the form for the click events.

    In the MouseUp event, you would do a me.popup menu thingy.

    In the DblClick Event you would do form.visible = true

    If I can find an example I'll post it later.
    It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Hey that would be great... any help would be appreciated.

    Thanks
    -------------------------
    My name says it all!

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Dalceon
    he did leave something out.

    most peple I've ever seen do this use a picture box to store the image instead of the form itself, anyhows to get the icon to do something, you need to do code in the form for the click events.

    In the MouseUp event, you would do a me.popup menu thingy.

    In the DblClick Event you would do form.visible = true

    If I can find an example I'll post it later.
    He is having problems with the button being placed in the title bar, not the icon being placed in the system tray. Unless I am confused...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Button is there.... Just when I press it nothing happens. If I place another button on the form and call the ButtonPressed sub... it works... I get an icon on the tray. I would like to get the same results with the title bar button your code puts there.

    Thanks
    Last edited by CoderNewbie; Jul 8th, 2003 at 01:39 PM.
    -------------------------
    My name says it all!

  8. #8
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I think the problem is with the fact that your use of SetParent. I don't know of any other way to put a button in the titlebar, but if you comment out the SetParent line, the button works at least. It must screw up which window is receiving the WM_COMMAND message. You'll probably have to subclass the button, too.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Hmmm I remove it out of he sub init and terminate and I no longer get the button. If there is another place I need to remove it, I cant locate it.

    Thanks
    -------------------------
    My name says it all!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Ahh Forget the post before this. If I comment out that line its no longer in the title bar... but the button works... If Hobo doesn't have a fix for this... its back to the drawing board for me. Thanks for the input... and thanks Hobo for taking the time to look at it.
    -------------------------
    My name says it all!

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Any luck on this Hobo? If not let me know so I can figure another way to tray my program. Thanks man.
    -------------------------
    My name says it all!

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by CoderNewbie
    Any luck on this Hobo? If not let me know so I can figure another way to tray my program. Thanks man.
    Whoops. I forgot all about this.

    I'm not sure if I left something out when I posted this code, but attached is the source that I had on my hard drive. I ran the code and it works perfectly for me.

    Let me know if this fixes the problem or not.
    Attached Files Attached Files
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Hey thanks for posting the zip file but still no cigar. I run the example you gave me and I get the button but the button does nothing.
    -------------------------
    My name says it all!

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Odd.

    What OS and what version of VB?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    xp pro, vb6 sp5.

    Yea it is odd... driving me nuts I normally wouldn't put so much effort into this but I really like the way that code works.

    Thanks
    -------------------------
    My name says it all!

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm running 9x, so maybe there's a difference in the API. I've tried a few other tricks, but none of them seem to work.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Ahh... Well I appreciate ya looking at it. I'll just use the code with a button to minimize to tray.

    Thanks again.
    -------------------------
    My name says it all!

  18. #18
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    The subclassing code seems very strange, never seen anythibng like that before...
    The code doesn't work on my PC either...it doesn't trap the button being clicked...starting to think it's the subclassing...will have a look laterz...


    Woka

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