<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="https://syndication.webwiz.net/rss_namespace/">
 <channel>
  <title>Codejock Developer Community : TrayIcon GetForegroundWindow Problem</title>
  <link>http://forum.codejock.com/</link>
  <description><![CDATA[This is an XML content feed of; Codejock Developer Community : Suite Pro : TrayIcon GetForegroundWindow Problem]]></description>
  <copyright>Copyright (c) 2006-2013 Web Wiz Forums - All Rights Reserved.</copyright>
  <pubDate>Wed, 22 Apr 2026 04:13:01 +0000</pubDate>
  <lastBuildDate>Mon, 21 Jan 2008 11:47:46 +0000</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 12.04</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>forum.codejock.com/RSS_post_feed.asp?TID=9357</WebWizForums:feedURL>
  <image>
   <title><![CDATA[Codejock Developer Community]]></title>
   <url>http://forum.codejock.com/forum_images/codejock-logo.gif</url>
   <link>http://forum.codejock.com/</link>
  </image>
  <item>
   <title><![CDATA[TrayIcon GetForegroundWindow Problem :  The following method should...]]></title>
   <link>http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30356&amp;title=trayicon-getforegroundwindow-problem#30356</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://forum.codejock.com/member_profile.asp?PF=2676">jpbro</a><br /><strong>Subject:</strong> 9357<br /><strong>Posted:</strong> 21 January 2008 at 11:47am<br /><br />The following method should work in *most* cases. The IsForegroundTaskWindow call may incorrectly return false in the case where a legitimate task window with no caption is actually in front of the passed window, but hopefully this is a rare occurence?<br><br>In a module:<br><br>Option Explicit<br><br>' Required Win32 API Declarations<br>Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long<br>Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long<br>Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long<br>Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long<br>Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long<br>Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long<br>Private Declare Function GetForegroundWindow Lib "user32" () As Long<br>Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long<br>Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long<br>Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long<br>Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long<br>' API Constants<br>Private Const GWL_HWNDPARENT As Long = (-8) ' Constant used to determine window owner<br>Private Const SW_SHOW = 5&nbsp;&nbsp; ' Bring to front<br>Private Const SW_RESTORE = 9&nbsp;&nbsp;&nbsp; ' Restore from minimized<br><br>Private mlngForegroundTaskHwnd As Long<br><br>Public Function IsForegroundTaskWindow(hWnd As Long) As Long<br>&nbsp;&nbsp;&nbsp; '---------------------------------------------------------------------------------------<br>&nbsp;&nbsp;&nbsp; ' Procedure : IsForegroundTaskWindow<br>&nbsp;&nbsp;&nbsp; ' Created&nbsp;&nbsp; : January 21, 2008<br>&nbsp;&nbsp;&nbsp; ' Author&nbsp;&nbsp;&nbsp; : Jason Peter Brown (jbrown@statslog.com)<br>&nbsp;&nbsp;&nbsp; ' Purpose&nbsp;&nbsp; : To determine whether our window is the foreground window during a<br>&nbsp;&nbsp;&nbsp; '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TrayIcon Click event<br>&nbsp;&nbsp;&nbsp; ' Notes&nbsp;&nbsp;&nbsp;&nbsp; : This is necessary because Shell_TrayWnd class steals the focus before<br>&nbsp;&nbsp;&nbsp; '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; we can test if our window is the foreground window<br>&nbsp;&nbsp;&nbsp; ' Returns&nbsp;&nbsp; : True if passed hWnd is (most likely) the foreground window<br>&nbsp;&nbsp;&nbsp; '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; False if not<br>&nbsp;&nbsp;&nbsp; ' Issues&nbsp;&nbsp;&nbsp; : May be incorrect if the actual foreground task window has no caption<br>&nbsp;&nbsp;&nbsp; '---------------------------------------------------------------------------------------<br><br>&nbsp;&nbsp;&nbsp; Call EnumWindows(AddressOf EnumWindowsProc, hWnd)&nbsp;&nbsp; ' Determine the hwnd of the foreground task<br>&nbsp;&nbsp;&nbsp; IsForegroundTaskWindow = (mlngForegroundTaskHwnd = hWnd)&nbsp;&nbsp;&nbsp; ' If the results from EnumWindowsProc matches the passed hwnd, we can assume we are the foreground task<br>&nbsp;&nbsp;&nbsp; mlngForegroundTaskHwnd = 0&nbsp; ' Reset the Foreground Task Hwnd for the next call<br>End Function<br><br>Private Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long<br>&nbsp;&nbsp;&nbsp; '---------------------------------------------------------------------------------------<br>&nbsp;&nbsp;&nbsp; ' Procedure : EnumWindowsProc<br>&nbsp;&nbsp;&nbsp; ' Created&nbsp;&nbsp; : January 21, 2008<br>&nbsp;&nbsp;&nbsp; ' Author&nbsp;&nbsp;&nbsp; : Jason Peter Brown (jbrown@statslog.com), adapted from code by<br>&nbsp;&nbsp;&nbsp; '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Karl E. Peterson (http://vb.mvps.org/articles/ap199902.pdf)<br>&nbsp;&nbsp;&nbsp; ' Purpose&nbsp;&nbsp; : To discover the hWnd of the foreground task window (window with a caption)<br>&nbsp;&nbsp;&nbsp; ' Returns&nbsp;&nbsp; : 1 to continue searching windows in the task list<br>&nbsp;&nbsp;&nbsp; '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 to stop searching windows<br>&nbsp;&nbsp;&nbsp; ' Issues&nbsp;&nbsp;&nbsp; : Will be incorrect if the actual foreground task window has no caption<br>&nbsp;&nbsp;&nbsp; '---------------------------------------------------------------------------------------<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; Dim strWindowText As String ' Window Caption<br>&nbsp;&nbsp;&nbsp; Dim r As Long&nbsp;&nbsp; ' API return value<br>&nbsp;&nbsp;&nbsp; Dim lngContinue As Long: lngContinue = 1&nbsp;&nbsp;&nbsp; ' 1 = search for next window, 0 = stop searching<br>&nbsp;&nbsp;&nbsp; Dim lngThread1 As Long&nbsp; ' For comparing the hWnd thread to the lParam thread<br>&nbsp;&nbsp;&nbsp; Dim lngThread2 As Long&nbsp; ' If they are the same, the we can assume our window<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' is the foreground task window<br>&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp; ' Make sure we meet visibility requirements.<br>&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp; If IsWindowVisible(hWnd) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' It shouldn't have any parent window, either.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If GetParent(hWnd) = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' And, finally, it shouldn't have an owner.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If GetWindowLong(hWnd, GWL_HWNDPARENT) = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Retrieve windowtext (caption)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' We do this, because we are looking for the first<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' window with a caption. This *should* be the topmost task window<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strWindowText = Space$(256)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = GetWindowText(hWnd, strWindowText, Len(strWindowText))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If r Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' There was a window caption, so this is (hopefully) a task window<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' This will cause problems if the actual foreground task window<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' does not have a caption...Ideas?<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Get the ThreadIDs of the hWnd and lParam, to see if they are the same<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngThread1 = GetWindowThreadProcessId(hWnd, ByVal 0&amp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngThread2 = GetWindowThreadProcessId(lParam, ByVal 0&amp;)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If lngThread1 = lngThread2 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' The threads are the same, so our window is the foreground window<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mlngForegroundTaskHwnd = lParam<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' The threads are different, so the foreground window is NOT our window<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mlngForegroundTaskHwnd = hWnd<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngContinue = 0&nbsp;&nbsp;&nbsp; ' If we have gone this far, we should stop searching<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' because we are either the foreground task window or not<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' at this point<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp; EnumWindowsProc = lngContinue&nbsp; ' Return 1 to keep looking for a window, otherwise 0 if we are done<br>End Function<br><br>Public Function ForceForegroundWindow(ByVal hWnd As Long) As Boolean<br>&nbsp;&nbsp;&nbsp; '---------------------------------------------------------------------------------------<br>&nbsp;&nbsp;&nbsp; ' Procedure : ForceForegroundWindow<br>&nbsp;&nbsp;&nbsp; ' Created&nbsp;&nbsp; : January 21, 2008<br>&nbsp;&nbsp;&nbsp; ' Author&nbsp;&nbsp;&nbsp; : Karl E. Peterson (http://vb.mvps.org/articles/ap199902.pdf) with minor<br>&nbsp;&nbsp;&nbsp; '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; style modifications by Jason Peter Brown (jbrown@statslog.com)<br>&nbsp;&nbsp;&nbsp; ' Purpose&nbsp;&nbsp; : To push a window to the foreground under all known circumstances<br>&nbsp;&nbsp;&nbsp; ' Returns&nbsp;&nbsp; : True on success, False on failure<br>&nbsp;&nbsp;&nbsp; '---------------------------------------------------------------------------------------<br>&nbsp;&nbsp;&nbsp; Dim lngThread1 As Long<br>&nbsp;&nbsp;&nbsp; Dim lngThread2 As Long<br>&nbsp;&nbsp;&nbsp; Dim r As Long<br>&nbsp;&nbsp;&nbsp; Dim strClass As String<br>&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp; ' Nothing to do if already in foreground.<br>&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp; If hWnd = GetForegroundWindow() Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForceForegroundWindow = True<br>&nbsp;&nbsp;&nbsp; Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' VB4/32, VB5, VB6 Push Your Way to the Front<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' First need to get the thread responsible for<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' the foreground window, then the thread running<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' the passed window.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngThread1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&amp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngThread2 = GetWindowThreadProcessId(hWnd, ByVal 0&amp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' By sharing input state, threads share their<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' concept of the active window.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If lngThread1 &lt;&gt; lngThread2 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call AttachThreadInput(lngThread1, lngThread2, True)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = SetForegroundWindow(hWnd)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call AttachThreadInput(lngThread1, lngThread2, False)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = SetForegroundWindow(hWnd)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Restore and repaint<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If IsIconic(hWnd) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call ShowWindow(hWnd, SW_RESTORE)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call ShowWindow(hWnd, SW_SHOW)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' SetForegroundWindow return accurately reflects<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' success.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForceForegroundWindow = CBool(r)<br>&nbsp;&nbsp;&nbsp; End If<br>End Function<br><br>Then, in your form:<br><br>Private Sub TrayIcon1_Click()<br>&nbsp;&nbsp;&nbsp; On Error Resume Next<br><br>&nbsp;&nbsp;&nbsp; If mblnMinimized Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' We are minimized to the tray<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.TrayIcon1.MaximizeFromTray Me.hWnd&nbsp;&nbsp; ' Restore<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.WindowState = vbMaximized&nbsp;&nbsp;&nbsp; ' Maximize<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mblnMinimized = False&nbsp;&nbsp; ' Clear the minimized flag<br>&nbsp;&nbsp;&nbsp; Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' We are *not* minimized to the tray<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If IsForegroundTaskWindow(Me.hWnd) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Our window is most likely the foreground task window,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.TrayIcon1.MinimizeToTray Me.hWnd ' so minimize it to the tray<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mblnMinimized = True&nbsp;&nbsp;&nbsp; ' Flag that we are indeed minimized to the tray<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Our window is most likely not the foreground task window<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForceForegroundWindow Me.hWnd&nbsp;&nbsp; ' So bring our window to the front<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp; End If<br>End Sub<br><br><br>Private Sub Form_Resize()<br>&nbsp;&nbsp;&nbsp; On Error Resume Next<br><br>&nbsp;&nbsp;&nbsp; If Me.WindowState = vbMinimized Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call TrayIcon1_Click<br>&nbsp;&nbsp;&nbsp; End If<br>End Sub<br><br>For the record, I also set the ShowInTaskbar property to False for my window. This allows it to only appear in the system tray. Hope this helps someone (and thanks for your help JKDev)!<br>]]>
   </description>
   <pubDate>Mon, 21 Jan 2008 11:47:46 +0000</pubDate>
   <guid isPermaLink="true">http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30356&amp;title=trayicon-getforegroundwindow-problem#30356</guid>
  </item> 
  <item>
   <title><![CDATA[TrayIcon GetForegroundWindow Problem : I have, unfortunately the events...]]></title>
   <link>http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30353&amp;title=trayicon-getforegroundwindow-problem#30353</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://forum.codejock.com/member_profile.asp?PF=2676">jpbro</a><br /><strong>Subject:</strong> 9357<br /><strong>Posted:</strong> 21 January 2008 at 11:06am<br /><br />I have, unfortunately the events are out of order so the Shell_TrayWnd class grabs the focus before any events fire. Most controls fire events in the MouseDown, MouseUp, Click order, but the TrayIcon control fires Click, MouseDown, MouseUp.<br><br>I have just about found a solution though - the second part of the code you sent has a task list example. What I am doing now is checking the task list on click to see if my app is the first in the list, and if so, assuming it is the foreground window. Seems to be working so far...I'll test it some more, clean up the code and then post it here if it is working.<br><br>Thanks.<br>]]>
   </description>
   <pubDate>Mon, 21 Jan 2008 11:06:27 +0000</pubDate>
   <guid isPermaLink="true">http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30353&amp;title=trayicon-getforegroundwindow-problem#30353</guid>
  </item> 
  <item>
   <title><![CDATA[TrayIcon GetForegroundWindow Problem : have you tried getting the window...]]></title>
   <link>http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30352&amp;title=trayicon-getforegroundwindow-problem#30352</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://forum.codejock.com/member_profile.asp?PF=3347">JKDev</a><br /><strong>Subject:</strong> 9357<br /><strong>Posted:</strong> 21 January 2008 at 10:51am<br /><br />have you tried getting the window in the mouse down or up event instead of the click event?...]]>
   </description>
   <pubDate>Mon, 21 Jan 2008 10:51:53 +0000</pubDate>
   <guid isPermaLink="true">http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30352&amp;title=trayicon-getforegroundwindow-problem#30352</guid>
  </item> 
  <item>
   <title><![CDATA[TrayIcon GetForegroundWindow Problem : Hi JKDev, thanks for the reply.The...]]></title>
   <link>http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30351&amp;title=trayicon-getforegroundwindow-problem#30351</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://forum.codejock.com/member_profile.asp?PF=2676">jpbro</a><br /><strong>Subject:</strong> 9357<br /><strong>Posted:</strong> 21 January 2008 at 10:45am<br /><br />Hi JKDev, thanks for the reply.<br><br>The code you provided does bring the window to the front, the problem I am having though is that I can't figure out a way to determine if my window (or its thread) is the foreground window in the TrayIcon Click event because the Shell_TrayWnd class becomes the foreground window *before* the Click event fires.<br><br>It seems that no matter what window appears to be the foreground window (i.e. an instance of Notepad, or my project), then GetForegroundWindow always returns the hwnd for the Shell_TrayWnd class. This makes it impossible (as far as I have found so far) to perform the actions I require in the Click event (Minimize if foreground, Maximize if minimized to tray, Bring to front if not foreground and not minimized to tray). Any ideas?<br><br>Thanks again.<br>]]>
   </description>
   <pubDate>Mon, 21 Jan 2008 10:45:48 +0000</pubDate>
   <guid isPermaLink="true">http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30351&amp;title=trayicon-getforegroundwindow-problem#30351</guid>
  </item> 
  <item>
   <title><![CDATA[TrayIcon GetForegroundWindow Problem : This works:  http://vb.mvps.org/articles/ap199902.pdf...]]></title>
   <link>http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30348&amp;title=trayicon-getforegroundwindow-problem#30348</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://forum.codejock.com/member_profile.asp?PF=3347">JKDev</a><br /><strong>Subject:</strong> 9357<br /><strong>Posted:</strong> 21 January 2008 at 10:01am<br /><br />This works:<DIV>&nbsp;</DIV><DIV><a href="http://vb.mvps.org/articles/ap199902.pdf" target="_blank">http://vb.mvps.org/articles/ap199902.pdf</A></DIV>]]>
   </description>
   <pubDate>Mon, 21 Jan 2008 10:01:13 +0000</pubDate>
   <guid isPermaLink="true">http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30348&amp;title=trayicon-getforegroundwindow-problem#30348</guid>
  </item> 
  <item>
   <title><![CDATA[TrayIcon GetForegroundWindow Problem : Hi,I&amp;#039;ll describe the behaviour...]]></title>
   <link>http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30301&amp;title=trayicon-getforegroundwindow-problem#30301</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://forum.codejock.com/member_profile.asp?PF=2676">jpbro</a><br /><strong>Subject:</strong> 9357<br /><strong>Posted:</strong> 19 January 2008 at 6:12pm<br /><br />Hi,<br><br>I'll describe the behaviour I'm trying to achieve:<br><br>1) User clicks TrayIcon and my form is the foreground window -&gt; MinimizeToTray(Me.hWnd)<br>2) User clicks TrayIcon and my form is *not* the foreground window -&gt; SetForegroundWindow(Me.hWnd)<br>3) User clicks TrayIcon and my form is minimized to tray -&gt; MaximizeFromTray(Me.hWnd)<br><br>However, when using GetForegroundWindow in the TrayIcon Click event, the hWnd for my form is never returned (I believe the taskbar hwnd always gets returned). Any body have any idea on a good way to go about this?<br><br>Thanks in advance.<br><br>]]>
   </description>
   <pubDate>Sat, 19 Jan 2008 18:12:44 +0000</pubDate>
   <guid isPermaLink="true">http://forum.codejock.com/forum_posts.asp?TID=9357&amp;PID=30301&amp;title=trayicon-getforegroundwindow-problem#30301</guid>
  </item> 
 </channel>
</rss>