I encountered a user interface "problem" today. At work I have multiple monitors, and at home I only have the one on my machine (I own Dell 9300, and so it's my desktop at both places). As a result, sometimes when going back and forth between the multiple monitors and my single, I sometimes wind up with an app that'll draw itself OFF the desktop window when I suddenly fire it up on my single monitor (if I open the app, it'll show up, but like on some wacky negative x/y coordinate way off in la la land).
There's a way, apparently, to bring the window BACK onto my visible desktop through Alt-SPACE-M or something like that, but I couldn't get it to work for some odd reason. So I wrote an app that'll simply re-align all my desktop windows to the 0/0 x/y position. It's a rough hack because all I wanted was to get my app to draw its window in a sane position again - so, I wouldn't take the code as-is and use it wherever. It worked for me, and it was an interesting way to step back and realize "Man, I'm a geek".
using System;using System.Collections.Generic;using System.Text;
namespace StraightenWindows{ class Interop { public delegate System.Boolean EnumWindowsProc( System.IntPtr hWnd, System.Int32 lParam); [System.Runtime.InteropServices.DllImport("user32")] public static extern System.Boolean EnumDesktopWindows( System.IntPtr hDesktop, Interop.EnumWindowsProc lpfn, System.Int32 lParam); [System.Runtime.InteropServices.DllImport("user32")] public static extern System.Boolean IsWindow(System.IntPtr hWnd); [System.Runtime.InteropServices.DllImport("user32")] public static extern System.Boolean IsWindowVisible(System.IntPtr hWnd); [System.Runtime.InteropServices.DllImport("user32")] public static extern System.IntPtr GetParent(System.IntPtr hWnd); [System.Runtime.InteropServices.DllImport("user32")] public static extern System.Int32 GetWindowText( System.IntPtr hWnd, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr, SizeConst = 255)] System.Text.StringBuilder lpString, System.Int32 nMaxCount); [System.Runtime.InteropServices.DllImport("user32")] public static extern System.Boolean MoveWindow( System.IntPtr hWnd, System.Int32 x, System.Int32 y, System.Int32 nWidth, System.Int32 nHeight, System.Boolean bRepaint); } class Program { static System.Boolean EnumWindowsCallback(System.IntPtr hWnd, System.Int32 lParam) { // verify it's a window and it's visible if (Interop.IsWindow(hWnd) && Interop.IsWindowVisible(hWnd)) { // verify it's a parent window if (Interop.GetParent(hWnd) == System.IntPtr.Zero) { // the get parent can also return zero on error, so check for that condition. if (System.Runtime.InteropServices.Marshal.GetLastWin32Error() == 0) { System.Text.StringBuilder sb = new StringBuilder(255); if (Interop.GetWindowText(hWnd, sb, 255) > 0) { Console.WriteLine("Window " + hWnd.ToString() + " has text '" + sb.ToString() + "'"); Console.WriteLine(); Console.WriteLine(); // fix Interop.MoveWindow( hWnd, 0, 0, 500, 500, true); } } } }
return true; } static void Main(string[] args) { Interop.EnumWindowsProc p = new Interop.EnumWindowsProc(EnumWindowsCallback); Interop.EnumDesktopWindows(System.IntPtr.Zero, // use current desktop p, 0); Console.Write("Press any key to continue..."); Console.ReadKey(); return; } }}
Remember Me
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.