【问题标题】:how to change the FixedSingle of another program in c#如何在c#中更改另一个程序的FixedSingle
【发布时间】:2022-10-23 15:41:05
【问题描述】:

我想用一个命令把FormBorderStyle的值改成另一个程序的Sizable值,比如用SetWindowPos命令改变窗口大小或者用SetWindowText改变c#中的标题名,但是我还没有找到所以希望大家您能否建议该命令以便我可以找到并参考它?

【问题讨论】:

  • 这里的上下文是什么?什么是固定单?
  • @JonSkeet Form.FormBorderStyle 在 WinForms 中。因此,OP 正在询问如何更改任意 Win32 顶级应用程序窗口的边框样式。
  • 您是在谈论 Windows 边框样式吗? windows API 列出了许多可用于修改当前窗口的消息和函数。也不要假设 .net-runtime 中定义的边框样式适用于非托管可执行文件
  • @戴:对。我嫌疑犯可能是这样,但问题应该是很多更清晰。
  • 您必须 pinvoke SetWindowLongPtr() 以使用 SWP_FRAMECHANGED 修改 GWL_STYLE 和 SetWindowPos() 以刷新框架。当窗口大小改变时,该程序正确保持窗口更新的可能性并不大。

标签: c#


【解决方案1】:
  private const uint GWL_STYLE = 0xFFFFFFF0;
    private const uint WS_SIZEBOX = 0x00040000;
    private const uint WS_MINIMIZEBOX = 0x00020000;
    [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
            private static extern int SetWindowLong32(IntPtr hWnd, uint nIndex, uint dwNewLong);
    
            [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
            private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, uint nIndex, uint dwNewLong);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern UInt32 GetWindowLong(IntPtr hWnd, uint nIndex);
    
            [DllImport("user32.dll")]
            private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
    public static IntPtr SetWindowLongPtr(IntPtr hWnd, uint nIndex, uint dwNewLong)
            {
                if (IntPtr.Size == 8)
                {
                    return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
                }
                else
                {
                    return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong));
                }
            }
    
            public static IntPtr GetWindowLongPtr(IntPtr hWnd, uint nIndex)
            {
                if (IntPtr.Size == 8)
                {
                    return GetWindowLongPtr(hWnd, nIndex);
                }
                else
                {
                    return new IntPtr(GetWindowLong(hWnd, nIndex));
                }
            }

更改属性

uint GSTYLE= (uint)GetWindowLongPtr(Hwnd, GWL_STYLE);
SetWindowLongPtr(Hwnd, GWL_STYLE, GSTYLE| WS_SIZEBOX);

恢复

uint GSTYLE= (uint)GetWindowLongPtr(Hwnd, GWL_STYLE);
SetWindowLongPtr(Hwnd, GWL_STYLE, GSTYLE& ~(WS_SIZEBOX));
   

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多