OSDN Git Service

AppliStation,パフォーマンスチューニング,SendMessage引数型修正など(動作変更なし)
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / NativeMethods.cs
index d6f7964..9bf00f2 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
@@ -130,7 +131,6 @@ namespace AppliStation.Util
                /// </summary>\r
                /// <remarks>Vista未満のバージョンでは何もしない。</remarks>\r
                /// <param name="listView">対象のListView</param>\r
-               [System.Diagnostics.ConditionalAttribute("USE_VISTA_EFFECTS")]\r
                public static void ListView_EnableVistaExplorerTheme(ListView listView)\r
                {\r
 #if USE_VISTA_EFFECTS\r
@@ -156,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
@@ -165,7 +165,7 @@ namespace AppliStation.Util
                #region ColumnHeaderのソートの三角印用\r
                \r
                [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]\r
-               internal struct HD_ITEM\r
+               private struct HD_ITEM\r
                {\r
                        public uint    mask;\r
                        public int     cxy;\r
@@ -178,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
@@ -194,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
@@ -214,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
@@ -253,13 +247,150 @@ namespace AppliStation.Util
 //             public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);\r
                #endregion\r
                \r
+               #region RichTextBox関連\r
+               \r
+               [StructLayout(LayoutKind.Sequential)]\r
+               private struct CHARFORMAT2\r
+               {\r
+                       public int cbSize;\r
+                       public uint dwMask;\r
+                       public uint dwEffects;\r
+                       public int yHeight;\r
+                       public int yOffset;\r
+                       public int crTextColor;\r
+                       public byte bCharSet;\r
+                       public byte bPitchAndFamily;\r
+                       [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]\r
+                       public char[] szFaceName;\r
+                       public short wWeight;\r
+                       public short sSpacing;\r
+                       public int crBackColor;\r
+                       public int LCID;\r
+                       public uint dwReserved;\r
+                       public short sStyle;\r
+                       public short wKerning;\r
+                       public byte bUnderlineType;\r
+                       public byte bAnimation;\r
+                       public byte bRevAuthor;\r
+                       public byte bReserved1;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// リッチテキストの選択部分に文字フォーマット効果を与える\r
+               /// </summary>\r
+               /// <param name="richTextBox">対象のリッチテキストボックス</param>\r
+               /// <param name="dwMask">有効な文字フォーマット効果</param>\r
+               /// <param name="dwEffect">文字フォーマット効果のフラグ</param>\r
+               public static void RichTextBox_SetSelectionFormat(RichTextBox richTextBox, uint dwMask, uint dwEffect)\r
+               {\r
+                       CHARFORMAT2 cfmt = new CHARFORMAT2();\r
+                       cfmt.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2));\r
+                       cfmt.dwMask = dwMask;\r
+                       cfmt.dwEffects = dwEffect;\r
+                       \r
+                       //SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION, ref cfmt);\r
+                       SendMessage(richTextBox.Handle, 0x0444, new IntPtr(0x0001), ref cfmt);\r
+               }\r
+               /// <summary>\r
+               /// リッチテキストにテキストリンクを追加する\r
+               /// </summary>\r
+               /// <param name="richTextBox">対象のリッチテキストボックス</param>\r
+               /// <param name="text">追加するテキスト</param>\r
+               public static void RichTextBox_AddTextLink(RichTextBox richTextBox, string text)\r
+               {\r
+                       int pos = richTextBox.TextLength;\r
+                       richTextBox.Select(pos,0);\r
+                       richTextBox.SelectedText = text;\r
+                       richTextBox.Select(pos, text.Length);\r
+                       \r
+                       // RichTextBox_SetSelectionFormat(richTextBox, CFM_LINK, CFE_LINK);\r
+                       RichTextBox_SetSelectionFormat(richTextBox, 0x00000020, 0x0020);\r
+                       \r
+                       richTextBox.Select(richTextBox.TextLength, 0);\r
+               }\r
+               \r
+               #endregion\r
+               \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
+                               button.ImageAlign = ContentAlignment.MiddleLeft;\r
+                               using (Icon ico = new Icon(SystemIcons.Shield, 16, 16)) {\r
+                                       button.Image = ico.ToBitmap();\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
-               internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 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
-               internal static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, int wParam, ref HD_ITEM 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
+               \r
        }\r
 }\r