Skip to content

Instantly share code, notes, and snippets.

@sayurin
Created March 4, 2014 05:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sayurin/9340629 to your computer and use it in GitHub Desktop.
Save sayurin/9340629 to your computer and use it in GitHub Desktop.
WinFormsのTextBoxでfocusを失う時にIMEへの入力文字を確定させる
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sayuri.Windows.Forms {
class TextBox2 : TextBox {
[DllImport("Imm32.dll")]
static extern IntPtr ImmGetContext(IntPtr hWnd);
[DllImport("Imm32.dll")]
static extern bool ImmGetOpenStatus(IntPtr hIMC);
[DllImport("Imm32.dll")]
static extern bool ImmNotifyIME(IntPtr hIMC, int dwAction, int dwIndex, int dwValue);
[DllImport("Imm32.dll")]
static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
const int NI_SELECTCANDIDATESTR = 0x0015;
const int CPS_COMPLETE = 0x0001;
protected override void OnLostFocus(EventArgs e) {
var context = ImmGetContext(Handle);
if (context != IntPtr.Zero) {
if (ImmGetOpenStatus(context))
ImmNotifyIME(context, NI_SELECTCANDIDATESTR, CPS_COMPLETE, 0);
ImmReleaseContext(Handle, context);
}
base.OnLostFocus(e);
}
}
}
namespace Sayuri.Windows.Forms
open System.Runtime.InteropServices
open System.Windows.Forms
type TextBox2 () =
inherit TextBox ()
[<DllImport "Imm32.dll">]
static extern nativeint ImmGetContext(nativeint hWnd)
[<DllImport "Imm32.dll">]
static extern bool ImmGetOpenStatus(nativeint hIMC)
[<DllImport "Imm32.dll">]
static extern bool ImmNotifyIME(nativeint hIMC, uint32 dwAction, uint32 dwIndex, uint32 dwValue)
[<DllImport "Imm32.dll">]
static extern bool ImmReleaseContext(nativeint hWnd, nativeint hIMC)
[<Literal>]
static let NI_SELECTCANDIDATESTR = 0x0015u
[<Literal>]
static let CPS_COMPLETE = 0x0001u
override this.OnLostFocus e =
let context = ImmGetContext this.Handle
if context <> 0n then
if ImmGetOpenStatus context then
ImmNotifyIME(context, NI_SELECTCANDIDATESTR, CPS_COMPLETE, 0u) |> ignore
ImmReleaseContext(this.Handle, context) |> ignore
base.OnLostFocus e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment