Изменение заголовка окна сторонней программы
#39747532
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: Брянск
Сообщения: 269
|
|
Добрый день.
Нужно изменить заголовок окна сторонней программы.
Код:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowNameChange
{
class Program
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, string lParam);
private const int WM_SETTEXT = 0xC;
[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string text);
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
static string GetWindowText(IntPtr hWnd)
{
int len = GetWindowTextLength(hWnd) + 1;
StringBuilder sb = new StringBuilder(len);
len = GetWindowText(hWnd, sb, len);
return sb.ToString(0, len);
}
static void Main(string[] args)
{
EnumWindows((hWnd, lParam) =>
{
if (IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) != 0)
{
string title = GetWindowText(hWnd);
if (title.Contains("Word"))
{
Console.WriteLine(title);
string newTitle = "Блокнот-1";
IntPtr target_hwnd = hWnd;
Console.WriteLine("hwnd: " + target_hwnd);
SetForegroundWindow(target_hwnd);
SendMessage(target_hwnd, WM_SETTEXT, 0, newTitle);
//Console.WriteLine(SendMessage(target_hwnd, WM_SETTEXT, 0, "Калк"));
//SetWindowText(target_hwnd, newTitle);
}
}
return true;
}, IntPtr.Zero);
Console.Read();
}
}
}
При замене заголовка окна Блокнот срабатывает, как нужно.
При замене заголовков окна Word отображаемый заголовок не меняется.
С чем это может быть связано?
|
|