110,538
社区成员
发帖
与我相关
我的任务
分享
public class Popup
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int width, int height, int flags);
[DllImport("user32.dll")]
private static extern int GetWindowRect(int hwnd, ref RECT lpRect);
[DllImport("user32.dll")]
private static extern IntPtr SetActiveWindow(IntPtr hwnd);
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private static void Optimize(Primitives.Popup popup)
{
var hwnd = ((HwndSource)PresentationSource.FromVisual(popup.Child)).Handle;
RECT rect = default(RECT);
int r = GetWindowRect((int)hwnd, ref rect);
if (r == 0) return;
double Width = !double.IsNaN(popup.Width) && popup.Width > 0 ? popup.Width : popup.ActualWidth;
double Height = !double.IsNaN(popup.Height) && popup.Height > 0 ? popup.Height : popup.ActualHeight;
SetWindowPos(hwnd, -1, rect.Left, rect.Top, (int)Width, (int)Height, 0);
}
private static List<int> instances = new List<int>();
public static void AutoRender()
{
EventManager.RegisterClassHandler(typeof(Primitives.Popup), Primitives.Popup.PreviewGotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnPreviewGotKeyboardFocus), true);
}
private static void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
try
{
var textBox = e.NewFocus as TextBoxBase;
if (textBox == null) return;
IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(textBox)).Handle;
if (hwnd != IntPtr.Zero && !instances.Contains(hwnd.ToInt32()))
{
instances.Add(hwnd.ToInt32());
SetActiveWindow(hwnd);
}
}
catch (Exception) { }
}
}
除popup外,暂未发现有输入法问题