// Boston, MA 02110-1301, USA.
using System;
-using System.Runtime.InteropServices;
using System.Diagnostics;
+using System.Linq;
+using System.Net;
+using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
-using System.Net;
+using OpenTween.Connection;
namespace OpenTween
{
- [Microsoft.VisualBasic.CompilerServices.StandardModule]
public static class Win32Api
{
- #region "先行起動プロセスをアクティブにする"
- // 外部プロセスのウィンドウを起動する
- public static void WakeupWindow(IntPtr hWnd)
- {
- // メイン・ウィンドウが最小化されていれば元に戻す
- if (IsIconic(hWnd))
- {
- ShowWindowAsync(hWnd, SW_RESTORE);
- }
-
- // メイン・ウィンドウを最前面に表示する
- SetForegroundWindow(hWnd);
- }
-
- // 外部プロセスのメイン・ウィンドウを起動するためのWin32 API
- [DllImport("user32.dll")]
- private extern static bool SetForegroundWindow(
- IntPtr hWnd);
-
- // ウィンドウの表示状態を設定
- [DllImport("user32.dll")]
- private extern static bool ShowWindowAsync(
- IntPtr hWnd,
- int nCmdShow);
-
- // 指定されたウィンドウが最小化( アイコン化)されているかどうかを調べる
- [DllImport("user32.dll")]
- private extern static bool IsIconic(
- IntPtr hWnd);
-
- // ShowWindowAsync関数のパラメータに渡す定義値
- private const int SW_RESTORE = 9; // 画面を元の大きさに戻す
-
- // 実行中の同じアプリケーションのプロセスを取得する
- public static Process GetPreviousProcess()
- {
- var curProcess = Process.GetCurrentProcess();
- var allProcesses = Process.GetProcessesByName(curProcess.ProcessName);
-
- foreach (Process checkProcess in allProcesses)
- {
- // 自分自身のプロセスIDは無視する
- if (checkProcess.Id != curProcess.Id)
- {
- // プロセスのフルパス名を比較して同じアプリケーションか検証
- if (string.Compare(
- checkProcess.MainModule.FileName,
- curProcess.MainModule.FileName, true) == 0)
- {
- // 同じフルパス名のプロセスを取得
- return checkProcess;
- }
- }
- }
-
- // 同じアプリケーションのプロセスが見つからない!
- return null;
- }
- #endregion
- #region "タスクトレイアイコンのクリック"
- // 指定されたクラス名およびウィンドウ名と一致するトップレベルウィンドウのハンドルを取得します
- [DllImport("user32.dll")]
- private extern static IntPtr FindWindow(
- string lpClassName,
- string lpWindowName);
-
- // 指定された文字列と一致するクラス名とウィンドウ名文字列を持つウィンドウのハンドルを返します
- [DllImport("user32.dll")]
- private extern static IntPtr FindWindowEx(
- IntPtr hWnd1,
- IntPtr hWnd2,
- string lpsz1,
- string lpsz2);
-
// 指定されたウィンドウへ、指定されたメッセージを送信します
[DllImport("user32.dll")]
- private extern static int SendMessage(
+ private extern static IntPtr SendMessage(
IntPtr hwnd,
- int wMsg,
+ SendMessageType wMsg,
IntPtr wParam,
IntPtr lParam);
// SendMessageで送信するメッセージ
- private enum Sm_Message : int
+ private enum SendMessageType : uint
{
+ WM_SETREDRAW = 0x000B, //再描画を許可するかを設定
WM_USER = 0x400, //ユーザー定義メッセージ
- TB_GETBUTTON = WM_USER + 23, //ツールバーのボタン取得
- TB_BUTTONCOUNT = WM_USER + 24, //ツールバーのボタン(アイコン)数取得
- TB_GETBUTTONINFO = WM_USER + 65, //ツールバーのボタン詳細情報取得
- }
- // ツールバーボタン構造体
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- private struct TBBUTTON
- {
- public int iBitmap;
- public IntPtr idCommand;
- public byte fsState;
- public byte fsStyle;
- public byte bReserved0;
- public byte bReserved1;
- public int dwData;
- public int iString;
- }
- // ツールバーボタン詳細情報構造体
- [StructLayout(LayoutKind.Sequential)]
- private struct TBBUTTONINFO
- {
- public Int32 cbSize;
- public Int32 dwMask;
- public Int32 idCommand;
- public Int32 iImage;
- public Byte fsState;
- public Byte fsStyle;
- public short cx;
- public IntPtr lParam;
- public IntPtr pszText;
- public Int32 cchText;
- }
- // TBBUTTONINFOのlParamでポイントされるアイコン情報(PostMessageで使用)
- [StructLayout(LayoutKind.Sequential)]
- private struct TRAYNOTIFY
- {
- public IntPtr hWnd;
- public UInt32 uID;
- public UInt32 uCallbackMessage;
- public UInt32 dwDummy1;
- public UInt32 dwDummy2;
- public IntPtr hIcon;
- }
- // TBBUTTONINFOに指定するマスク情報
- [Flags()]
- private enum ToolbarButtonMask : int
- {
- TBIF_COMMAND = 0x20,
- TBIF_LPARAM = 0x10,
- TBIF_TEXT = 0x2,
- }
- // 指定されたウィンドウを作成したスレッドの ID を取得します
- [DllImport("user32.dll", SetLastError = true)]
- private static extern int GetWindowThreadProcessId(
- IntPtr hwnd,
- out int lpdwProcessId);
-
- // 指定したプロセスIDに対するプロセスハンドルを取得します
- [DllImport("kernel32.dll")]
- private static extern IntPtr OpenProcess(
- ProcessAccess dwDesiredAccess,
- [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
- int dwProcessId);
-
- // OpenProcessで指定するアクセス権
- [Flags()]
- private enum ProcessAccess : int
- {
- /// <summary>Specifies all possible access flags for the process object.</summary>
- AllAccess = CreateThread | DuplicateHandle | QueryInformation | SetInformation | Terminate | VMOperation | VMRead | VMWrite | Synchronize,
- /// <summary>Enables usage of the process handle in the CreateRemoteThread function to create a thread in the process.</summary>
- CreateThread = 0x2,
- /// <summary>Enables usage of the process handle as either the source or target process in the DuplicateHandle function to duplicate a handle.</summary>
- DuplicateHandle = 0x40,
- /// <summary>Enables usage of the process handle in the GetExitCodeProcess and GetPriorityClass functions to read information from the process object.</summary>
- QueryInformation = 0x400,
- /// <summary>Enables usage of the process handle in the SetPriorityClass function to set the priority class of the process.</summary>
- SetInformation = 0x200,
- /// <summary>Enables usage of the process handle in the TerminateProcess function to terminate the process.</summary>
- Terminate = 0x1,
- /// <summary>Enables usage of the process handle in the VirtualProtectEx and WriteProcessMemory functions to modify the virtual memory of the process.</summary>
- VMOperation = 0x8,
- /// <summary>Enables usage of the process handle in the ReadProcessMemory function to read from the virtual memory of the process.</summary>
- VMRead = 0x10,
- /// <summary>Enables usage of the process handle in the WriteProcessMemory function to write to the virtual memory of the process.</summary>
- VMWrite = 0x20,
- /// <summary>Enables usage of the process handle in any of the wait functions to wait for the process to terminate.</summary>
- Synchronize = 0x100000,
- }
- // 指定したプロセスの仮想アドレス空間にメモリ領域を確保
- [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
- private static extern IntPtr VirtualAllocEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- int dwSize,
- AllocationTypes flAllocationType,
- MemoryProtectionTypes flProtect);
-
- // アロケート種類
- [Flags()]
- private enum AllocationTypes : uint
- {
- Commit = 0x1000,
- Reserve = 0x2000,
- Decommit = 0x4000,
- Release = 0x8000,
- Reset = 0x80000,
- Physical = 0x400000,
- TopDown = 0x100000,
- WriteWatch = 0x200000,
- LargePages = 0x20000000,
- }
- // アロケートしたメモリに対する保護レベル
- [Flags()]
- private enum MemoryProtectionTypes : uint
- {
- Execute = 0x10,
- ExecuteRead = 0x20,
- ExecuteReadWrite = 0x40,
- ExecuteWriteCopy = 0x80,
- NoAccess = 0x1,
- ReadOnly = 0x2,
- ReadWrite = 0x4,
- WriteCopy = 0x8,
- GuardModifierflag = 0x100,
- NoCacheModifierflag = 0x200,
- WriteCombineModifierflag = 0x400,
+
+ TCM_FIRST = 0x1300, //タブコントロールメッセージ
+ TCM_SETMINTABWIDTH = TCM_FIRST + 49, //タブアイテムの最小幅を設定
+
+ LVM_FIRST = 0x1000, //リストビューメッセージ
+ LVM_GETSELECTIONMARK = LVM_FIRST + 66, //複数選択時の起点になるアイテムの位置を取得
+ LVM_SETSELECTIONMARK = LVM_FIRST + 67, //複数選択時の起点になるアイテムを設定
}
- // オープンしているカーネルオブジェクトのハンドルをクローズします
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool CloseHandle(IntPtr hHandle);
-
- // 指定されたプロセスの仮想アドレス空間内のメモリ領域を解放またはコミット解除します
- [DllImport("kernel32.dll")]
- private static extern bool VirtualFreeEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- int dwSize,
- int dwFreeType);
-
- // メモリ解放種別
- [Flags()]
- private enum MemoryFreeTypes
+
+ /// <summary>
+ /// コントロールの再描画を許可するかを設定します
+ /// </summary>
+ /// <param name="control">対象となるコントロール</param>
+ /// <param name="redraw">再描画を許可する場合は true、抑制する場合は false</param>
+ /// <returns>このメッセージを処理する場合、アプリケーションは 0 を返します</returns>
+ public static int SetRedrawState(Control control, bool redraw)
{
- Release = 0x8000,
+ var state = redraw ? new IntPtr(1) : IntPtr.Zero;
+ return (int)SendMessage(control.Handle, SendMessageType.WM_SETREDRAW, state, IntPtr.Zero);
}
- //指定したプロセスのメモリ領域にデータをコピーする
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool WriteProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- ref TBBUTTONINFO lpBuffer,
- int nSize,
- out int lpNumberOfBytesWritten);
-
- //指定したプロセスのメモリ領域のデータを呼び出し側プロセスのバッファにコピーする
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool ReadProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- IntPtr lpBuffer,
- int nSize,
- out int lpNumberOfBytesRead);
-
- //メッセージをウィンドウのメッセージ キューに置き、対応するウィンドウがメッセージを処理するのを待たずに戻ります
- [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- private static extern bool PostMessage(
- IntPtr hWnd,
- uint Msg,
- UInt32 wParam,
- UInt32 lParam);
-
- //PostMessageで送信するメッセージ
- private enum PM_Message : uint
+
+ /// <summary>
+ /// タブコントロールのアイテムの最小幅を設定します
+ /// </summary>
+ /// <param name="tabControl">対象となるタブコントロール</param>
+ /// <param name="width">アイテムの最小幅。-1 を指定するとデフォルトの幅が使用されます</param>
+ /// <returns>設定前の最小幅</returns>
+ public static int SetMinTabWidth(TabControl tabControl, int width)
{
- WM_LBUTTONDOWN = 0x201, //左マウスボタン押し下げ
- WM_LBUTTONUP = 0x202, //左マウスボタン離し
+ return (int)SendMessage(tabControl.Handle, SendMessageType.TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)width);
}
- //タスクトレイアイコンのクリック処理
- public static bool ClickTasktrayIcon(string tooltip)
+ [DllImport("user32")]
+ private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
+
+ /// <summary>
+ /// 最小化状態のウィンドウを最小化する前の状態に復元します。
+ /// </summary>
+ /// <param name="window">復元するウィンドウ。</param>
+ /// <exception cref="System.ArgumentNullException"><paramref name="window"/> が null です。</exception>
+ public static void RestoreWindow(IWin32Window window)
{
- const string TRAY_WINDOW = "Shell_TrayWnd";
- const string TRAY_NOTIFYWINDOW = "TrayNotifyWnd";
- const string TRAY_PAGER = "SysPager";
- const string TOOLBAR_CONTROL = "ToolbarWindow32";
- //タスクバーのハンドル取得
- var taskbarWin = FindWindow(TRAY_WINDOW, null);
- if (taskbarWin.Equals(IntPtr.Zero)) return false;
- //通知領域のハンドル取得
- var trayWin = FindWindowEx(taskbarWin, IntPtr.Zero, TRAY_NOTIFYWINDOW, null);
- if (trayWin.Equals(IntPtr.Zero)) return false;
- //SysPagerの有無確認。(XP/2000はSysPagerあり)
- var tempWin = FindWindowEx(trayWin, IntPtr.Zero, TRAY_PAGER, null);
- if (tempWin.Equals(IntPtr.Zero)) tempWin = trayWin;
- //タスクトレイがツールバーで出来ているか確認
- // → ツールバーでなければ終了
- var toolWin = FindWindowEx(tempWin, IntPtr.Zero, TOOLBAR_CONTROL, null);
- if (toolWin.Equals(IntPtr.Zero)) return false;
- //タスクトレイのプロセス(Explorer)を取得し、外部から参照するために開く
- int expPid = 0;
- GetWindowThreadProcessId(toolWin, out expPid);
- var hProc = OpenProcess(ProcessAccess.VMOperation | ProcessAccess.VMRead | ProcessAccess.VMWrite, false, expPid);
- if (hProc.Equals(IntPtr.Zero)) return false;
-
- //プロセスを閉じるためにtry-finally
- try
- {
- var tbButtonLocal = new TBBUTTON(); //本プロセス内のタスクバーボタン情報作成(サイズ特定でのみ使用)
- //Explorer内のタスクバーボタン格納メモリ確保
- var ptbSysButton = VirtualAllocEx(hProc, IntPtr.Zero, Marshal.SizeOf(tbButtonLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
- if (ptbSysButton.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
- try
- {
- var tbButtonInfoLocal = new TBBUTTONINFO(); //本プロセス内ツールバーボタン詳細情報作成
- //Explorer内のタスクバーボタン詳細情報格納メモリ確保
- var ptbSysInfo = VirtualAllocEx(hProc, IntPtr.Zero, Marshal.SizeOf(tbButtonInfoLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
- if (ptbSysInfo.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
- try
- {
- const int titleSize = 256; //Tooltip文字列長
- var title = ""; //Tooltip文字列
- //共有メモリにTooltip読込メモリ確保
- var pszTitle = Marshal.AllocCoTaskMem(titleSize);
- if (pszTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
- try
- {
- //Explorer内にTooltip読込メモリ確保
- var pszSysTitle = VirtualAllocEx(hProc, IntPtr.Zero, titleSize, AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
- if (pszSysTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
- try
- {
- //通知領域ボタン数取得
- var iCount = (int)SendMessage(toolWin, (int)Sm_Message.TB_BUTTONCOUNT, new IntPtr(0), new IntPtr(0));
- //左から順に情報取得
- for (var i = 0; i < iCount; i++)
- {
- var dwBytes = 0; //読み書きバイト数
- var tbButtonLocal2 = new TBBUTTON(); //ボタン情報
- var tbButtonInfoLocal2 = new TBBUTTONINFO(); //ボタン詳細情報
- //共有メモリにボタン情報読込メモリ確保
- var ptrLocal = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonLocal));
- if (ptrLocal.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
- try
- {
- Marshal.StructureToPtr(tbButtonLocal, ptrLocal, true); //共有メモリ初期化
- //ボタン情報取得(idCommandを取得するため)
- SendMessage(
- toolWin,
- (int)Sm_Message.TB_GETBUTTON,
- new IntPtr(i),
- ptbSysButton);
- //Explorer内のメモリを共有メモリに読み込み
- ReadProcessMemory(
- hProc,
- ptbSysButton,
- ptrLocal,
- Marshal.SizeOf(tbButtonLocal),
- out dwBytes);
- //共有メモリの内容を構造体に変換
- tbButtonLocal2 = (TBBUTTON)Marshal.PtrToStructure(
- ptrLocal,
- typeof(TBBUTTON));
- }
- finally
- {
- Marshal.FreeCoTaskMem(ptrLocal); //共有メモリ解放
- }
-
- //ボタン詳細情報を取得するためのマスク等を設定
- tbButtonInfoLocal.cbSize = Marshal.SizeOf(tbButtonInfoLocal);
- tbButtonInfoLocal.dwMask = (int)(ToolbarButtonMask.TBIF_COMMAND | ToolbarButtonMask.TBIF_LPARAM | ToolbarButtonMask.TBIF_TEXT);
- tbButtonInfoLocal.pszText = pszSysTitle; //Tooltip書き込み先領域
- tbButtonInfoLocal.cchText = titleSize;
- //マスク設定等をExplorerのメモリへ書き込み
- WriteProcessMemory(
- hProc,
- ptbSysInfo,
- ref tbButtonInfoLocal,
- Marshal.SizeOf(tbButtonInfoLocal),
- out dwBytes);
- //ボタン詳細情報取得
- SendMessage(
- toolWin,
- (int)Sm_Message.TB_GETBUTTONINFO,
- tbButtonLocal2.idCommand,
- ptbSysInfo);
- //共有メモリにボタン詳細情報を読み込む領域確保
- var ptrInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonInfoLocal));
- if (ptrInfo.Equals(IntPtr.Zero)) return false; //共有メモリ確保失敗
- try
- {
- Marshal.StructureToPtr(tbButtonInfoLocal, ptrInfo, true); //共有メモリ初期化
- //Explorer内のメモリを共有メモリに読み込み
- ReadProcessMemory(
- hProc,
- ptbSysInfo,
- ptrInfo,
- Marshal.SizeOf(tbButtonInfoLocal),
- out dwBytes);
- //共有メモリの内容を構造体に変換
- tbButtonInfoLocal2 = (TBBUTTONINFO)Marshal.PtrToStructure(
- ptrInfo,
- typeof(TBBUTTONINFO));
- }
- finally
- {
- Marshal.FreeCoTaskMem(ptrInfo); //共有メモリ解放
- }
- //Tooltipの内容をExplorer内のメモリから共有メモリへ読込
- ReadProcessMemory(hProc, pszSysTitle, pszTitle, titleSize, out dwBytes);
- //ローカル変数へ変換
- title = Marshal.PtrToStringAnsi(pszTitle, titleSize);
-
- //Tooltipが指定文字列を含んでいればクリック
- if (title.Contains(tooltip))
- {
- //PostMessageでクリックを送るために、ボタン詳細情報のlParamでポイントされているTRAYNOTIFY情報が必要
- var tNotify = new TRAYNOTIFY();
- var tNotify2 = new TRAYNOTIFY();
- //共有メモリ確保
- var ptNotify = Marshal.AllocCoTaskMem(Marshal.SizeOf(tNotify));
- if (ptNotify.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
- try
- {
- Marshal.StructureToPtr(tNotify, ptNotify, true); //初期化
- //lParamのメモリを読込
- ReadProcessMemory(
- hProc,
- tbButtonInfoLocal2.lParam,
- ptNotify,
- Marshal.SizeOf(tNotify),
- out dwBytes);
- //構造体へ変換
- tNotify2 = (TRAYNOTIFY)
- Marshal.PtrToStructure(
- ptNotify,
- typeof(TRAYNOTIFY));
- }
- finally
- {
- Marshal.FreeCoTaskMem(ptNotify); //共有メモリ解放
- }
- //クリックするためには通知領域がアクティブでなければならない
- SetForegroundWindow(tNotify2.hWnd);
- //左クリック
- PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, tNotify2.uID, (uint)PM_Message.WM_LBUTTONDOWN);
- PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, tNotify2.uID, (uint)PM_Message.WM_LBUTTONUP);
- return true;
- }
- }
- return false; //該当なし
- }
- finally
- {
- VirtualFreeEx(hProc, pszSysTitle, titleSize, (int)MemoryFreeTypes.Release); //メモリ解放
- }
- }
- finally
- {
- Marshal.FreeCoTaskMem(pszTitle); //共有メモリ解放
- }
- }
- finally
- {
- VirtualFreeEx(hProc, ptbSysInfo, Marshal.SizeOf(tbButtonInfoLocal), (int)MemoryFreeTypes.Release); //メモリ解放
- }
- }
- finally
- {
- VirtualFreeEx(hProc, ptbSysButton, Marshal.SizeOf(tbButtonLocal), (int)MemoryFreeTypes.Release); //メモリ解放
- }
- }
- finally
+ if (window == null)
{
- CloseHandle(hProc); //Explorerのプロセス閉じる
+ throw new ArgumentNullException("window");
}
- }
- #endregion
- //画面をブリンクするためのWin32API。起動時に10ページ読み取りごとに継続確認メッセージを表示する際の通知強調用
- [DllImport("user32.dll")]
- public static extern int FlashWindow(
- int hwnd,
- int bInvert);
+ ShowWindow(window.Handle, /* SW_RESTORE */ 9);
+ }
#region "画面ブリンク用"
public static bool FlashMyWindow(IntPtr hwnd,
fInfo.uCount = flashCount;
fInfo.dwTimeout = 0;
- return FlashWindowEx(fInfo);
+ return FlashWindowEx(ref fInfo);
}
public enum FlashSpecification : uint
/// http://www.atmarkit.co.jp/fdotnet/dotnettips/723flashwindow/flashwindow.html
[DllImport("user32.dll")]
private static extern bool FlashWindowEx(
- FLASHWINFO FWInfo);
+ ref FLASHWINFO FWInfo);
private struct FLASHWINFO
IntPtr hwnd,
IntPtr rect);
+ #region "selection mark"
+ // 複数選択時の起点になるアイテム (selection mark) の位置を取得する
+ public static int ListView_GetSelectionMark(IntPtr hwndLV)
+ {
+ return SendMessage(hwndLV, SendMessageType.LVM_GETSELECTIONMARK, IntPtr.Zero, IntPtr.Zero).ToInt32();
+ }
+
+ // 複数選択時の起点になるアイテム (selection mark) を設定する
+ public static void ListView_SetSelectionMark(IntPtr hwndLV, int itemIndex)
+ {
+ SendMessage(hwndLV, SendMessageType.LVM_SETSELECTIONMARK, IntPtr.Zero, (IntPtr)itemIndex);
+ }
+ #endregion
+
#region "スクリーンセーバー起動中か判定"
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(
// returns non-zero value if function succeeds
//スクリーンセーバーが起動中かを取得する定数
- private const int SPI_GETSCREENSAVERRUNNING = 0x61;
+ private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
public static bool IsScreenSaverRunning()
{
- var ret = 0;
var isRunning = false;
-
- ret = SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRunning, 0);
+ SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRunning, 0);
return isRunning;
}
#endregion
int fsModifiers, int vk);
[DllImport("user32")]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);
+ [DllImport("kernel32", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
+ private static extern ushort GlobalAddAtom([MarshalAs(UnmanagedType.LPTStr)] string lpString);
[DllImport("kernel32")]
- private static extern int GlobalAddAtom(string lpString);
- [DllImport("kernel32")]
- private static extern int GlobalDeleteAtom(int nAtom);
+ private static extern ushort GlobalDeleteAtom(ushort nAtom);
private static int registerCount = 0;
// register a global hot key
public static int RegisterGlobalHotKey(int hotkeyValue, int modifiers, Form targetForm)
{
- var hotkeyID = 0;
+ ushort hotkeyID = 0;
try
{
// use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs)
}
// unregister a global hotkey
- public static void UnregisterGlobalHotKey(int hotkeyID, Form targetForm)
+ public static void UnregisterGlobalHotKey(ushort hotkeyID, Form targetForm)
{
if (hotkeyID != 0)
{
IntPtr lpBuffer,
int lpdwBufferLength);
- private struct INTERNET_PROXY_INFO
+ private struct INTERNET_PROXY_INFO : IDisposable
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ private void Dispose(bool disposing)
+ {
+ if (proxy != IntPtr.Zero) Marshal.FreeHGlobal(proxy);
+ if (proxyBypass != IntPtr.Zero) Marshal.FreeHGlobal(proxyBypass);
+ }
}
private static void RefreshProxySettings(string strProxy)
}
finally
{
- if (ipi.proxy != IntPtr.Zero) Marshal.FreeHGlobal(ipi.proxy);
- if (ipi.proxyBypass != IntPtr.Zero) Marshal.FreeHGlobal(ipi.proxyBypass);
+ ipi.Dispose();
}
}
}
}
- public static void SetProxy(HttpConnection.ProxyType pType, string host, int port, string username, string password)
+ public static void SetProxy(ProxyType pType, string host, int port, string username, string password)
{
string proxy = null;
switch (pType)
{
- case HttpConnection.ProxyType.IE:
+ case ProxyType.IE:
proxy = null;
break;
- case HttpConnection.ProxyType.None:
+ case ProxyType.None:
proxy = "";
break;
- case HttpConnection.ProxyType.Specified:
+ case ProxyType.Specified:
proxy = host + (port > 0 ? ":" + port.ToString() : "");
break;
}