OSDN Git Service

TabControlにメイリオフォントを指定するとタブの最小幅が広くなる問題の対策 (thx @5px!)
[opentween/open-tween.git] / OpenTween / Win32Api.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
11 // 
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 // 
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
20 // for more details. 
21 // 
22 // You should have received a copy of the GNU General public License along
23 // with this program. if not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
28 using System.Diagnostics;
29 using System.Linq;
30 using System.Net;
31 using System.Runtime.InteropServices;
32 using System.Threading;
33 using System.Windows.Forms;
34
35 namespace OpenTween
36 {
37     public static class Win32Api
38     {
39         #region "先行起動プロセスをアクティブにする"
40         // 外部プロセスのウィンドウを起動する
41         public static void WakeupWindow(IntPtr hWnd)
42         {
43             // メイン・ウィンドウが最小化されていれば元に戻す
44             if (IsIconic(hWnd))
45             {
46                 ShowWindowAsync(hWnd, SW_RESTORE);
47             }
48
49             // メイン・ウィンドウを最前面に表示する
50             SetForegroundWindow(hWnd);
51         }
52
53         // 外部プロセスのメイン・ウィンドウを起動するためのWin32 API
54         [DllImport("user32.dll")]
55         private extern static bool SetForegroundWindow(
56             IntPtr hWnd);
57
58         // ウィンドウの表示状態を設定
59         [DllImport("user32.dll")]
60         private extern static bool ShowWindowAsync(
61             IntPtr hWnd,
62             int nCmdShow);
63
64         // 指定されたウィンドウが最小化( アイコン化)されているかどうかを調べる
65         [DllImport("user32.dll")]
66         private extern static bool IsIconic(
67             IntPtr hWnd);
68
69         // ShowWindowAsync関数のパラメータに渡す定義値
70         private const int SW_RESTORE = 9; // 画面を元の大きさに戻す
71
72         // 実行中の同じアプリケーションのプロセスを取得する
73         public static Process GetPreviousProcess()
74         {
75             var curProcess = Process.GetCurrentProcess();
76             try
77             {
78                 return Process.GetProcessesByName(curProcess.ProcessName)
79                     .Where(p => p.Id != curProcess.Id)
80                     .FirstOrDefault(p => string.Compare(p.MainModule.FileName, curProcess.MainModule.FileName, StringComparison.OrdinalIgnoreCase) == 0);
81             }
82             catch
83             {
84                 return null;
85             }
86         }
87         #endregion
88
89         #region "タスクトレイアイコンのクリック"
90         // 指定されたクラス名およびウィンドウ名と一致するトップレベルウィンドウのハンドルを取得します
91         [DllImport("user32.dll", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
92         private extern static IntPtr FindWindow(
93             [MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
94             [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
95
96         // 指定された文字列と一致するクラス名とウィンドウ名文字列を持つウィンドウのハンドルを返します
97         [DllImport("user32.dll", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
98         private extern static IntPtr FindWindowEx(
99             IntPtr hWnd1,
100             IntPtr hWnd2,
101             [MarshalAs(UnmanagedType.LPTStr)] string lpsz1,
102             [MarshalAs(UnmanagedType.LPTStr)] string lpsz2);
103
104         // 指定されたウィンドウへ、指定されたメッセージを送信します
105         [DllImport("user32.dll")]
106         private extern static IntPtr SendMessage(
107             IntPtr hwnd,
108             int wMsg,
109             IntPtr wParam,
110             IntPtr lParam);
111
112         // SendMessageで送信するメッセージ
113         private enum Sm_Message : int
114         {
115             WM_USER = 0x400,                     //ユーザー定義メッセージ
116             TB_GETBUTTON = WM_USER + 23,         //ツールバーのボタン取得
117             TB_BUTTONCOUNT = WM_USER + 24,       //ツールバーのボタン(アイコン)数取得
118             TB_GETBUTTONINFO = WM_USER + 65,     //ツールバーのボタン詳細情報取得
119
120             TCM_FIRST = 0x1300,                  //タブコントロールメッセージ
121             TCM_SETMINTABWIDTH = TCM_FIRST + 49, //タブアイテムの最小幅を設定
122         }
123         // ツールバーボタン構造体
124         [StructLayout(LayoutKind.Sequential, Pack = 1)]
125         private struct TBBUTTON
126         {
127             public int iBitmap;
128             public IntPtr idCommand;
129             public byte fsState;
130             public byte fsStyle;
131             public byte bReserved0;
132             public byte bReserved1;
133             public int dwData;
134             public int iString;
135         }
136         // ツールバーボタン詳細情報構造体
137         [StructLayout(LayoutKind.Sequential)]
138         private struct TBBUTTONINFO
139         {
140             public Int32 cbSize;
141             public Int32 dwMask;
142             public Int32 idCommand;
143             public Int32 iImage;
144             public Byte fsState;
145             public Byte fsStyle;
146             public short cx;
147             public IntPtr lParam;
148             public IntPtr pszText;
149             public Int32 cchText;
150         }
151         // TBBUTTONINFOのlParamでポイントされるアイコン情報(PostMessageで使用)
152         [StructLayout(LayoutKind.Sequential)]
153         private struct TRAYNOTIFY
154         {
155             public IntPtr hWnd;
156             public UInt32 uID;
157             public UInt32 uCallbackMessage;
158             public UInt32 dwDummy1;
159             public UInt32 dwDummy2;
160             public IntPtr hIcon;
161         }
162         // TBBUTTONINFOに指定するマスク情報
163         [Flags()]
164         private enum ToolbarButtonMask : int
165         {
166             TBIF_COMMAND = 0x20,
167             TBIF_LPARAM = 0x10,
168             TBIF_TEXT = 0x2,
169         }
170         // 指定されたウィンドウを作成したスレッドの ID を取得します
171         [DllImport("user32.dll", SetLastError = true)]
172         private static extern int GetWindowThreadProcessId(
173             IntPtr hwnd,
174             out int lpdwProcessId);
175
176         // 指定したプロセスIDに対するプロセスハンドルを取得します
177         [DllImport("kernel32.dll")]
178         private static extern IntPtr OpenProcess(
179             ProcessAccess dwDesiredAccess,
180             [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
181             int dwProcessId);
182
183         // OpenProcessで指定するアクセス権
184         [Flags()]
185         private enum ProcessAccess : int
186         {
187             /// <summary>Specifies all possible access flags for the process object.</summary>
188             AllAccess = CreateThread | DuplicateHandle | QueryInformation | SetInformation | Terminate | VMOperation | VMRead | VMWrite | Synchronize,
189             /// <summary>Enables usage of the process handle in the CreateRemoteThread function to create a thread in the process.</summary>
190             CreateThread = 0x2,
191             /// <summary>Enables usage of the process handle as either the source or target process in the DuplicateHandle function to duplicate a handle.</summary>
192             DuplicateHandle = 0x40,
193             /// <summary>Enables usage of the process handle in the GetExitCodeProcess and GetPriorityClass functions to read information from the process object.</summary>
194             QueryInformation = 0x400,
195             /// <summary>Enables usage of the process handle in the SetPriorityClass function to set the priority class of the process.</summary>
196             SetInformation = 0x200,
197             /// <summary>Enables usage of the process handle in the TerminateProcess function to terminate the process.</summary>
198             Terminate = 0x1,
199             /// <summary>Enables usage of the process handle in the VirtualProtectEx and WriteProcessMemory functions to modify the virtual memory of the process.</summary>
200             VMOperation = 0x8,
201             /// <summary>Enables usage of the process handle in the ReadProcessMemory function to read from the virtual memory of the process.</summary>
202             VMRead = 0x10,
203             /// <summary>Enables usage of the process handle in the WriteProcessMemory function to write to the virtual memory of the process.</summary>
204             VMWrite = 0x20,
205             /// <summary>Enables usage of the process handle in any of the wait functions to wait for the process to terminate.</summary>
206             Synchronize = 0x100000,
207         }
208         // 指定したプロセスの仮想アドレス空間にメモリ領域を確保
209         [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
210         private static extern IntPtr VirtualAllocEx(
211             IntPtr hProcess,
212             IntPtr lpAddress,
213             IntPtr dwSize,
214             AllocationTypes flAllocationType,
215             MemoryProtectionTypes flProtect);
216
217         // アロケート種類
218         [Flags()]
219         private enum AllocationTypes : uint
220         {
221             Commit = 0x1000,
222             Reserve = 0x2000,
223             Decommit = 0x4000,
224             Release = 0x8000,
225             Reset = 0x80000,
226             Physical = 0x400000,
227             TopDown = 0x100000,
228             WriteWatch = 0x200000,
229             LargePages = 0x20000000,
230         }
231         // アロケートしたメモリに対する保護レベル
232         [Flags()]
233         private enum MemoryProtectionTypes : uint
234         {
235             Execute = 0x10,
236             ExecuteRead = 0x20,
237             ExecuteReadWrite = 0x40,
238             ExecuteWriteCopy = 0x80,
239             NoAccess = 0x1,
240             ReadOnly = 0x2,
241             ReadWrite = 0x4,
242             WriteCopy = 0x8,
243             GuardModifierflag = 0x100,
244             NoCacheModifierflag = 0x200,
245             WriteCombineModifierflag = 0x400,
246         }
247         // オープンしているカーネルオブジェクトのハンドルをクローズします
248         [DllImport("kernel32.dll", SetLastError = true)]
249         private static extern bool CloseHandle(IntPtr hHandle);
250
251         // 指定されたプロセスの仮想アドレス空間内のメモリ領域を解放またはコミット解除します
252         [DllImport("kernel32.dll")]
253         private static extern bool VirtualFreeEx(
254             IntPtr hProcess,
255             IntPtr lpAddress,
256             IntPtr dwSize,
257             MemoryFreeTypes dwFreeType);
258
259         // メモリ解放種別
260         [Flags()]
261         private enum MemoryFreeTypes
262         {
263             Release = 0x8000,
264         }
265         //指定したプロセスのメモリ領域にデータをコピーする
266         [DllImport("kernel32.dll", SetLastError = true)]
267         private static extern bool WriteProcessMemory(
268             IntPtr hProcess,
269             IntPtr lpBaseAddress,
270             ref TBBUTTONINFO lpBuffer,
271             IntPtr nSize,
272             out int lpNumberOfBytesWritten);
273
274         //指定したプロセスのメモリ領域のデータを呼び出し側プロセスのバッファにコピーする
275         [DllImport("kernel32.dll", SetLastError = true)]
276         private static extern bool ReadProcessMemory(
277             IntPtr hProcess,
278             IntPtr lpBaseAddress,
279             IntPtr lpBuffer,
280             IntPtr nSize,
281             out int lpNumberOfBytesRead);
282
283         //メッセージをウィンドウのメッセージ キューに置き、対応するウィンドウがメッセージを処理するのを待たずに戻ります
284         [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
285         private static extern bool PostMessage(
286             IntPtr hWnd,
287             uint Msg,
288             IntPtr wParam,
289             IntPtr lParam);
290
291         //PostMessageで送信するメッセージ
292         private enum PM_Message : uint
293         {
294             WM_LBUTTONDOWN = 0x201,      //左マウスボタン押し下げ
295             WM_LBUTTONUP = 0x202,        //左マウスボタン離し
296         }
297
298         /// <summary>
299         /// タブコントロールのアイテムの最小幅を設定します
300         /// </summary>
301         /// <param name="tabControl">対象となるタブコントロール</param>
302         /// <param name="width">アイテムの最小幅。-1 を指定するとデフォルトの幅が使用されます</param>
303         /// <returns>設定前の最小幅</returns>
304         public static int SetMinTabWidth(TabControl tabControl, int width)
305         {
306             return (int)SendMessage(tabControl.Handle, (int)Sm_Message.TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)width);
307         }
308
309         //タスクトレイアイコンのクリック処理
310         public static bool ClickTasktrayIcon(string tooltip)
311         {
312             const string TRAY_WINDOW = "Shell_TrayWnd";
313             const string TRAY_NOTIFYWINDOW = "TrayNotifyWnd";
314             const string TRAY_PAGER = "SysPager";
315             const string TOOLBAR_CONTROL = "ToolbarWindow32";
316             //タスクバーのハンドル取得
317             var taskbarWin = FindWindow(TRAY_WINDOW, null);
318             if (taskbarWin.Equals(IntPtr.Zero)) return false;
319             //通知領域のハンドル取得
320             var trayWin = FindWindowEx(taskbarWin, IntPtr.Zero, TRAY_NOTIFYWINDOW, null);
321             if (trayWin.Equals(IntPtr.Zero)) return false;
322             //SysPagerの有無確認。(XP/2000はSysPagerあり)
323             var tempWin = FindWindowEx(trayWin, IntPtr.Zero, TRAY_PAGER, null);
324             if (tempWin.Equals(IntPtr.Zero)) tempWin = trayWin;
325             //タスクトレイがツールバーで出来ているか確認
326             // → ツールバーでなければ終了
327             var toolWin = FindWindowEx(tempWin, IntPtr.Zero, TOOLBAR_CONTROL, null);
328             if (toolWin.Equals(IntPtr.Zero)) return false;
329             //タスクトレイのプロセス(Explorer)を取得し、外部から参照するために開く
330             int expPid = 0;
331             GetWindowThreadProcessId(toolWin, out expPid);
332             var hProc = OpenProcess(ProcessAccess.VMOperation | ProcessAccess.VMRead | ProcessAccess.VMWrite, false, expPid);
333             if (hProc.Equals(IntPtr.Zero)) return false;
334
335             //プロセスを閉じるためにtry-finally
336             try
337             {
338                 var tbButtonLocal = new TBBUTTON();   //本プロセス内のタスクバーボタン情報作成(サイズ特定でのみ使用)
339                 //Explorer内のタスクバーボタン格納メモリ確保
340                 var ptbSysButton = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
341                 if (ptbSysButton.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
342                 try
343                 {
344                     var tbButtonInfoLocal = new TBBUTTONINFO();   //本プロセス内ツールバーボタン詳細情報作成
345                     //Explorer内のタスクバーボタン詳細情報格納メモリ確保
346                     var ptbSysInfo = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
347                     if (ptbSysInfo.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
348                     try
349                     {
350                         const int titleSize = 256;    //Tooltip文字列長
351                         var title = "";            //Tooltip文字列
352                         //共有メモリにTooltip読込メモリ確保
353                         var pszTitle = Marshal.AllocCoTaskMem(titleSize);
354                         if (pszTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
355                         try
356                         {
357                             //Explorer内にTooltip読込メモリ確保
358                             var pszSysTitle = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)titleSize, AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
359                             if (pszSysTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
360                             try
361                             {
362                                 //通知領域ボタン数取得
363                                 var iCount = (int)SendMessage(toolWin, (int)Sm_Message.TB_BUTTONCOUNT, new IntPtr(0), new IntPtr(0));
364                                 //左から順に情報取得
365                                 for (var i = 0; i < iCount; i++)
366                                 {
367                                     var dwBytes = 0;  //読み書きバイト数
368                                     var tbButtonLocal2 = new TBBUTTON();  //ボタン情報
369                                     var tbButtonInfoLocal2 = new TBBUTTONINFO();  //ボタン詳細情報
370                                     //共有メモリにボタン情報読込メモリ確保
371                                     var ptrLocal = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonLocal));
372                                     if (ptrLocal.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
373                                     try
374                                     {
375                                         Marshal.StructureToPtr(tbButtonLocal, ptrLocal, true);   //共有メモリ初期化
376                                         //ボタン情報取得(idCommandを取得するため)
377                                         SendMessage(
378                                             toolWin,
379                                             (int)Sm_Message.TB_GETBUTTON,
380                                             new IntPtr(i),
381                                             ptbSysButton);
382                                         //Explorer内のメモリを共有メモリに読み込み
383                                         ReadProcessMemory(
384                                             hProc,
385                                             ptbSysButton,
386                                             ptrLocal,
387                                             (IntPtr)Marshal.SizeOf(tbButtonLocal),
388                                             out dwBytes);
389                                         //共有メモリの内容を構造体に変換
390                                         tbButtonLocal2 = (TBBUTTON)Marshal.PtrToStructure(
391                                                                 ptrLocal,
392                                                                 typeof(TBBUTTON));
393                                     }
394                                     finally
395                                     {
396                                         Marshal.FreeCoTaskMem(ptrLocal); //共有メモリ解放
397                                     }
398
399                                     //ボタン詳細情報を取得するためのマスク等を設定
400                                     tbButtonInfoLocal.cbSize = Marshal.SizeOf(tbButtonInfoLocal);
401                                     tbButtonInfoLocal.dwMask = (int)(ToolbarButtonMask.TBIF_COMMAND | ToolbarButtonMask.TBIF_LPARAM | ToolbarButtonMask.TBIF_TEXT);
402                                     tbButtonInfoLocal.pszText = pszSysTitle;     //Tooltip書き込み先領域
403                                     tbButtonInfoLocal.cchText = titleSize;
404                                     //マスク設定等をExplorerのメモリへ書き込み
405                                     WriteProcessMemory(
406                                         hProc,
407                                         ptbSysInfo,
408                                         ref tbButtonInfoLocal,
409                                         (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
410                                         out dwBytes);
411                                     //ボタン詳細情報取得
412                                     SendMessage(
413                                         toolWin,
414                                         (int)Sm_Message.TB_GETBUTTONINFO,
415                                         tbButtonLocal2.idCommand,
416                                         ptbSysInfo);
417                                     //共有メモリにボタン詳細情報を読み込む領域確保
418                                     var ptrInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonInfoLocal));
419                                     if (ptrInfo.Equals(IntPtr.Zero)) return false; //共有メモリ確保失敗
420                                     try
421                                     {
422                                         Marshal.StructureToPtr(tbButtonInfoLocal, ptrInfo, true);    //共有メモリ初期化
423                                         //Explorer内のメモリを共有メモリに読み込み
424                                         ReadProcessMemory(
425                                             hProc,
426                                             ptbSysInfo,
427                                             ptrInfo,
428                                             (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
429                                             out dwBytes);
430                                         //共有メモリの内容を構造体に変換
431                                         tbButtonInfoLocal2 = (TBBUTTONINFO)Marshal.PtrToStructure(
432                                                                     ptrInfo,
433                                                                     typeof(TBBUTTONINFO));
434                                     }
435                                     finally
436                                     {
437                                         Marshal.FreeCoTaskMem(ptrInfo);  //共有メモリ解放
438                                     }
439                                     //Tooltipの内容をExplorer内のメモリから共有メモリへ読込
440                                     ReadProcessMemory(hProc, pszSysTitle, pszTitle, (IntPtr)titleSize, out dwBytes);
441                                     //ローカル変数へ変換
442                                     title = Marshal.PtrToStringAnsi(pszTitle, titleSize);
443
444                                     //Tooltipが指定文字列を含んでいればクリック
445                                     if (title.Contains(tooltip))
446                                     {
447                                         //PostMessageでクリックを送るために、ボタン詳細情報のlParamでポイントされているTRAYNOTIFY情報が必要
448                                         var tNotify = new TRAYNOTIFY();
449                                         var tNotify2 = new TRAYNOTIFY();
450                                         //共有メモリ確保
451                                         var ptNotify = Marshal.AllocCoTaskMem(Marshal.SizeOf(tNotify));
452                                         if (ptNotify.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
453                                         try
454                                         {
455                                             Marshal.StructureToPtr(tNotify, ptNotify, true); //初期化
456                                             //lParamのメモリを読込
457                                             ReadProcessMemory(
458                                                 hProc,
459                                                 tbButtonInfoLocal2.lParam,
460                                                 ptNotify,
461                                                 (IntPtr)Marshal.SizeOf(tNotify),
462                                                 out dwBytes);
463                                             //構造体へ変換
464                                             tNotify2 = (TRAYNOTIFY)
465                                                             Marshal.PtrToStructure(
466                                                                 ptNotify,
467                                                                 typeof(TRAYNOTIFY));
468                                         }
469                                         finally
470                                         {
471                                             Marshal.FreeCoTaskMem(ptNotify); //共有メモリ解放
472                                         }
473                                         //クリックするためには通知領域がアクティブでなければならない
474                                         SetForegroundWindow(tNotify2.hWnd);
475                                         //左クリック
476                                         PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONDOWN);
477                                         PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONUP);
478                                         return true;
479                                     }
480                                 }
481                                 return false;    //該当なし
482                             }
483                             finally
484                             {
485                                 VirtualFreeEx(hProc, pszSysTitle, (IntPtr)titleSize, MemoryFreeTypes.Release);   //メモリ解放
486                             }
487                         }
488                         finally
489                         {
490                             Marshal.FreeCoTaskMem(pszTitle);     //共有メモリ解放
491                         }
492                     }
493                     finally
494                     {
495                         VirtualFreeEx(hProc, ptbSysInfo, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), MemoryFreeTypes.Release);    //メモリ解放
496                     }
497                 }
498                 finally
499                 {
500                     VirtualFreeEx(hProc, ptbSysButton, (IntPtr)Marshal.SizeOf(tbButtonLocal), MemoryFreeTypes.Release);      //メモリ解放
501                 }
502             }
503             finally
504             {
505                 CloseHandle(hProc);  //Explorerのプロセス閉じる
506             }
507         }
508         #endregion
509
510         #region "画面ブリンク用"
511         public static bool FlashMyWindow(IntPtr hwnd,
512             FlashSpecification flashType,
513             int flashCount)
514         {
515             var fInfo = new FLASHWINFO();
516             fInfo.cbSize = Convert.ToInt32(Marshal.SizeOf(fInfo));
517             fInfo.hwnd = hwnd;
518             fInfo.dwFlags = (int)FlashSpecification.FlashAll;
519             fInfo.uCount = flashCount;
520             fInfo.dwTimeout = 0;
521
522             return FlashWindowEx(ref fInfo);
523         }
524
525         public enum FlashSpecification : uint
526         {
527             FlashStop = FLASHW_STOP,
528             FlashCaption = FLASHW_CAPTION,
529             FlashTray = FLASHW_TRAY,
530             FlashAll = FLASHW_ALL,
531             FlashTimer = FLASHW_TIMER,
532             FlashTimerNoForeground = FLASHW_TIMERNOFG,
533         }
534         /// http://www.atmarkit.co.jp/fdotnet/dotnettips/723flashwindow/flashwindow.html
535         [DllImport("user32.dll")]
536         private static extern bool FlashWindowEx(
537             ref FLASHWINFO FWInfo);
538
539
540         private struct FLASHWINFO
541         {
542             public Int32 cbSize;    // FLASHWINFO構造体のサイズ
543             public IntPtr hwnd;     // 点滅対象のウィンドウ・ハンドル
544             public Int32 dwFlags;   // 以下の「FLASHW_XXX」のいずれか
545             public Int32 uCount;    // 点滅する回数
546             public Int32 dwTimeout; // 点滅する間隔(ミリ秒単位)
547         }
548
549         // 点滅を止める
550         private const Int32 FLASHW_STOP = 0;
551         // タイトルバーを点滅させる
552         private const Int32 FLASHW_CAPTION = 0x1;
553         // タスクバー・ボタンを点滅させる
554         private const Int32 FLASHW_TRAY = 0x2;
555         // タスクバー・ボタンとタイトルバーを点滅させる
556         private const Int32 FLASHW_ALL = 0x3;
557         // FLASHW_STOPが指定されるまでずっと点滅させる
558         private const Int32 FLASHW_TIMER = 0x4;
559         // ウィンドウが最前面に来るまでずっと点滅させる
560         private const Int32 FLASHW_TIMERNOFG = 0xC;
561         #endregion
562
563         [DllImport("user32.dll")]
564         public static extern bool ValidateRect(
565             IntPtr hwnd,
566             IntPtr rect);
567
568         #region "スクリーンセーバー起動中か判定"
569         [DllImport("user32", CharSet = CharSet.Auto)]
570         private static extern int SystemParametersInfo(
571                     int intAction,
572                     int intParam,
573                     ref bool bParam,
574                     int intWinIniFlag);
575         // returns non-zero value if function succeeds
576
577         //スクリーンセーバーが起動中かを取得する定数
578         private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
579
580         public static bool IsScreenSaverRunning()
581         {
582             var isRunning = false;
583             SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRunning, 0);
584             return isRunning;
585         }
586         #endregion
587
588         #region "グローバルフック"
589         [DllImport("user32")]
590         private static extern int RegisterHotKey(IntPtr hwnd, int id,
591             int fsModifiers, int vk);
592         [DllImport("user32")]
593         private static extern int UnregisterHotKey(IntPtr hwnd, int id);
594         [DllImport("kernel32", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
595         private static extern ushort GlobalAddAtom([MarshalAs(UnmanagedType.LPTStr)] string lpString);
596         [DllImport("kernel32")]
597         private static extern ushort GlobalDeleteAtom(ushort nAtom);
598
599         private static int registerCount = 0;
600         // register a global hot key
601         public static int RegisterGlobalHotKey(int hotkeyValue, int modifiers, Form targetForm)
602         {
603             ushort hotkeyID = 0;
604             try
605             {
606                 // use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs)
607                 registerCount++;
608                 var atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + targetForm.Name + registerCount.ToString();
609                 hotkeyID = GlobalAddAtom(atomName);
610                 if (hotkeyID == 0)
611                 {
612                     throw new Exception("Unable to generate unique hotkey ID. Error code: " +
613                        Marshal.GetLastWin32Error().ToString());
614                 }
615
616                 // register the hotkey, throw if any error
617                 if (RegisterHotKey(targetForm.Handle, hotkeyID, modifiers, hotkeyValue) == 0)
618                 {
619                     throw new Exception("Unable to register hotkey. Error code: " +
620                        Marshal.GetLastWin32Error().ToString());
621                 }
622                 return hotkeyID;
623             }
624             catch (Exception)
625             {
626                 // clean up if hotkey registration failed
627                 UnregisterGlobalHotKey(hotkeyID, targetForm);
628                 return 0;
629             }
630         }
631
632         // unregister a global hotkey
633         public static void UnregisterGlobalHotKey(ushort hotkeyID, Form targetForm)
634         {
635             if (hotkeyID != 0)
636             {
637                 UnregisterHotKey(targetForm.Handle, hotkeyID);
638                 // clean up the atom list
639                 GlobalDeleteAtom(hotkeyID);
640                 hotkeyID = 0;
641             }
642         }
643         #endregion
644
645         #region "プロセスのProxy設定"
646         [DllImport("wininet.dll", SetLastError =true)]
647         private static extern bool InternetSetOption(IntPtr hInternet,
648                                                      int dwOption,
649                                                      IntPtr lpBuffer,
650                                                      int lpdwBufferLength);
651
652         private struct INTERNET_PROXY_INFO : IDisposable
653         {
654             public int dwAccessType;
655             public IntPtr proxy;
656             public IntPtr proxyBypass;
657
658             public void Dispose()
659             {
660                 Dispose(true);
661             }
662
663             private void Dispose(bool disposing)
664             {
665                 if (proxy != IntPtr.Zero) Marshal.FreeHGlobal(proxy);
666                 if (proxyBypass != IntPtr.Zero) Marshal.FreeHGlobal(proxyBypass);
667             }
668         }
669
670         private static void RefreshProxySettings(string strProxy)
671         {
672             const int INTERNET_OPTION_PROXY = 38;
673             //const int INTERNET_OPEN_TYPE_PRECONFIG = 0;   //IE setting
674             const int INTERNET_OPEN_TYPE_DIRECT = 1;      //Direct
675             const int INTERNET_OPEN_TYPE_PROXY = 3;       //Custom
676
677             INTERNET_PROXY_INFO ipi;
678
679             // Filling in structure
680             if (!string.IsNullOrEmpty(strProxy))
681             {
682                 ipi.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
683                 ipi.proxy = Marshal.StringToHGlobalAnsi(strProxy);
684                 ipi.proxyBypass = Marshal.StringToHGlobalAnsi("local");
685             }
686             else if (strProxy == null)
687             {
688                 //IE Default
689                 var p = WebRequest.GetSystemWebProxy();
690                 if (p.IsBypassed(new Uri("http://www.google.com/")))
691                 {
692                     ipi.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
693                     ipi.proxy = IntPtr.Zero;
694                     ipi.proxyBypass = IntPtr.Zero;
695                 }
696                 else
697                 {
698                     ipi.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
699                     ipi.proxy = Marshal.StringToHGlobalAnsi(p.GetProxy(new Uri("http://www.google.com/")).Authority);
700                     ipi.proxyBypass = Marshal.StringToHGlobalAnsi("local");
701                 }
702             }
703             else
704             {
705                 ipi.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
706                 ipi.proxy = IntPtr.Zero;
707                 ipi.proxyBypass = IntPtr.Zero;
708             }
709
710             try
711             {
712                 // Allocating memory
713                 var pIpi = Marshal.AllocCoTaskMem(Marshal.SizeOf(ipi));
714                 if (pIpi.Equals(IntPtr.Zero)) return;
715                 try
716                 {
717                     // Converting structure to IntPtr
718                     Marshal.StructureToPtr(ipi, pIpi, true);
719                     var ret = InternetSetOption(IntPtr.Zero,
720                                                            INTERNET_OPTION_PROXY,
721                                                            pIpi,
722                                                            Marshal.SizeOf(ipi));
723                 }
724                 finally
725                 {
726                     Marshal.FreeCoTaskMem(pIpi);
727                 }
728             }
729             finally
730             {
731                 ipi.Dispose();
732             }
733         }
734
735         private static void RefreshProxyAccount(string username, string password)
736         {
737             const int INTERNET_OPTION_PROXY_USERNAME = 43;
738             const int INTERNET_OPTION_PROXY_PASSWORD = 44;
739
740             if (!string.IsNullOrEmpty(username) || !string.IsNullOrEmpty(password))
741             {
742                 var ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_USERNAME, IntPtr.Zero, 0);
743                 ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_PASSWORD, IntPtr.Zero, 0);
744             }
745             else
746             {
747                 var pUser = Marshal.StringToBSTR(username);
748                 var pPass = Marshal.StringToBSTR(password);
749                 try
750                 {
751                     var ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_USERNAME, pUser, username.Length + 1);
752                     ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_PASSWORD, pPass, password.Length + 1);
753                 }
754                 finally
755                 {
756                     Marshal.FreeBSTR(pUser);
757                     Marshal.FreeBSTR(pPass);
758                 }
759             }
760         }
761
762         public static void SetProxy(HttpConnection.ProxyType pType, string host, int port, string username, string password)
763         {
764             string proxy = null;
765             switch (pType)
766             {
767             case HttpConnection.ProxyType.IE:
768                 proxy = null;
769                 break;
770             case HttpConnection.ProxyType.None:
771                 proxy = "";
772                 break;
773             case HttpConnection.ProxyType.Specified:
774                 proxy = host + (port > 0 ? ":" + port.ToString() : "");
775                 break;
776             }
777             RefreshProxySettings(proxy);
778             RefreshProxyAccount(username, password);
779         }
780 #endregion
781     }
782 }