éå»ã2åã«ããã£ã¦ã¢ã¯ãã£ãã«ããããã«ãããããã£ã¦ã¾ããããç¥ãåãã«æãã¦ããã£ãããæ¹ã§
ã¾ã¨ã¾ãã¾ãããããã ã¨ã確å®ã«å¯¾è±¡ã¨ãªãããã»ã¹ãã¢ã¯ãã£ãã«ãªãã¯ãã§ãã
以ä¸ããµã³ãã«ã³ã¼ãã§ãã
// vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { ////////////////////////////////////////////////////////////////// // // 以ä¸Win32Apiã®å®£è¨. // // [DllImport("user32.dll", SetLastError = true)] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] static extern bool BringWindowToTop(IntPtr hWnd); [DllImport("user32.dll")] extern static bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true)] extern static bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); /// <summary> /// å¼·å¶çã«ã¦ã£ã³ãã¦ãã¢ã¯ãã£ãã«ããéã«å©ç¨ãããç¬èªã¦ã£ã³ãã¦ã¡ãã»ã¼ã¸ (WM_USERã®å¤ã«1å ç®) /// </summary> const int MY_FORCE_FOREGROUND_MESSAGE = 0x400 + 1; /// <summary> /// ã³ã³ã¹ãã©ã¯ã¿. /// </summary> public Form1() { InitializeComponent(); // // ãã¹ãç¨ã«10ç§éå¾ æ©ããã // Thread.Sleep(10000); } /// <summary> /// ãã©ã¼ã ã®ãã¼ãæã«ã³ã¼ã«ããã¯ããã¾ãã /// </summary> /// <param name="sender">ã¤ãã³ãéä¿¡å ãªãã¸ã§ã¯ã</param> /// <param name="e">ã¤ãã³ãå¼æ°ãªãã¸ã§ã¯ã</param> private void Form1_Load(object sender, EventArgs e) { // // ã¡ãã»ã¼ã¸ãæãã¦ããã©ã¼ã ã®ãã¼ãå¦çãå®äºæ¬¡ç¬¬ãã¦ã£ã³ãã¦ãã¢ã¯ãã£ãã«ãªãããã«ããã // PostMessage(new HandleRef(this, Handle), MY_FORCE_FOREGROUND_MESSAGE, (IntPtr)0, (IntPtr)0); } /// <summary> /// æå®ããããã³ãã«ãæã¤ã¦ã£ã³ãã¦ãå¼·å¶çã«ã¢ã¯ãã£ãã«ãã¾ãã /// </summary> /// <param name="targetHandle">対象ã¨ãªãã¦ã£ã³ãã¦ãã³ãã«ãªãã¸ã§ã¯ã</param> protected void SetForceForegroundWindow(IntPtr targetHandle){ uint nullProcessId = 0; // ã¿ã¼ã²ããã¨ãªããã³ãã«ã®ã¹ã¬ããIDãåå¾. uint targetThreadId = GetWindowThreadProcessId(targetHandle, out nullProcessId); // ç¾å¨ã¢ã¯ãã£ãã¨ãªã£ã¦ããã¦ã£ã³ãã¦ã®ã¹ã¬ããIDãåå¾. uint currentActiveThreadId = GetWindowThreadProcessId(GetForegroundWindow(), out nullProcessId); //////////////////////////////////////////////// // // ã¢ã¯ãã£ãå¦ç // // SetForegroundWindow(targetHandle); if(targetThreadId == currentActiveThreadId){ // // ç¾å¨ã¢ã¯ãã£ããªã®ãèªåã®å ´åã¯åé¢ã«æã£ã¦ããã // BringWindowToTop(targetHandle); }else{ // // å¥ã®ããã»ã¹ãã¢ã¯ãã£ããªå ´åã¯ããã®ããã»ã¹ã«ã¢ã¿ããããå ¥åã奪ã. // AttachThreadInput(targetThreadId, currentActiveThreadId, true); try { // // èªåãåé¢ã«æã£ã¦ããã // BringWindowToTop(targetHandle); } finally { // // ã¢ã¿ããã解é¤. // AttachThreadInput(targetThreadId, currentActiveThreadId, false); } } } protected override void WndProc(ref Message m) { if(m.Msg == MY_FORCE_FOREGROUND_MESSAGE){ // // ã¦ã£ã³ãã¦ãå¼·å¶çã«ã¢ã¯ãã£ãã«ããã // SetForceForegroundWindow(m.HWnd); // // ãã®å¾ã®ã¦ã£ã³ãã¦ã¡ãã»ã¼ã¸ã®å¦çã¯å¿ è¦ãªãã®ã§ãå¦çãæ»ãã // return; } // // ãã以å¤ã®å ´åã¯ãé常éãã¡ãã»ã¼ã¸å¦çãè¡ã£ã¦ãããã // base.WndProc(ref m); } } }
ä¸è¨ã®ãµã³ãã«ã§ã¯ãã³ã³ã¹ãã©ã¯ãæã«10ç§éã®Sleepãããã¦ããã¾ãã
ãã®å¾ããã©ã¼ã ãã¼ãæã«PostMessageã使ç¨ãã¦ç¬èªã¡ãã»ã¼ã¸ãéä¿¡ãã¦ãã¾ãã
ãã®ããã«ããã¨ããã¼ãå¦çã確å®ã«å®äºããå¾ã§ã¢ã¯ãã£ãå¦çãè¡ãäºãåºæ¥ã¾ãã
(ã¡ãã»ã¼ã¸ã¯ãã¥ã¼ã«æºã¾ã£ã¦ãããé ã«å¦çããããã)
ã¢ããªãèµ·åããããããªã¦ã£ã³ãã¦ãéãã¦ãã£ã¦ã¿ã¦ãã ããã
10ç§ãã£ãå¾ã«ãã¦ã£ã³ãã¦ãæåé¢ã§ã¢ã¯ãã£ããªç¶æ
ã§è¡¨ç¤ºãããã¯ãã§ãã
ã¡ãªã¿ã«éå»äºåã®åã¯ã以ä¸ããè¦ãã¾ãã
- ç¹å®ã®ããã»ã¹ãã¢ã¯ãã£ãã«ãã
- ç¹å®ã®ããã»ã¹ãã¢ã¯ãã£ãã«ãã-2