당신은 포커스를 설정하기 위해 WindowsApi.SetFocus 방법을 사용할 수 있습니다 : 여기
도 그것의 스크린 샷이다.
an external application의 특정 컨트롤에 포커스를 설정하는 데이 메서드를 사용할 수 있으므로 타사 컨트롤에서 응용 프로그램에서 작동해야합니다. 여기
다른 옵션 - 윈폼에서 외부 응용 프로그램에서 컨트롤에 대한 포커스를 설정하는 코드의 작업 블록 :
[DllImport("user32.dll")]
static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
[DllImport("user32.dll")]
static extern bool AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);
[DllImport("user32.dll", SetLastError = true)]
static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, string lParam);
private delegate bool EnumWindowsProc(IntPtr hWnd, string className);
[DllImport("user32.dll")]
static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType, uint cchType);
void SetFocus(IntPtr hwndTarget, string childClassName)
{
// hwndTarget is the other app's main window
// ...
IntPtr targetThreadID = GetWindowThreadProcessId(hwndTarget, IntPtr.Zero); //target thread id
IntPtr myThreadID = GetCurrentThread(); // calling thread id, our thread id
bool lRet = AttachThreadInput(myThreadID, targetThreadID, true); // attach current thread id to target window
// if it's not already in the foreground...
lRet = BringWindowToTop(hwndTarget);
SetForegroundWindow(hwndTarget);
// Enumerate and find target to set focus on
EnumChildWindows(hwndTarget, OnChildWindow, childClassName);
}
List<object> windowHandles = new List<object>();
static bool OnChildWindow(IntPtr handle, string className)
{
// Find class of current handle
StringBuilder pszType = new StringBuilder();
pszType.Capacity = 255;
RealGetWindowClass(handle, pszType, (UInt32)pszType.Capacity);
string s = pszType.ToString();
// Remove (potentially) unneeded parts of class
if (s.Length > className.Length)
{
s = s.Substring(0, className.Length);
}
// Set focus on correct control
if (s == className)
{
SetFocus(handle);
}
return true;
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeComponent();
SetFocus(this.Handle, "<ClassName from Spy++>");
}
당신이 스파이를 사용할 수있는 텍스트 상자의 클래스 이름을 모르는 경우 ++
폼의 컨트롤 이름을 알고 있습니다 ... "this"대신 첫 번째 줄에 사용해야합니다. – Bohn
코드를 컴파일 할 때 어떤 네임 스페이스를 포함해야합니까? – Bohn
컨트롤의 클래스 이름을 기반으로 창에서 특정 컨트롤의 포커스를 설정하는 코드 전체가 업데이트되었습니다. – Blachshma