问题
After launching osk.exe with ShellExecuteEx() I would like to position the keyboard window relative to the data-entry fields, so that it doesn't cover them.
How do I set the window position for the osk before calling it?
Also, how can I have the application hide the osk when I am finished?
回答1:
You can use FindWindow using the window class "OSKMainClass" to get the window handle, and then SetWindowPos on that handle to position it to the coordinates you want. (You may need to use the control's ClientToScreen method to convert to the proper coordinates, but I'll let you figure that part out.)
// Off the top of my head - not at a machine that has a Delphi compiler at
// the moment.
var
OSKWnd: HWnd;
begin
OSKWnd := FindWindow(PChar('OSKMainClass'), nil);
if OSKWnd <> 0 then
begin
SetWindowPos(OSKWnd,
HWND_BOTTOM,
NewPos.Left,
NewPos.Top,
NewPos.Width,
NewPos.Height,
0);
end;
end;
Code taken in part from a CodeProject article related to the same topic. I got the window class using AutoHotKey's Window Spy utility.
Notes:
Remy Lebeau points out in a comment that you should make sure to use
CreateProcess()
orShellExecuteEx()
so that you get back a process handle that can then be passed toWaitForInputIdle()
before callingFindWindow()
. Otherwise the call toFindWindow()
may happen before OSK creates the window.mghie points out in a comment that the only way he could get this to work was by running the app as Administrator; otherwise the call to
SetWindowPos()
resulted in an "Access Denied (5)".
回答2:
I can,t move the window as mentioned above. Using Win32 commands, SetWindow or MoveWindow not worked for on screen keyboard. Its worked only while running exe in admin privilege.
I think its not a good solution. I found another solution. Please go through this. After trying using registry values its worked well i can move on screen keyboard in my application
try
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Osk", true);
myKey.SetValue("WindowLeft", oskLeft, RegistryValueKind.DWord);
myKey.SetValue("WindowTop", oskTop, RegistryValueKind.DWord);
}
catch
{
//Log the error
}
来源:https://stackoverflow.com/questions/24707902/how-to-position-the-on-screen-keyboard-window