[Roam-commit] SF.net SVN: roam: [82] Trunk/Source
Status: Alpha
Brought to you by:
jonfroehlich
|
From: <jon...@us...> - 2007-07-16 07:03:19
|
Revision: 82
http://roam.svn.sourceforge.net/roam/?rev=82&view=rev
Author: jonfroehlich
Date: 2007-07-16 00:03:17 -0700 (Mon, 16 Jul 2007)
Log Message:
-----------
tested out ability to switch to any open application with a valid hwnd (see EnumWindowsTest which can automatically switch to Internet Explorer, if Internet Explorer is open)
Modified Paths:
--------------
Trunk/Source/Roam.Common/Utils/WindowUtils.cs
Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.Designer.cs
Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.cs
Trunk/Source/Roam.suo
Modified: Trunk/Source/Roam.Common/Utils/WindowUtils.cs
===================================================================
--- Trunk/Source/Roam.Common/Utils/WindowUtils.cs 2007-07-16 06:41:26 UTC (rev 81)
+++ Trunk/Source/Roam.Common/Utils/WindowUtils.cs 2007-07-16 07:03:17 UTC (rev 82)
@@ -30,6 +30,8 @@
/// <author>Jon Froehlich</author>
public static class WindowUtils
{
+ public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
+
/// <summary>
/// The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window,
/// in turn, to an application-defined callback function. EnumWindows continues until the last top-level window
@@ -49,8 +51,35 @@
/// referencing a handle to a window that has been destroyed.
/// </remarks>
[DllImport("coredll.dll", SetLastError = true)]
- public static extern int EnumWindows(IntPtr lpEnumFunc, uint lParam);
+ private static extern int EnumWindows(IntPtr lpEnumFunc, uint lParam);
+ public static int EnumWindows(EnumWindowsProc enumWindowsProc, uint lParam)
+ {
+ IntPtr callbackDelegatePointer = Marshal.GetFunctionPointerForDelegate(enumWindowsProc);
+ return EnumWindows(callbackDelegatePointer, lParam);
+ }
+
+ public static int EnumWindows(EnumWindowsProc enumWindowsProc)
+ {
+ return EnumWindows(enumWindowsProc, 0);
+ }
+
+ /// <summary>
+ /// The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates
+ /// the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system
+ /// assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.
+ /// </summary>
+ /// <param name="hWnd">[in] Handle to the window that should be activated and brought to the foreground. </param>
+ /// <returns>If the window was brought to the foreground, the return value is nonzero. If the window was not brought to the foreground,
+ /// the return value is zero.
+ /// </returns>
+ /// <remarks>
+ /// The foreground window is the window at the top of the Z-Order. It is the window that the user is working with. In a preemptive
+ /// multitasking environment, you should generally let the user control which window is the foreground window.
+ /// </remarks>
+ [DllImport("coredll.dll", SetLastError = true)]
+ public static extern bool SetForegroundWindow(IntPtr hWnd);
+
public static bool ContainsHandle(IntPtr handle, Control control)
{
if (control.Handle == handle)
Modified: Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.Designer.cs
===================================================================
--- Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.Designer.cs 2007-07-16 06:41:26 UTC (rev 81)
+++ Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.Designer.cs 2007-07-16 07:03:17 UTC (rev 82)
@@ -33,9 +33,10 @@
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
+ this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this._scrollableLabel = new Roam.UI.Controls.ScrollableLabel();
- this.menuItem5 = new System.Windows.Forms.MenuItem();
+ this.menuItem6 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// mainMenu1
@@ -52,6 +53,7 @@
//
this.menuItem2.MenuItems.Add(this.menuItem3);
this.menuItem2.MenuItems.Add(this.menuItem5);
+ this.menuItem2.MenuItems.Add(this.menuItem6);
this.menuItem2.MenuItems.Add(this.menuItem4);
this.menuItem2.Text = "Menu";
//
@@ -60,6 +62,11 @@
this.menuItem3.Text = "Enum Windows";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
+ // menuItem5
+ //
+ this.menuItem5.Text = "Foreground";
+ this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
+ //
// menuItem4
//
this.menuItem4.Text = "Clear";
@@ -74,10 +81,11 @@
this._scrollableLabel.Size = new System.Drawing.Size(240, 266);
this._scrollableLabel.TabIndex = 1;
//
- // menuItem5
+ // menuItem6
//
- this.menuItem5.Text = "Foreground";
- this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
+ this.menuItem6.Enabled = false;
+ this.menuItem6.Text = "Switch to Internet Explorer";
+ this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// EnumWindowsTest
//
@@ -101,5 +109,6 @@
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
+ private System.Windows.Forms.MenuItem menuItem6;
}
}
\ No newline at end of file
Modified: Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.cs
===================================================================
--- Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.cs 2007-07-16 06:41:26 UTC (rev 81)
+++ Trunk/Source/Roam.Test/Sandbox/EnumWindowsTest.cs 2007-07-16 07:03:17 UTC (rev 82)
@@ -12,28 +12,30 @@
{
public partial class EnumWindowsTest : Form
{
- EnumWindowsProc callbackDelegate;
- IntPtr callbackDelegatePointer;
+ private WindowUtils.EnumWindowsProc _callbackDelegate;
- [DllImport("coredll.dll", SetLastError = true)]
- public static extern bool EnumWindows(IntPtr lpEnumFunc, uint lParam);
- public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
+ //just to test out the app switch
+ private IntPtr _hwndInternetExplorer = IntPtr.Zero;
public EnumWindowsTest()
{
InitializeComponent();
_scrollableLabel.MaxLines = 200;
- callbackDelegate = new EnumWindowsProc(EnumWindowsCallbackProc);
- callbackDelegatePointer = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
+ _callbackDelegate = new WindowUtils.EnumWindowsProc(EnumWindowsCallbackProc);
}
public int EnumWindowsCallbackProc(IntPtr hwnd, IntPtr lParam)
{
//System.Diagnostics.Debug.WriteLine("Window: " + hwnd.ToString());
- //System.Diagnostics.Debug.WriteLine("Window: " + hwnd.ToString());
- _scrollableLabel.AddLine("hWnd: {0} title: {1}", hwnd, WindowUtils.GetWindowText(hwnd));
- //StringBuilder sb = new StringBuilder();
- //_scrollableLabel.AddLine(string.Format("{0} {1}", hwnd.ToString(), WindowUtils.GetWindowText(hwnd, sb, int.MaxValue)));
+ string windowText = WindowUtils.GetWindowText(hwnd);
+ _scrollableLabel.AddLine("hWnd: {0} title: {1}", hwnd, windowText);
+
+ if (windowText.IndexOf("Internet") != -1)
+ {
+ _hwndInternetExplorer = hwnd;
+ menuItem6.Enabled = true;
+ }
+
return 1;
}
@@ -44,7 +46,7 @@
private void menuItem3_Click(object sender, EventArgs e)
{
- EnumWindows(callbackDelegatePointer, 0);
+ WindowUtils.EnumWindows(_callbackDelegate);
}
private void menuItem4_Click(object sender, EventArgs e)
@@ -63,61 +65,13 @@
_scrollableLabel.AddLine("Foreground Child: {0} title: {1}", childOfForeground, WindowUtils.GetWindowText(childOfForeground));
}
}
- }
- //public partial class EnumWindowsTest : Form
- //{
- // /// <summary>
- // /// The EnumWindowsProc function is an application-defined callback function used with the EnumWindows
- // /// or EnumDesktopWindows function. It receives top-level window handles. The WNDENUMPROC type defines
- // /// a pointer to this callback function. EnumWindowsProc is a placeholder for the application-defined
- // /// function name.
- // /// </summary>
- // /// <param name="hwnd">[in] Handle to a top-level window. </param>
- // /// <param name="lParam">[in] Specifies the application-defined value given in EnumWindows or EnumDesktopWindows. </param>
- // /// <returns>To continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE. </returns>
- // public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
-
- // private EnumWindowsProc _callbackDelegate;
- // private IntPtr _callbackDelegatePointer;
-
- // [DllImport("coredll.dll", SetLastError = true)]
- // public static extern int EnumWindows(IntPtr lpEnumFunc, uint lParam);
-
- // public EnumWindowsTest()
- // {
- // InitializeComponent();
- // _callbackDelegate = new EnumWindowsProc(EnumWindowsCallbackProc);
- // _callbackDelegatePointer = Marshal.GetFunctionPointerForDelegate(_callbackDelegate);
- // }
-
- // private void menuItem1_Click(object sender, EventArgs e)
- // {
- // this.Close();
- // }
-
- // /// <summary>
- // ///
- // /// </summary>
- // /// <param name="hwnd"></param>
- // /// <param name="lParam"></param>
- // /// <returns>To continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE. </returns>
- // public int EnumWindowsCallbackProc(IntPtr hwnd, IntPtr lParam)
- // {
- // System.Diagnostics.Debug.WriteLine("Window: " + hwnd.ToString());
- // StringBuilder sb = new StringBuilder();
- // _scrollableLabel.AddLine(string.Format("{0} {1}", hwnd.ToString(), WindowUtils.GetWindowText(hwnd, sb, int.MaxValue)));
- // return 1;
- // }
-
- // private void menuItem3_Click(object sender, EventArgs e)
- // {
- // EnumWindows(_callbackDelegatePointer, 0);
- // }
-
- // private void menuItem4_Click(object sender, EventArgs e)
- // {
- // _scrollableLabel.Clear();
- // }
- //}
+ private void menuItem6_Click(object sender, EventArgs e)
+ {
+ if (_hwndInternetExplorer != IntPtr.Zero)
+ {
+ WindowUtils.SetForegroundWindow(_hwndInternetExplorer);
+ }
+ }
+ }
}
\ No newline at end of file
Modified: Trunk/Source/Roam.suo
===================================================================
(Binary files differ)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|