{
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>
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
{
set { complemented = value; }
}
+ private string imeText;
+ public string ImeText
+ {
+ get { return imeText; }
+ set { imeText = value; }
+ }
+
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
switch (e.KeyCode)
{
case Keys.Tab:
- if (this.dropdownForm.Visible)
+ if (this.dropdownForm.Visible && this.dropdownForm.DropdownListBox.SelectedIndex >= 0)
{
//タブキーによるコマンド補完
this.complemented = true;
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);
//e.Handled = true;
}
break;
-/*
+
case Keys.A:
if ((Keys.Control & Control.ModifierKeys) == Keys.Control || Keys.Home == e.KeyCode)
{
e.Handled = true;
}
break;
-*/
+
case Keys.Up:
if (listBox.Items.Count > 0)
{
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;
{
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