OSDN Git Service

v2.6.0.0 draft
[passer/Passer2.git] / components / AutoCompleteTextBox.cs
index 5ff5739..b837ab2 100644 (file)
@@ -19,6 +19,19 @@ namespace Lugens.Components
 {
     public partial class AutoCompleteTextBox : TextBox, IComparer<IconListBoxItem>
     {
+        [DllImport("Imm32.dll")]
+        private static extern IntPtr ImmGetContext(IntPtr hWnd);
+
+        [DllImport("Imm32.dll")]
+        private static extern int ImmGetCompositionString(
+            IntPtr hIMC, int dwIndex, byte[] buf, int dwBufLen);
+
+        [DllImport("Imm32.dll")]
+        private static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
+
+        // IME入力中文字取得に使用する値(ひらがな)
+        private const int GCS_COMPSTR = 0x0008;
+
         /// <summary>
         /// オートコンプリートドロップダウンリスト表示の最大件数
         /// </summary>
@@ -40,10 +53,20 @@ namespace Lugens.Components
         private bool showableDropdownList = true;
         public bool ShowableDropdownList { get { return this.showableDropdownList; } set { this.showableDropdownList = value; } }
 
+        /// <summary>
+        /// タブ補完時のイベントハンドラ
+        /// </summary>
+        /// <param name="type"></param>
         public delegate void ComplementEventHandler(IconListBoxType type);
-
         public event ComplementEventHandler Complement;
 
+        /// <summary>
+        /// IME変換時のイベントハンドラ
+        /// </summary>
+        /// <param name="type"></param>
+        public delegate void ImeCompositionEventHandler(int type, string text);
+        public event ImeCompositionEventHandler ImeComposition;
+
         private bool complemented = false;
         public bool Complemented
         {
@@ -51,6 +74,13 @@ namespace Lugens.Components
             set { complemented = value; }
         }
 
+        private string imeText;
+        public string ImeText
+        {
+            get { return imeText; }
+            set { imeText = value; }
+        }
+
         protected override void Dispose(bool disposing)
         {
             base.Dispose(disposing);
@@ -98,7 +128,7 @@ namespace Lugens.Components
             switch (e.KeyCode)
             {
                 case Keys.Tab:
-                    if (this.dropdownForm.Visible)
+                    if (this.dropdownForm.Visible && this.dropdownForm.DropdownListBox.SelectedIndex >= 0)
                     {
                         //タブキーによるコマンド補完
                         this.complemented = true;
@@ -110,6 +140,7 @@ namespace Lugens.Components
                             case IconListBoxType.File:
                             case IconListBoxType.History:
                             case IconListBoxType.SysCommand:
+                            case IconListBoxType.SearchEngine:
                                 this.Text = item.Text;
                                 this.SelectionStart = this.Text.Length;
                                 this.dropdownForm.DoVisibleChange(false);
@@ -119,7 +150,7 @@ namespace Lugens.Components
                         //e.Handled = true;
                     }
                     break;
-/*
+
                 case Keys.A:
                     if ((Keys.Control & Control.ModifierKeys) == Keys.Control || Keys.Home == e.KeyCode)
                     {
@@ -127,7 +158,7 @@ namespace Lugens.Components
                         e.Handled = true;
                     }
                     break;
-*/
+
                 case Keys.Up:
                     if (listBox.Items.Count > 0)
                     {
@@ -203,8 +234,11 @@ namespace Lugens.Components
                 case Keys.Enter:
                     if (this.dropdownForm.Visible)
                     {
-                        this.Text = this.dropdownForm.DropdownListBox.Text;
-                        this.SelectionStart = this.Text.Length;
+                        if (this.dropdownForm.DropdownListBox.SelectedIndex >= 0)
+                        {
+                            this.Text = this.dropdownForm.DropdownListBox.Text;
+                            this.SelectionStart = this.Text.Length;
+                        }
                         this.dropdownForm.DoVisibleChange(false);
                     }
                     break;
@@ -318,5 +352,27 @@ namespace Lugens.Components
         {
             this.SetDropdownFormLocation();
         }
+
+        protected override void WndProc(ref Message m)
+        {
+            switch (m.Msg)
+            {
+                case 0x010F: //WM_IME_COMPOSITION
+                    //if (((int)m.LParam & GCS_COMPSTR) == GCS_COMPSTR)
+                    //{
+                        IntPtr hIMC = ImmGetContext(this.Handle);
+                        StringBuilder sb = new StringBuilder(1024);
+                        byte[] buffer = new byte[1024];
+                        int length = ImmGetCompositionString(hIMC, GCS_COMPSTR, buffer, 1024);
+                        ImmReleaseContext(this.Handle, hIMC);
+                        string str = Encoding.GetEncoding("Shift_JIS").GetString(buffer);
+                        str = str.TrimEnd('\0');
+                        this.ImeComposition((int)m.LParam, str);
+                    //}
+                    break;
+            }
+            base.WndProc(ref m);
+        }
+
     }
 }
\ No newline at end of file