Fork me on GitHub

修改窗体的Title

直接上代码

 

 /// <summary>
        /// 获取窗体的名称
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lpString"></param>
        /// <param name="nMaxCount"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern int GetWindowTextW(IntPtr hWnd, IntPtr lpString, int nMaxCount);

        [DllImport("user32.dll")]
        static extern int SetWindowText(IntPtr hWnd, string text);
        private const int NumChars = 256;
        public static string GetWindowTitle(IntPtr hwnd)
        {
            IntPtr intPtr = Marshal.AllocHGlobal(NumChars);
            GetWindowTextW(hwnd, intPtr, 100);
            var s = Marshal.PtrToStringUni(intPtr);
            Marshal.FreeHGlobal(intPtr);
            return s;
        }

        private async Task StartMyNotepad()
        {
            IntPtr handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
           var setResult = SetWindowText(handle, "测试进程");
            Console.WriteLine(GetWindowTitle(handle));

            this.Visibility = Visibility.Collapsed;

        }

  需要再Load完成后再调用 StartMyNotepad()佛足将设置失败

posted @ 2022-02-14 15:03  黄高林  阅读(89)  评论(0编辑  收藏  举报