powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Изменение заголовка окна сторонней программы
1 сообщений из 1, страница 1 из 1
Изменение заголовка окна сторонней программы
    #39747532
Фотография mraklbrw
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Добрый день.
Нужно изменить заголовок окна сторонней программы.

Код:
Код: c#
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 отображаемый заголовок не меняется.
С чем это может быть связано?
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Изменение заголовка окна сторонней программы
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]