Code:
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
public static extern bool SetWindowText(IntPtr hWnd, String strNewWindowName);
[DllImport("user32.dll")]
public static extern int UpdateWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hwnd = IntPtr.Zero;
IntPtr hwndChild = IntPtr.Zero;
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains("Calculator"))
{
SetForegroundWindow(proc.MainWindowHandle);
Thread.Sleep(1000);
hwnd = GetForegroundWindow();
//MessageBox.Show(hwnd.ToString());
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "Backspace");
//MessageBox.Show(hwndChild.ToString());
int length = GetWindowTextLength(hwndChild);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hwndChild, sb, sb.Capacity);
MessageBox.Show(sb.ToString());
SetWindowText(hwndChild, "aaaaa"); //<== loi o day
}
}
}
đây là code mình muốn thay đổi TEXT nút Backspace của Calculator . Nhưng khi SetWindowText xong ! TEXT của BUTTON Backspace vẫn không thay dổi !
Mình thiếu ở chỗ nào :( .. bạn nào biết giúp mình với