// Project: XnaShooter, File: WindowsHelper.cs
// Namespace: XnaShooter.Helpers, Class: WindowsHelper
// Path: C:\code\XnaShooter\Helpers, Author: Abi
// Code lines: 11, Size of file: 205 Bytes
// Creation date: 01.11.2005 19:43
// Last modified: 11.12.2005 14:11
// Generated with Commenter by abi.exDream.com
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
#if !XBOX360
using System.Security.Principal;
#endif
using System.Globalization;
#endregion
namespace XnaShooter.Helpers
{
///
/// Windows helper
///
class WindowsHelper
{
#region NativeMethods helper class (required for FxCop checks)
#if DEBUG && !XBOX360
///
/// NativeMethods helper class (required for FxCop checks)
///
internal class NativeMethods
{
///
/// Don't allow instantiating this class.
///
private NativeMethods()
{
} // NativeMethods()
///
/// Win32 function: Get foreground window handle (hwnd)
///
[DllImport("User32.dll")]
internal static extern int GetForegroundWindow();
///
/// Win32 function: Set foreground window handle
///
[DllImport("User32.dll")]
internal static extern int SetForegroundWindow(int hWnd);
///
/// Win 32 function: Get window thread process id
///
/// Window handle
/// Process id
/// Thread id
[DllImport("user32.dll")]
internal static extern int GetWindowThreadProcessId(
int window, int processId);
///
/// Win32 function: Attaches input to a thread for controlling windows
///
[DllImport("User32.dll")]
internal static extern int AttachThreadInput(
int idAttach, int idAttachTo, int fAttach);
///
/// Win32 function: Checks if a window is iconic (on the task bar)
///
[DllImport("User32.dll")]
internal static extern int IsIconic(int hWnd);
///
/// Win32 function: Show window with nCmdShow parameters.
///
[DllImport("User32.dll")]
internal static extern int ShowWindow(int hWnd, int nCmdShow);
///
/// Win32 function: Get device context of window handle (hwnd).
///
[DllImport("User32.dll")]
internal static extern IntPtr GetWindowDC(IntPtr hwnd);
} // class NativeMethods
#endif
#endregion
#region Constructor
///
/// Don't allow instantiating this class.
///
private WindowsHelper()
{
} // WindowsHelper()
#endregion
#region Get and set foreground window
#if DEBUG && !XBOX360
// Only supported in debug mode and in windows
///
/// Win32 constants
///
public const int
SwShow = 5,
SwRestore = 9;
///
/// Helper function to force a window into foreground, will
/// even work if our process is not the same as the other windows.
/// We will try to get the others thread input process and
/// attach it to our thread for setting the window to the foreground.
///
public static bool ForceForegroundWindow(int hWnd)
{
int foregroundWnd = NativeMethods.GetForegroundWindow();
// Do nothing if already in foreground.
if (hWnd == foregroundWnd)
return true;
// First need to get the thread responsible for this window,
// and the thread for the foreground window.
int ret = 0,
threadID1 = NativeMethods.GetWindowThreadProcessId(foregroundWnd, 0),
threadID2 = NativeMethods.GetWindowThreadProcessId(hWnd, 0);
// By sharing input state, threads share their concept of
// the active window.
if (threadID1 != threadID2)
{
NativeMethods.AttachThreadInput(threadID1, threadID2, 1);//true
ret = NativeMethods.SetForegroundWindow(hWnd);
NativeMethods.AttachThreadInput(threadID1, threadID2, 0);//false
} // if (threadID1)
else
ret = NativeMethods.SetForegroundWindow(hWnd);
// Restore and repaint
if (NativeMethods.IsIconic(hWnd) != 0)
NativeMethods.ShowWindow(hWnd, SwRestore);
else
NativeMethods.ShowWindow(hWnd, SwShow);
// Succeeded
return ret != 0;
} // ForceForegroundWindow(hWnd)
#endif
#endregion
#region Get default player name
///
/// Get default player name from windows identity, and if
/// this failes use computer name!
///
static public string GetDefaultPlayerName()
{
string defaultPlayerName = "Player";
#if !XBOX360
try
{
defaultPlayerName = WindowsIdentity.GetCurrent().Name;
if (String.IsNullOrEmpty(defaultPlayerName))
{
defaultPlayerName = "Player";
} // if (defaultPlayerName)
else
{
// Windows will return name in format \,
// we just want the username!
string[] nameInfo = defaultPlayerName.Split(
new char[] { '\\' }, 2);
if (nameInfo.Length >= 2)
{
defaultPlayerName = nameInfo[1];
} // if
} // else
} // try
catch { } // Ignore any error
#endif
return defaultPlayerName;
} // GetDefaultPlayerName()
#endregion
} // class WindowsHelper
} // namespace XnaShooter.Helpers