0

I need to use an OnScreenKeyboard Component like Native Windows OSK in my Wpf application. I can call the UI by Process.Start("osk.exe") but the UI appears on top of the main ui window. I want to start the OSK app just bottom of my app. Is this possible with any arguments? -e.g. process.StartInfo.WindowPosition=xxx- I'd prefer to use it before I create my own Component.

Kubi
  • 2,139
  • 6
  • 35
  • 62
  • The main window of *osk.exe* uses the [WS_EX_TOPMOST](https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543.aspx) extended window style, so it is always in front of all other non-topmost windows. This is to be expected, since the user will have to interact with the OSK. Obscuring it by another window pretty much defies that purpose. If you need to (temporarily) hide the OSK, call [ShowWindow](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548.aspx) on its top-level window. A more intrusive way would be to remove the `WS_EX_TOPMOST` style (`SetWindowLongPtr`). – IInspectable Nov 12 '15 at 13:46

1 Answers1

0

OK. I solved my problem with Win32 SetWindowPos method. Sharing if it might help others. Below code is opening and resizing/positioning the Native OSK.exe. It places the keyboard relative to the mouse position.

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public static implicit operator Point(POINT point)
    {
        return new Point(point.X, point.Y);
    }
}

    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out POINT lpPoint);

    public static Point GetCursorPosition()
    {
        POINT lpPoint;
        GetCursorPos(out lpPoint);
        //bool success = User32.GetCursorPos(out lpPoint);
        // if (!success)

        return lpPoint;
    }


    private void OSKMenuItem_Click(object sender, RoutedEventArgs e)
    {
        Process osk = new Process();
        osk.StartInfo.FileName = "osk.exe";
        osk.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

        osk.Start();

        Task.Delay(1000).ContinueWith((a) => {
            Point p = GetCursorPosition();

            try
            {
                IntPtr target_hwnd = FindWindowByCaption(IntPtr.Zero, "On-Screen Keyboard");
                if (target_hwnd == IntPtr.Zero)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("Error");
                }
                SetWindowPos(target_hwnd, IntPtr.Zero, (int)p.X - 200, (int)p.Y, 1300, 600, 0);
            }
            catch (Exception ex)
            {

                //throw;
            }
        });
Kubi
  • 2,139
  • 6
  • 35
  • 62