[DllImport( "imm32.dll")]
public static extern bool ImmGetOpenStatus (IntPtr hIMC);
[ DllImport("imm32.dll" )]
public static extern IntPtr ImmGetContext (IntPtr hWnd);
[ DllImport("imm32.dll" )]
public static extern int ImmReleaseContext (IntPtr hWnd, IntPtr hIMC);
[ DllImport("imm32.dll" )]
public static extern bool ImmSetCompositionWindow (IntPtr hIMC, ref COMPOSITIONFORM lpCompForm);
[ StructLayout(LayoutKind .Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[ StructLayout(LayoutKind .Sequential)]
public struct POINTAPI
{
public int X;
public int Y;
}
[ StructLayout(LayoutKind .Sequential)]
public struct COMPOSITIONFORM
{
public uint dwStyle;
public POINTAPI ptCurrentPos;
public RECT rcArea;
}
const int EM_GETSEL = 0xB0;
const int EM_LINEFROMCHAR = 0xC9;
const int EM_LINEINDEX = 0xBB;
[ DllImport("user32.dll" , EntryPoint = "SendMessage")]
public static extern int SendMessage (
int hwnd ,
int wMsg ,
int wParam ,
ref int lParam
);
private Point GetCursorPos( int TextHwnd )
{
int i = 0, j = 0, k = 0;
int lParam = 0, wParam = 0;
i = SendMessage (TextHwnd, EM_GETSEL, wParam , ref lParam);
j = i / 65536;
int lineNo = SendMessage( TextHwnd, EM_LINEFROMCHAR , j, ref lParam ) + 1;
k = SendMessage (TextHwnd, EM_LINEINDEX, -1, ref lParam);
int colNo = j - k + 1;
Point ret = new Point(lineNo , colNo);
return ret ;
}
void textBox_KeyDown(object sender, KeyEventArgs e )
{
IntPtr it = ((System. Windows.Interop .HwndSource) PresentationSource.FromVisual (rBox)). Handle;
IntPtr hImc = ImmGetContext( it);
if (ImmGetOpenStatus (hImc))
{
COMPOSITIONFORM cf = new COMPOSITIONFORM();
cf.dwStyle = 2;
//Point p = GetCursorPos(it.ToInt32());
//Point p1 = this.PointToScreen(p);
Point p2 = this. TranslatePoint(new Point(0, 0), CVisualTreeHelper.GetParent <Window>( this));
cf.ptCurrentPos .X = ( int)p2 .X;
cf.ptCurrentPos .Y = ( int)p2 .Y + 300;
bool setcom = ImmSetCompositionWindow( hImc, ref cf);
}
ImmReleaseContext(hImc , it);
}
资料
msdn: https://msdn.microsoft.com/en-us/library/dd318653(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/dd317764(v=VS.85).aspx
CFS_DEFAULT Move the composition window to the default position. The IME window can display the composition window outside the client area, such as in a floating window.
CFS_FORCE_POSITION Display the upper left corner of the composition window at exactly the position specified by ptCurrentPos. The coordinates are relative to the upper left corner of the window containing the composition window and are not subject to adjustment by the IME.
CFS_POINT Display the upper left corner of the composition window at the position specified by ptCurrentPos. The coordinates are relative to the upper left corner of the window containing the composition window and are subject to adjustment by the IME.
CFS_RECT Display the composition window at the position specified by rcArea. The coordinates are relative to the upper left of the window containing the composition window.
http://dev.firnow.com/course/4_webprogram/asp.net/netjs/2008215/99962_35.html
Public Const CFS_DEFAULT = &H0
Public Const CFS_RECT = &H1
Public Const CFS_POINT = &H2
Public Const CFS_SCREEN = &H4
Public Const CFS_FORCE_POSITION = &H20
Public Const CFS_CANDIDATEPOS = &H40
Public Const CFS_EXCLUDE = &H80
CFS_DEFAULT Move the composition window to the default position. The IME window can display the composition window outside the client area, such as in a floating window.
CFS_FORCE_POSITION Display the upper left corner of the composition window at exactly the position specified by ptCurrentPos. The coordinates are relative to the upper left corner of the window containing the composition window and are not subject to adjustment by the IME.
CFS_POINT Display the upper left corner of the composition window at the position specified by ptCurrentPos. The coordinates are relative to the upper left corner of the window containing the composition window and are subject to adjustment by the IME.
CFS_RECT Display the composition window at the position specified by rcArea. The coordinates are relative to the upper left of the window containing the composition window.
http://dev.firnow.com/course/4_webprogram/asp.net/netjs/2008215/99962_35.html
Public Const CFS_DEFAULT = &H0
Public Const CFS_RECT = &H1
Public Const CFS_POINT = &H2
Public Const CFS_SCREEN = &H4
Public Const CFS_FORCE_POSITION = &H20
Public Const CFS_CANDIDATEPOS = &H40
Public Const CFS_EXCLUDE = &H80