OSDN Git Service

AppliStation-GUI,ListViewの列の追加と削除をサポートした。
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / NativeMethods.cs
index 0065897..5d46eb6 100644 (file)
@@ -4,6 +4,7 @@
 using System;\r
 using System.Runtime.InteropServices;\r
 using System.Windows.Forms;\r
+using System.Drawing;\r
 \r
 namespace AppliStation.Util\r
 {\r
@@ -30,7 +31,7 @@ namespace AppliStation.Util
                        try {\r
                                // status := (PBST_NORMAL | PBST_ERROR | PBST_PAUSED)\r
                                // SendMessage(progressBar.Handle, PBM_SETSTATE, state, 0);\r
-                               SendMessage(progBar.Handle, 0x410, state, 0);\r
+                               SendMessage(progBar.Handle, 0x410, new IntPtr(state), IntPtr.Zero);\r
                        } catch (Exception) {\r
                        }\r
 #endif\r
@@ -119,7 +120,7 @@ namespace AppliStation.Util
                {\r
                        try {\r
                                // SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_DOUBLEBUFFER, bEnable? LVS_EX_DOUBLEBUFFER:0);\r
-                               SendMessage(listView.Handle, 0x1036, 0x00010000, ((bEnable)? 0x00010000u:0x0u));\r
+                               SendMessage(listView.Handle, 0x1036, new IntPtr(0x00010000), new IntPtr((bEnable)? 0x00010000u:0x0u));\r
                        } catch (Exception) {\r
                        }\r
                }\r
@@ -155,7 +156,7 @@ namespace AppliStation.Util
 #if USE_VISTA_EFFECTS\r
                        try {\r
                                // SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_AUTOCHECKSELECT, bAutoCheckSelect?LVS_EX_AUTOCHECKSELECT:0);\r
-                               SendMessage(listView.Handle, 0x1036, 0x08000000, (bAutoCheckSelect)?0x08000000u:0x0u);\r
+                               SendMessage(listView.Handle, 0x1036, new IntPtr(0x08000000), new IntPtr((bAutoCheckSelect)?0x08000000u:0x0u));\r
                        } catch (Exception) {\r
                        }\r
 #endif\r
@@ -177,12 +178,6 @@ namespace AppliStation.Util
                        public int     iOrder;\r
                }\r
                \r
-               internal static IntPtr ListView_GetHeader(ListView listview)\r
-               {\r
-                       // SendMessage(hWnd, LVM_GETHEADER, 0, NULL);\r
-                       return SendMessage(listview.Handle, 0x101E, 0, 0);\r
-               }\r
-               \r
                /// <summary>\r
                /// WinXP以降、ソートの矢印を表示\r
                /// </summary>\r
@@ -193,13 +188,13 @@ namespace AppliStation.Util
                {\r
                        try {\r
                                // SendMessage(hWnd, LVM_GETHEADER, NULL, NULL);\r
-                               IntPtr hWnd = SendMessage(listView.Handle, 0x101F, 0, 0);\r
+                               IntPtr hWnd = SendMessage(listView.Handle, 0x101F, IntPtr.Zero, IntPtr.Zero);\r
                                \r
                                HD_ITEM hdi = new HD_ITEM();\r
                                hdi.mask = 0x0004; // HDI_FORMAT;\r
                                for (int i = 0; i < listView.Columns.Count; i++) {\r
                                        // SendMessage(hWnd, HDM_GETITEMW, i, &hdi);\r
-                                       SendMessage(hWnd, 0x120b, i, ref hdi);\r
+                                       SendMessage(hWnd, 0x120b, new IntPtr(i), ref hdi);\r
                                        \r
                                        const int HDF_SORTUP = 0x400;\r
                                        const int HDF_SORTDOWN = 0x200;\r
@@ -213,7 +208,7 @@ namespace AppliStation.Util
                                        }\r
                                        \r
                                        // SendMessage(hWnd, HDM_SETITEMW, i, &hdi);\r
-                                       SendMessage(hWnd, 0x120c, i, ref hdi);\r
+                                       SendMessage(hWnd, 0x120c, new IntPtr(i), ref hdi);\r
                                }\r
                        } catch (Exception) {\r
                        }\r
@@ -294,7 +289,7 @@ namespace AppliStation.Util
                        cfmt.dwEffects = dwEffect;\r
                        \r
                        //SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION, ref cfmt);\r
-                       SendMessage(richTextBox.Handle, 0x0444, 0x0001, ref cfmt);\r
+                       SendMessage(richTextBox.Handle, 0x0444, new IntPtr(0x0001), ref cfmt);\r
                }\r
                /// <summary>\r
                /// リッチテキストにテキストリンクを追加する\r
@@ -316,14 +311,87 @@ namespace AppliStation.Util
                \r
                #endregion\r
                \r
-               [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
-               internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);\r
+               #region アイコン関連\r
+               \r
+               [DllImport("shell32.dll")]\r
+               private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);\r
+               \r
+               [DllImport("user32.dll", SetLastError=true)]\r
+               [return: MarshalAs(UnmanagedType.Bool)]\r
+               private static extern bool DestroyIcon(IntPtr hIcon);\r
+               \r
+               \r
+               /// <summary>\r
+               /// アイコンファイル(実行ファイル・DLL)を開いてアイコンを作る\r
+               /// </summary>\r
+               /// <remarks>内部でコピーされるのでWin32APIのDestroyIconを使わないでいいが、やや遅い動作</remarks>\r
+               /// <param name="form">ハンドラ</param>\r
+               /// <param name="lpszExeFileName">対象ファイル</param>\r
+               /// <param name="nIconIndex">アイコンインデックス</param>\r
+               /// <returns>生成されたアイコン</returns>\r
+               public static Icon ExtractIcon(Form form, string lpszExeFileName, uint nIconIndex)\r
+               {\r
+                       Icon ico = null;\r
+                       \r
+                       if (! System.IO.File.Exists(lpszExeFileName)) {\r
+                               ico = Icon.ExtractAssociatedIcon(lpszExeFileName); // ExtractAssociatedIconに例外を吐いてもらう\r
+                       } else {\r
+                               IntPtr hInst = (form != null)? form.Handle : IntPtr.Zero;\r
+                               IntPtr hIcon = IntPtr.Zero;\r
+                               \r
+                               try {\r
+                                       hIcon = ExtractIcon(hInst, lpszExeFileName, (int) nIconIndex);\r
+                                       if ((hIcon != IntPtr.Zero) && (hIcon.ToInt32() != 2)) {\r
+                                               ico = (Icon) Icon.FromHandle(hIcon).Clone();\r
+                                               DestroyIcon(hIcon);\r
+                                       }\r
+                               } catch (System.Runtime.InteropServices.COMException) {\r
+                                       // ExtraIconのP/Invoke失敗時用\r
+                               }\r
+                       }\r
+                       return ico;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// ユーザ昇格が必要か設定し、設定必要なときシールドアイコンを表示する。\r
+               /// </summary>\r
+               /// <param name="button">対象のボタン</param>\r
+               /// <param name="required">ユーザ昇格が必要か否か、すなわちシールドアイコンを表示するか</param>           \r
+               public static void Button_SetElevationRequiredState(Button button, bool required)\r
+               {\r
+                       if (Environment.OSVersion.Version.Major >= 6) {\r
+                               button.FlatStyle = FlatStyle.System;\r
+                               // SendMessage(hWnd, BCM_SETSHIELD, 0, required);\r
+                               SendMessage(button.Handle, 0x160C, IntPtr.Zero, new IntPtr((required)? 1u : 0u));\r
+                       } else { // Legacy OS\r
+                               // FlatStyle.System に設定されている場合、Image プロパティに割り当てられているイメージは表示されない対策\r
+                               if (button.FlatStyle == FlatStyle.System) {\r
+                                       button.FlatStyle = FlatStyle.Standard;\r
+                               }\r
+                               \r
+                               button.TextImageRelation = TextImageRelation.ImageBeforeText;\r
+                               if (required) {\r
+                                       button.ImageAlign = ContentAlignment.MiddleLeft;\r
+                                       using (Icon ico = new Icon(SystemIcons.Shield, 16, 16)) {\r
+                                               button.Image = ico.ToBitmap();\r
+                                       }\r
+                               } else {\r
+                                       button.Image = null;\r
+                               }\r
+                               button.AutoSize = true;\r
+                       }\r
+               }\r
+               \r
+               #endregion\r
+               \r
+               [DllImport("user32.dll", CharSet = CharSet.Auto)]\r
+               internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);\r
                \r
                [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
-               private static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, int wParam, ref HD_ITEM lParam);\r
+               private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref HD_ITEM lParam);\r
                \r
                [DllImport("user32.dll", CharSet=CharSet.Auto)]\r
-               private static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, int wParam, ref CHARFORMAT2 lParam);\r
+               private static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, ref CHARFORMAT2 lParam);\r
 \r
                [DllImport("uxtheme.dll", ExactSpelling=true, CharSet=CharSet.Unicode)]\r
                internal static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList);\r