先參考dll
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
private System.Diagnostics.Process softKey;
然后啟動代碼如下
private void button1_Click(object sender, EventArgs e)
{
//打開軟鍵盤
try
{
IntPtr ptr = new IntPtr();
bool isWow64FsRedirectionDisabled = Wow64DisableWow64FsRedirection(ref ptr);
if (isWow64FsRedirectionDisabled)
{
softKey = System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\osk.exe");
bool isWow64FsRedirectionReverted = Wow64RevertWow64FsRedirection(ptr);
}
// 上面的陳述句在打開軟鍵盤后,系統還沒用立刻把軟鍵盤的視窗創建出來了。所以下面的代碼用回圈來查詢視窗是否創建,只有創建了視窗
// FindWindow才能找到視窗句柄,才可以移動視窗的位置和設定視窗的大小。這里是關鍵。
IntPtr intptr = IntPtr.Zero;
while (IntPtr.Zero == intptr)
{
System.Threading.Thread.Sleep(100);
intptr = FindWindow(null, "螢屏鍵盤");
}
// 獲取螢屏尺寸
int iActulaWidth = Screen.PrimaryScreen.Bounds.Width;
int iActulaHeight = Screen.PrimaryScreen.Bounds.Height;
// 設定軟鍵盤的顯示位置,底部居中
int posX = (iActulaWidth - 1000) / 2;
int posY = (iActulaHeight - 300);
//設定鍵盤顯示位置
MoveWindow(intptr, posX, posY, 1000, 300, true);
//設定軟鍵盤到前端顯示
SetForegroundWindow(intptr);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
軟鍵盤的句柄intptr都可以獲取到,但是無法移動,但是把findwindow尋找的表單改為“計算器”的話,就可以順利移動,其他一些表單也都可以移動,唯獨軟鍵盤無法移動
請問是不是權限問題或者軟鍵盤表單最前端造成的
有沒有什么辦法可以讓win10系統下軟鍵盤出現在想要的位置
uj5u.com熱心網友回復:
軟鍵盤是不跟你玩的……如果沒有輸入法要求的話,我推薦使用自己的軟鍵盤,請看 [sketchpunk/openkeyboard: Virtual keyboard with dynamic user defined layouts](https://github.com/sketchpunk/openkeyboard )
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/238846.html
標籤:C#
