OSDN Git Service

na-get-lib,ひとまず依存関係解決関数群を作った。
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / NativeMethods.cs
1 using System;\r
2 using System.Runtime.InteropServices;\r
3 using System.Windows.Forms;\r
4 \r
5 namespace AppliStation.Util\r
6 {\r
7         /// <summary>\r
8         /// Win32ネイティブメソッドを叩いてGUI操作するための関数群のクラス\r
9         /// </summary>\r
10         public sealed class NativeMethods\r
11         {\r
12                 /// <summary>\r
13                 /// 呼び出し禁止\r
14                 /// </summary>\r
15                 private NativeMethods()\r
16                 {\r
17                 }\r
18                 \r
19 \r
20                 /// <summary>\r
21                 /// WindowsVista向け、プログレスバーステータス(色)を設定する\r
22                 /// </summary>\r
23                 /// <param name="progBar">対象のプログレスバー</param>\r
24                 /// <param name="state">状態。(1:Normal,2:Error,3:Paused)</param>\r
25                 public static void ProgressBar_SetState(ProgressBar progBar, uint state)\r
26                 {\r
27                         try {\r
28                                 // status := (PBST_NORMAL | PBST_ERROR | PBST_PAUSED)\r
29                                 // SendMessage(progressBar.Handle, PBM_SETSTATE, state, 0);\r
30                                 SendMessage(progBar.Handle, 0x410, state, 0);\r
31                         } catch (Exception) {\r
32                         }\r
33                 }\r
34                 \r
35                 #region タスクバーおよびタイトルバーのフラッシュ\r
36                 \r
37                 /// <summary>\r
38                 /// タスクバーおよびタイトルバーボタンのフラッシュの設定フラグ\r
39                 /// </summary>\r
40                 public enum FlashFlag : uint {\r
41                         /// <summary>\r
42                         /// 点滅の停止\r
43                         /// </summary>\r
44                         Stop = 0,\r
45                         /// <summary>\r
46                         /// タイトルバーを点滅\r
47                         /// </summary>\r
48                         Caption = 1,\r
49                         /// <summary>\r
50                         /// タスクバーボタンを点滅\r
51                         /// </summary>\r
52                         Tray = 2,\r
53                         /// <summary>\r
54                         /// タイトルバーとタスクバーボタンを点滅\r
55                         /// </summary>\r
56                         All = 3,\r
57                         /// <summary>\r
58                         /// Stopが設定されるまで点滅する\r
59                         /// </summary>\r
60                         Timer = 4,\r
61                         /// <summary>\r
62                         /// フォアグラウンドの状態になるまで点滅\r
63                         /// </summary>\r
64                         TimerNoFG = 12,\r
65                 }\r
66                 \r
67                 [StructLayout(LayoutKind.Sequential)]\r
68                 struct FLASHWINFO\r
69                 {\r
70                     public int cbSize;\r
71                     public IntPtr hWnd;\r
72                     public FlashFlag dwFlags;\r
73                     public uint uCount;\r
74                     public uint dwTimeout;\r
75                 }\r
76                 \r
77                 /// <summary>\r
78                 /// タスクバーおよびタイトルバーボタンを点滅させる\r
79                 /// </summary>\r
80                 /// <param name="form">対象フォーム</param>\r
81                 /// <param name="flag">点滅パラメータフラグ</param>\r
82                 /// <param name="count">点滅回数</param>\r
83                 /// <param name="timeout">点滅の間隔(ミリ秒)</param>\r
84                 /// <returns></returns>\r
85                 public static bool Form_FlashWindow(Form form, FlashFlag flag, uint count, uint timeout)\r
86                 {\r
87                         try {\r
88                                 FLASHWINFO info = new FLASHWINFO();\r
89                                 info.cbSize = Marshal.SizeOf(typeof(FLASHWINFO));\r
90                                 info.hWnd = form.Handle;\r
91                                 info.dwFlags = flag;\r
92                                 info.uCount = count;\r
93                                 info.dwTimeout = timeout;\r
94                                 \r
95                                 return FlashWindowEx(ref info) == 0;\r
96                         } catch (Exception) {\r
97                                 return false;\r
98                         }\r
99                 }\r
100                 \r
101                 [DllImport("user32.dll")]\r
102                 static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);\r
103                 \r
104                 #endregion\r
105                 \r
106                 #region ListView関連\r
107                 \r
108                 #region 選択の半透明(コメント)\r
109                 /*\r
110                 /// <summary>\r
111                 /// マウスでの選択に半透明ツールを採用する\r
112                 /// </summary>\r
113                 /// <param name="listView">対象のリストビュー</param>\r
114                 public static void ListView_EnableDoubleBuffer(ListView listView)\r
115                 {\r
116                         try {\r
117                                 // SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_DOUBLEBUFFER, LVS_EX_DOUBLEBUFFER);\r
118                                 SendMessage(listView.Handle, 0x1036, 0x00010000, 0x00010000);\r
119                         } catch (Exception) {\r
120                         }\r
121                 }\r
122                 */\r
123                 #endregion\r
124                 \r
125                 /// <summary>\r
126                 /// 選択されたアイテムの部分の背景にグラデーションがかかった感じになる、\r
127                 /// Vista以降でのエクスプローラの見た目をListViewに反映させる。\r
128                 /// \r
129                 /// なお、Vista未満のバージョンでは何もしない。\r
130                 /// </summary>\r
131                 /// <param name="listView">対象のListView</param>\r
132                 public static void ListView_EnableVistaExplorerTheme(ListView listView)\r
133                 {\r
134                         // Vista未満はなにもしない\r
135                         OperatingSystem os = Environment.OSVersion;\r
136                         if (os.Platform != PlatformID.Win32NT || os.Version.Major < 6) return;\r
137                         \r
138                         try {\r
139                                 SetWindowTheme(listView.Handle, "explorer", null);\r
140                         } catch (Exception) {\r
141                         }\r
142                 }\r
143                 \r
144                 #region ColumnHeaderのソートの三角印用\r
145                 \r
146                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]\r
147                 internal struct HD_ITEM\r
148                 {\r
149                         public uint    mask;\r
150                         public int     cxy;\r
151                         [MarshalAs(UnmanagedType.LPTStr)]public string   pszText;\r
152                         public IntPtr   hbm;\r
153                         public int     cchTextMax;\r
154                         public int     fmt;\r
155                         [MarshalAs(UnmanagedType.LPTStr)]public string  lParam;\r
156                         public int     iImage;        // index of bitmap in ImageList\r
157                         public int     iOrder;\r
158                 }\r
159                 \r
160                 internal static IntPtr ListView_GetHeader(ListView listview)\r
161                 {\r
162                         // SendMessage(hWnd, LVM_GETHEADER, 0, NULL);\r
163                         return SendMessage(listview.Handle, 0x101E, 0, 0);\r
164                 }\r
165                 \r
166                 /// <summary>\r
167                 /// WinXP以降、ソートの矢印を表示\r
168                 /// </summary>\r
169                 /// <param name="listView">対象のListView</param>\r
170                 /// <param name="column">表示する矢印のヘッダ</param>\r
171                 /// <param name="order">ソートの昇順・降順</param>\r
172                 public static void ColumnHeader_SetSortState(ListView listView, int column, SortOrder order)\r
173                 {\r
174                         try {\r
175                                 // SendMessage(hWnd, LVM_GETHEADER, NULL, NULL);\r
176                                 IntPtr hWnd = SendMessage(listView.Handle, 0x101F, 0, 0);\r
177                                 \r
178                                 HD_ITEM hdi = new HD_ITEM();\r
179                                 hdi.mask = 0x0004; // HDI_FORMAT;\r
180                                 for (int i = 0; i < listView.Columns.Count; i++) {\r
181                                         // SendMessage(hWnd, HDM_GETITEMW, i, &hdi);\r
182                                         SendMessage(hWnd, 0x120b, i, ref hdi);\r
183                                         \r
184                                         const int HDF_SORTUP = 0x400;\r
185                                         const int HDF_SORTDOWN = 0x200;\r
186                                         \r
187                                         if (i != column || order == SortOrder.None) {\r
188                                                 hdi.fmt = hdi.fmt & ~(HDF_SORTUP | HDF_SORTDOWN);\r
189                                         } else if (order == SortOrder.Ascending) { // 昇順\r
190                                                 hdi.fmt = hdi.fmt & ~HDF_SORTDOWN | HDF_SORTUP;\r
191                                         } else if (order == SortOrder.Descending) { // 降順\r
192                                                 hdi.fmt = hdi.fmt & ~HDF_SORTUP | HDF_SORTDOWN;\r
193                                         }\r
194                                         \r
195                                         // SendMessage(hWnd, HDM_SETITEMW, i, &hdi);\r
196                                         SendMessage(hWnd, 0x120c, i, ref hdi);\r
197                                 }\r
198                         } catch (Exception) {\r
199                         }\r
200                 }\r
201                 \r
202                 #endregion\r
203                 \r
204                 #endregion\r
205                 \r
206                 #region EnableWindow(コメントアウト)\r
207 //              /// <summary>\r
208 //              /// 指定されたコントロール(ウィンドウ)への、\r
209 //              /// キーボード入力およびマウス入力を有効化または無効化\r
210 //              /// </summary>\r
211 //              /// <param name="control">対象のコントロールのハンドラ</param>\r
212 //              /// <param name="bEnable">有効にするか無効にするかを指定</param>\r
213 //              /// <returns>直前にウィンドウが無効状態だった場合はtrueを返す</returns>\r
214 //              public static bool Control_EnableWindow(Control ctrl, bool bEnable)\r
215 //              {\r
216 //                      try {\r
217 //                              return EnableWindow(ctrl.Handle, bEnable);\r
218 //                      } catch {\r
219 //                              ctrl.Enabled = bEnable;\r
220 //                              return true;\r
221 //                      }\r
222 //              }\r
223 //              \r
224 //              /// <summary>\r
225 //              /// 指定されたコントロール(ウィンドウ)への、\r
226 //              /// キーボード入力およびマウス入力を有効化または無効化\r
227 //              /// </summary>\r
228 //              /// <param name="control">対象のコントロールのハンドラ</param>\r
229 //              /// <param name="bEnable">有効にするか無効にするかを指定</param>\r
230 //              /// <returns>直前にウィンドウが無効状態だった場合はtrueを返す</returns>\r
231 //              [DllImport("user32.dll")]\r
232 //              public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);\r
233                 #endregion\r
234                 \r
235                 [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
236                 internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);\r
237                 \r
238                 [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
239                 internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, int wParam, ref HD_ITEM lParam);\r
240 \r
241                 [DllImport("uxtheme.dll", ExactSpelling=true, CharSet=CharSet.Unicode)]\r
242                 internal static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList);\r
243         }\r
244 }\r