OSDN Git Service

AppliStation-GUI,UserPrefForm.csへのコメント追加
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / NativeMethods.cs
1 // Vistaの効果を有効にするフラグ(不要の場合はコメントアウト)\r
2 #define USE_VISTA_EFFECTS\r
3 \r
4 using System;\r
5 using System.Runtime.InteropServices;\r
6 using System.Windows.Forms;\r
7 \r
8 namespace AppliStation.Util\r
9 {\r
10         /// <summary>\r
11         /// Win32ネイティブメソッドを叩いてGUI操作するための関数群のクラス\r
12         /// </summary>\r
13         public sealed class NativeMethods\r
14         {\r
15                 /// <summary>\r
16                 /// 呼び出し禁止\r
17                 /// </summary>\r
18                 private NativeMethods()\r
19                 {\r
20                 }\r
21 \r
22                 /// <summary>\r
23                 /// WindowsVista向け、プログレスバーステータス(色)を設定する\r
24                 /// </summary>\r
25                 /// <param name="progBar">対象のプログレスバー</param>\r
26                 /// <param name="state">状態。(1:Normal,2:Error,3:Paused)</param>\r
27                 public static void ProgressBar_SetState(ProgressBar progBar, uint state)\r
28                 {\r
29 #if USE_VISTA_EFFECTS\r
30                         try {\r
31                                 // status := (PBST_NORMAL | PBST_ERROR | PBST_PAUSED)\r
32                                 // SendMessage(progressBar.Handle, PBM_SETSTATE, state, 0);\r
33                                 SendMessage(progBar.Handle, 0x410, state, 0);\r
34                         } catch (Exception) {\r
35                         }\r
36 #endif\r
37                 }\r
38                 \r
39                 #region タスクバーおよびタイトルバーのフラッシュ\r
40                 \r
41                 /// <summary>\r
42                 /// タスクバーおよびタイトルバーボタンのフラッシュの設定フラグ\r
43                 /// </summary>\r
44                 public enum FlashFlag : uint {\r
45                         /// <summary>\r
46                         /// 点滅の停止\r
47                         /// </summary>\r
48                         Stop = 0,\r
49                         /// <summary>\r
50                         /// タイトルバーを点滅\r
51                         /// </summary>\r
52                         Caption = 1,\r
53                         /// <summary>\r
54                         /// タスクバーボタンを点滅\r
55                         /// </summary>\r
56                         Tray = 2,\r
57                         /// <summary>\r
58                         /// タイトルバーとタスクバーボタンを点滅\r
59                         /// </summary>\r
60                         All = 3,\r
61                         /// <summary>\r
62                         /// Stopが設定されるまで点滅する\r
63                         /// </summary>\r
64                         Timer = 4,\r
65                         /// <summary>\r
66                         /// フォアグラウンドの状態になるまで点滅\r
67                         /// </summary>\r
68                         TimerNoFG = 12,\r
69                 }\r
70                 \r
71                 [StructLayout(LayoutKind.Sequential)]\r
72                 struct FLASHWINFO\r
73                 {\r
74                     public int cbSize;\r
75                     public IntPtr hWnd;\r
76                     public FlashFlag dwFlags;\r
77                     public uint uCount;\r
78                     public uint dwTimeout;\r
79                 }\r
80                 \r
81                 /// <summary>\r
82                 /// タスクバーおよびタイトルバーボタンを点滅させる\r
83                 /// </summary>\r
84                 /// <param name="form">対象フォーム</param>\r
85                 /// <param name="flag">点滅パラメータフラグ</param>\r
86                 /// <param name="count">点滅回数</param>\r
87                 /// <param name="timeout">点滅の間隔(ミリ秒)</param>\r
88                 /// <returns></returns>\r
89                 public static bool Form_FlashWindow(Form form, FlashFlag flag, uint count, uint timeout)\r
90                 {\r
91                         try {\r
92                                 FLASHWINFO info = new FLASHWINFO();\r
93                                 info.cbSize = Marshal.SizeOf(typeof(FLASHWINFO));\r
94                                 info.hWnd = form.Handle;\r
95                                 info.dwFlags = flag;\r
96                                 info.uCount = count;\r
97                                 info.dwTimeout = timeout;\r
98                                 \r
99                                 return FlashWindowEx(ref info) == 0;\r
100                         } catch (Exception) {\r
101                                 return false;\r
102                         }\r
103                 }\r
104                 \r
105                 [DllImport("user32.dll")]\r
106                 static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);\r
107                 \r
108                 #endregion\r
109                 \r
110                 #region ListView関連\r
111                 \r
112                 /// <summary>\r
113                 /// リストビューにダブルバッファでの描画をするか否かを設定する\r
114                 /// </summary>\r
115                 /// <remarks>マウスでの選択に半透明ツールを採用するか否かもこの設定に依存</remarks>\r
116                 /// <param name="listView">対象のリストビュー</param>\r
117                 /// <param name="bEnable">ダブルバッファでの描画をするとき<code>true</code></param>\r
118                 public static void ListView_SetDoubleBuffer(ListView listView, bool bEnable)\r
119                 {\r
120                         try {\r
121                                 // SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_DOUBLEBUFFER, bEnable? LVS_EX_DOUBLEBUFFER:0);\r
122                                 SendMessage(listView.Handle, 0x1036, 0x00010000, ((bEnable)? 0x00010000u:0x0u));\r
123                         } catch (Exception) {\r
124                         }\r
125                 }\r
126                 \r
127                 /// <summary>\r
128                 /// 選択されたアイテムの部分の背景にグラデーションがかかった感じになる、\r
129                 /// Vista以降でのエクスプローラの見た目をListViewに反映させる。\r
130                 /// </summary>\r
131                 /// <remarks>Vista未満のバージョンでは何もしない。</remarks>\r
132                 /// <param name="listView">対象のListView</param>\r
133                 public static void ListView_EnableVistaExplorerTheme(ListView listView)\r
134                 {\r
135 #if USE_VISTA_EFFECTS\r
136                         // Vista未満はなにもしない\r
137                         OperatingSystem os = Environment.OSVersion;\r
138                         if (os.Platform != PlatformID.Win32NT || os.Version.Major < 6) return;\r
139                         \r
140                         try {\r
141                                 SetWindowTheme(listView.Handle, "explorer", null);\r
142                         } catch (Exception) {\r
143                         }\r
144 #endif\r
145                 }\r
146                 \r
147                 /// <summary>\r
148                 /// ヘッダに"すべて選択"に似たチェックボックスを作るか否かを指定する\r
149                 /// </summary>\r
150                 /// <remarks>このオプションを設定するとVistaエクスプローラでの「チェックボックスを使用して項目を選択する」と同様の動作になる</remarks>\r
151                 /// <param name="listView">対象のListBox</param>\r
152                 /// <param name="bAutoCheckSelect">チェックボックスを使用して項目を選択するとき<code>true</code></param>\r
153                 public static void ListView_SetAutoCheckSelect(ListView listView, bool bAutoCheckSelect)\r
154                 {\r
155 #if USE_VISTA_EFFECTS\r
156                         try {\r
157                                 // SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_AUTOCHECKSELECT, bAutoCheckSelect?LVS_EX_AUTOCHECKSELECT:0);\r
158                                 SendMessage(listView.Handle, 0x1036, 0x08000000, (bAutoCheckSelect)?0x08000000u:0x0u);\r
159                         } catch (Exception) {\r
160                         }\r
161 #endif\r
162                 }\r
163                 \r
164                 #region ColumnHeaderのソートの三角印用\r
165                 \r
166                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]\r
167                 private struct HD_ITEM\r
168                 {\r
169                         public uint    mask;\r
170                         public int     cxy;\r
171                         [MarshalAs(UnmanagedType.LPTStr)]public string   pszText;\r
172                         public IntPtr   hbm;\r
173                         public int     cchTextMax;\r
174                         public int     fmt;\r
175                         [MarshalAs(UnmanagedType.LPTStr)]public string  lParam;\r
176                         public int     iImage;        // index of bitmap in ImageList\r
177                         public int     iOrder;\r
178                 }\r
179                 \r
180                 internal static IntPtr ListView_GetHeader(ListView listview)\r
181                 {\r
182                         // SendMessage(hWnd, LVM_GETHEADER, 0, NULL);\r
183                         return SendMessage(listview.Handle, 0x101E, 0, 0);\r
184                 }\r
185                 \r
186                 /// <summary>\r
187                 /// WinXP以降、ソートの矢印を表示\r
188                 /// </summary>\r
189                 /// <param name="listView">対象のListView</param>\r
190                 /// <param name="column">表示する矢印のヘッダ</param>\r
191                 /// <param name="order">ソートの昇順・降順</param>\r
192                 public static void ColumnHeader_SetSortState(ListView listView, int column, SortOrder order)\r
193                 {\r
194                         try {\r
195                                 // SendMessage(hWnd, LVM_GETHEADER, NULL, NULL);\r
196                                 IntPtr hWnd = SendMessage(listView.Handle, 0x101F, 0, 0);\r
197                                 \r
198                                 HD_ITEM hdi = new HD_ITEM();\r
199                                 hdi.mask = 0x0004; // HDI_FORMAT;\r
200                                 for (int i = 0; i < listView.Columns.Count; i++) {\r
201                                         // SendMessage(hWnd, HDM_GETITEMW, i, &hdi);\r
202                                         SendMessage(hWnd, 0x120b, i, ref hdi);\r
203                                         \r
204                                         const int HDF_SORTUP = 0x400;\r
205                                         const int HDF_SORTDOWN = 0x200;\r
206                                         \r
207                                         if (i != column || order == SortOrder.None) {\r
208                                                 hdi.fmt = hdi.fmt & ~(HDF_SORTUP | HDF_SORTDOWN);\r
209                                         } else if (order == SortOrder.Ascending) { // 昇順\r
210                                                 hdi.fmt = hdi.fmt & ~HDF_SORTDOWN | HDF_SORTUP;\r
211                                         } else if (order == SortOrder.Descending) { // 降順\r
212                                                 hdi.fmt = hdi.fmt & ~HDF_SORTUP | HDF_SORTDOWN;\r
213                                         }\r
214                                         \r
215                                         // SendMessage(hWnd, HDM_SETITEMW, i, &hdi);\r
216                                         SendMessage(hWnd, 0x120c, i, ref hdi);\r
217                                 }\r
218                         } catch (Exception) {\r
219                         }\r
220                 }\r
221                 \r
222                 #endregion\r
223                 \r
224                 #endregion\r
225                 \r
226                 #region EnableWindow(コメントアウト)\r
227 //              /// <summary>\r
228 //              /// 指定されたコントロール(ウィンドウ)への、\r
229 //              /// キーボード入力およびマウス入力を有効化または無効化\r
230 //              /// </summary>\r
231 //              /// <param name="control">対象のコントロールのハンドラ</param>\r
232 //              /// <param name="bEnable">有効にするか無効にするかを指定</param>\r
233 //              /// <returns>直前にウィンドウが無効状態だった場合はtrueを返す</returns>\r
234 //              public static bool Control_EnableWindow(Control ctrl, bool bEnable)\r
235 //              {\r
236 //                      try {\r
237 //                              return EnableWindow(ctrl.Handle, bEnable);\r
238 //                      } catch {\r
239 //                              ctrl.Enabled = bEnable;\r
240 //                              return true;\r
241 //                      }\r
242 //              }\r
243 //              \r
244 //              /// <summary>\r
245 //              /// 指定されたコントロール(ウィンドウ)への、\r
246 //              /// キーボード入力およびマウス入力を有効化または無効化\r
247 //              /// </summary>\r
248 //              /// <param name="control">対象のコントロールのハンドラ</param>\r
249 //              /// <param name="bEnable">有効にするか無効にするかを指定</param>\r
250 //              /// <returns>直前にウィンドウが無効状態だった場合はtrueを返す</returns>\r
251 //              [DllImport("user32.dll")]\r
252 //              public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);\r
253                 #endregion\r
254                 \r
255                 #region RichTextBox関連\r
256                 \r
257                 [StructLayout(LayoutKind.Sequential)]\r
258                 private struct CHARFORMAT2\r
259                 {\r
260                         public int cbSize;\r
261                         public uint dwMask;\r
262                         public uint dwEffects;\r
263                         public int yHeight;\r
264                         public int yOffset;\r
265                         public int crTextColor;\r
266                         public byte bCharSet;\r
267                         public byte bPitchAndFamily;\r
268                         [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]\r
269                         public char[] szFaceName;\r
270                         public short wWeight;\r
271                         public short sSpacing;\r
272                         public int crBackColor;\r
273                         public int LCID;\r
274                         public uint dwReserved;\r
275                         public short sStyle;\r
276                         public short wKerning;\r
277                         public byte bUnderlineType;\r
278                         public byte bAnimation;\r
279                         public byte bRevAuthor;\r
280                         public byte bReserved1;\r
281                 }\r
282                 \r
283                 /// <summary>\r
284                 /// リッチテキストの選択部分に文字フォーマット効果を与える\r
285                 /// </summary>\r
286                 /// <param name="richTextBox">対象のリッチテキストボックス</param>\r
287                 /// <param name="dwMask">有効な文字フォーマット効果</param>\r
288                 /// <param name="dwEffect">文字フォーマット効果のフラグ</param>\r
289                 public static void RichTextBox_SetSelectionFormat(RichTextBox richTextBox, uint dwMask, uint dwEffect)\r
290                 {\r
291                         CHARFORMAT2 cfmt = new CHARFORMAT2();\r
292                         cfmt.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2));\r
293                         cfmt.dwMask = dwMask;\r
294                         cfmt.dwEffects = dwEffect;\r
295                         \r
296                         //SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION, ref cfmt);\r
297                         SendMessage(richTextBox.Handle, 0x0444, 0x0001, ref cfmt);\r
298                 }\r
299                 /// <summary>\r
300                 /// リッチテキストにテキストリンクを追加する\r
301                 /// </summary>\r
302                 /// <param name="richTextBox">対象のリッチテキストボックス</param>\r
303                 /// <param name="text">追加するテキスト</param>\r
304                 public static void RichTextBox_AddTextLink(RichTextBox richTextBox, string text)\r
305                 {\r
306                         int pos = richTextBox.TextLength;\r
307                         richTextBox.Select(pos,0);\r
308                         richTextBox.SelectedText = text;\r
309                         richTextBox.Select(pos, text.Length);\r
310                         \r
311                         // RichTextBox_SetSelectionFormat(richTextBox, CFM_LINK, CFE_LINK);\r
312                         RichTextBox_SetSelectionFormat(richTextBox, 0x00000020, 0x0020);\r
313                         \r
314                         richTextBox.Select(richTextBox.TextLength, 0);\r
315                 }\r
316                 \r
317                 #endregion\r
318                 \r
319                 [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
320                 internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);\r
321                 \r
322                 [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
323                 private static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, int wParam, ref HD_ITEM lParam);\r
324                 \r
325                 [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
326                 private static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, int wParam, ref CHARFORMAT2 lParam);\r
327 \r
328                 [DllImport("uxtheme.dll", ExactSpelling=true, CharSet=CharSet.Unicode)]\r
329                 internal static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList);\r
330                 \r
331         }\r
332 }\r