c# - 使用 winAPI 修改另一个应用程序的窗口样式

标签 c#

我的应用程序启动了另一个应用程序。因此,我想删除使用 c# 启动的应用程序的标题栏。

我该怎么做,从下面的代码开始?

//Get current style
lCurStyle = GetWindowLong(hwnd, GWL_STYLE)

//remove titlebar elements
lCurStyle = lCurStyle And Not WS_CAPTION
lCurStyle = lCurStyle And Not WS_SYSMENU
lCurStyle = lCurStyle And Not WS_THICKFRAME
lCurStyle = lCurStyle And Not WS_MINIMIZE
lCurStyle = lCurStyle And Not WS_MAXIMIZEBOX

//apply new style
SetWindowLong hwnd, GWL_STYLE, lCurStyle

//reapply a 3d border
lCurStyle = GetWindowLong(hwnd, GWL_EXSTYLE)

SetWindowLong hwnd, GWL_EXSTYLE, lCurStyle Or WS_EX_DLGMODALFRAME

//redraw
SetWindowPos hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED

最佳答案

#region Constants
//Finds a window by class name
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

//Sets a window to be a child window of another window
[DllImport("USER32.DLL")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

//Sets window attributes
[DllImport("USER32.DLL")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

//Gets window attributes
[DllImport("USER32.DLL")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll")]
static extern IntPtr GetMenu(IntPtr hWnd);

[DllImport("user32.dll")]
static extern int GetMenuItemCount(IntPtr hMenu);

[DllImport("user32.dll")]
static extern bool DrawMenuBar(IntPtr hWnd);

[DllImport("user32.dll")]
static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);

//assorted constants needed
public static uint MF_BYPOSITION = 0x400;
public static uint MF_REMOVE = 0x1000;
public static int GWL_STYLE = -16;
public static int WS_CHILD = 0x40000000; //child window
public static int WS_BORDER = 0x00800000; //window with border
public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar 
public static int WS_SYSMENU = 0x00080000; //window menu  
#endregion

public static void WindowsReStyle()
{ 
    Process[] Procs = Process.GetProcesses();
    foreach (Process proc in Procs)
    {
        if (proc.ProcessName.StartsWith("notepad"))
        {
            IntPtr pFoundWindow = proc.MainWindowHandle;
            int style = GetWindowLong(pFoundWindow, GWL_STYLE);

            //get menu
            IntPtr HMENU = GetMenu(proc.MainWindowHandle);
            //get item count
            int count = GetMenuItemCount(HMENU);
            //loop & remove
            for (int i = 0; i < count; i++)
                RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));

            //force a redraw
            DrawMenuBar(proc.MainWindowHandle);
            SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_SYSMENU)); 
            SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION)); 
        } 
    }
}  

关于c# - 使用 winAPI 修改另一个应用程序的窗口样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2832217/

相关文章:

c# - 我如何关闭大写锁定键

c# - 列表场景的 AutoMapper 似乎只重复映射列表中的第一个对象

c# - 如何使用 C# 在 Redis 中添加对象列表作为键的值?

c# - #CDC "Send failure: 501 out of bounds exception"向服务器发送数据时

C# udp 套接字没有收到整个消息

c# - 如何在给定设备路径的情况下获取 USB_DEVICE_DESCRIPTOR

c# - 无法使用使用serilog接收器的WriteTo.ElasticSearch进行Elasticsearch

c# - 循环到 ListView 元素

c# - 为什么不接受 IEnumerable(of T) 作为扩展方法接收器

c# - 复选框中的 asp-for 抛出和 asp.net 核心中的错误