using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace AppliStation.Util { /// /// Win32ネイティブメソッドを叩いてGUI操作するための関数群のクラス /// public sealed class NativeMethods { /// /// 呼び出し禁止 /// private NativeMethods() { } /// /// WindowsVista向け、プログレスバーステータス(色)を設定する /// /// 対象のプログレスバー /// 状態。(1:Normal,2:Error,3:Paused) public static void ProgressBar_SetState(ProgressBar progBar, uint state) { try { // status := (PBST_NORMAL | PBST_ERROR | PBST_PAUSED) // SendMessage(progressBar.Handle, PBM_SETSTATE, state, 0); SendMessage(progBar.Handle, 0x410, state, 0); } catch (Exception) { } } #region タスクバーおよびタイトルバーのフラッシュ /// /// タスクバーおよびタイトルバーボタンのフラッシュの設定フラグ /// public enum FlashFlag : uint { /// /// 点滅の停止 /// Stop = 0, /// /// タイトルバーを点滅 /// Caption = 1, /// /// タスクバーボタンを点滅 /// Tray = 2, /// /// タイトルバーとタスクバーボタンを点滅 /// All = 3, /// /// Stopが設定されるまで点滅する /// Timer = 4, /// /// フォアグラウンドの状態になるまで点滅 /// TimerNoFG = 12, } [StructLayout(LayoutKind.Sequential)] struct FLASHWINFO { public int cbSize; public IntPtr hWnd; public FlashFlag dwFlags; public uint uCount; public uint dwTimeout; } /// /// タスクバーおよびタイトルバーボタンを点滅させる /// /// 対象フォーム /// 点滅パラメータフラグ /// 点滅回数 /// 点滅の間隔(ミリ秒) /// public static bool Form_FlashWindow(Form form, FlashFlag flag, uint count, uint timeout) { try { FLASHWINFO info = new FLASHWINFO(); info.cbSize = Marshal.SizeOf(typeof(FLASHWINFO)); info.hWnd = form.Handle; info.dwFlags = flag; info.uCount = count; info.dwTimeout = timeout; return FlashWindowEx(ref info) == 0; } catch (Exception) { return false; } } [DllImport("user32.dll")] static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi); #endregion #region ColumnHeaderのソートの三角印用 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] internal struct HD_ITEM { public uint mask; public int cxy; [MarshalAs(UnmanagedType.LPTStr)]public string pszText; public IntPtr hbm; public int cchTextMax; public int fmt; [MarshalAs(UnmanagedType.LPTStr)]public string lParam; public int iImage; // index of bitmap in ImageList public int iOrder; } internal static IntPtr ListView_GetHeader(ListView listview) { // SendMessage(hWnd, LVM_GETHEADER, 0, NULL); return SendMessage(listview.Handle, 0x101E, 0, 0); } /// /// WinXP以降、ソートの矢印を表示 /// /// 対象のListView /// 表示する矢印のヘッダ /// ソートの昇順・降順 public static void ColumnHeader_SetSortState(ListView listView, int column, SortOrder order) { try { // SendMessage(hWnd, LVM_GETHEADER, NULL, NULL); IntPtr hWnd = SendMessage(listView.Handle, 0x101F, 0, 0); HD_ITEM hdi = new HD_ITEM(); hdi.mask = 0x0004; // HDI_FORMAT; for (int i = 0; i < listView.Columns.Count; i++) { // SendMessage(hWnd, HDM_GETITEMW, i, &hdi); SendMessage(hWnd, 0x120b, i, ref hdi); const int HDF_SORTUP = 0x400; const int HDF_SORTDOWN = 0x200; if (i != column || order == SortOrder.None) { hdi.fmt = hdi.fmt & ~(HDF_SORTUP | HDF_SORTDOWN); } else if (order == SortOrder.Ascending) { // 昇順 hdi.fmt = hdi.fmt & ~HDF_SORTDOWN | HDF_SORTUP; } else if (order == SortOrder.Descending) { // 降順 hdi.fmt = hdi.fmt & ~HDF_SORTUP | HDF_SORTDOWN; } // SendMessage(hWnd, HDM_SETITEMW, i, &hdi); SendMessage(hWnd, 0x120c, i, ref hdi); } } catch (Exception) { } } #endregion #region EnableWindow(コメントアウト) // /// // /// 指定されたコントロール(ウィンドウ)への、 // /// キーボード入力およびマウス入力を有効化または無効化 // /// // /// 対象のコントロールのハンドラ // /// 有効にするか無効にするかを指定 // /// 直前にウィンドウが無効状態だった場合はtrueを返す // public static bool Control_EnableWindow(Control ctrl, bool bEnable) // { // try { // return EnableWindow(ctrl.Handle, bEnable); // } catch { // ctrl.Enabled = bEnable; // return true; // } // } // // /// // /// 指定されたコントロール(ウィンドウ)への、 // /// キーボード入力およびマウス入力を有効化または無効化 // /// // /// 対象のコントロールのハンドラ // /// 有効にするか無効にするかを指定 // /// 直前にウィンドウが無効状態だった場合はtrueを返す // [DllImport("user32.dll")] // public static extern bool EnableWindow(IntPtr hWnd, bool bEnable); #endregion [DllImport("user32.dll", CharSet=CharSet.Auto)] internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam); [DllImport("user32.dll", CharSet=CharSet.Auto)] internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, int wParam, ref HD_ITEM lParam); } }