使用c#删除外部应用程序的标题栏

发布于 2024-09-01 00:52:38 字数 1604 浏览 5 评论 0原文

我的应用程序启动另一个外部应用程序。

我想在该外部应用程序启动后删除其标题栏。

这是否可行?如果可行,将如何实现?

根据评论,我正在使用下面的工作代码

//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);


//assorted constants needed
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 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);
            SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION));
        }
    }
}

My application starts up another external application.

I want to remove the title bar of this external application once it has started.

Is this feasible, and if so how would it be done?

Based on the comments I am using the working code below

//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);


//assorted constants needed
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 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);
            SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION));
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吹泡泡o 2024-09-08 00:52:38

无需注入任何内容,您只需使用 API 即可修改 Windows 样式位,例如,这适用于记事本,但 YMMV 取决于您正在使用的应用程序。

替代文本

//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

No need to inject anything, you can just modify the windows style bits as using the API, e.g. this works for Notepad, however YMMV depending on the app you're playing with.

alt text

//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
︶葆Ⅱㄣ 2024-09-08 00:52:38
Process[] processes = Process.GetProcessesByName("notepad");
IntPtr windowHandle = processes[0].MainWindowHandle;

const int GWL_STYLE = (-16); 
const UInt32 WS_VISIBLE = 0x10000000;
SetWindowLong(windowHandle, GWL_STYLE, (WS_VISIBLE));`
Process[] processes = Process.GetProcessesByName("notepad");
IntPtr windowHandle = processes[0].MainWindowHandle;

const int GWL_STYLE = (-16); 
const UInt32 WS_VISIBLE = 0x10000000;
SetWindowLong(windowHandle, GWL_STYLE, (WS_VISIBLE));`
寂寞陪衬 2024-09-08 00:52:38

好吧,Alex 从未详细说明过代码,至少这不是一个即插即用的解决方案,但这仍然主要归功于他......这有点错误,除非您使用“SetParent”来放置它在某种容器中(例如表单或面板)只是想我会分享结果。

Visual Basic:

Option Strict On
Public Class Form1
    Const WS_BORDER As Integer = 8388608
    Const WS_DLGFRAME As Integer = 4194304
    Const WS_CAPTION As Integer = WS_BORDER Or WS_DLGFRAME
    Const WS_SYSMENU As Integer = 524288
    Const WS_THICKFRAME As Integer = 262144
    Const WS_MINIMIZE As Integer = 536870912
    Const WS_MAXIMIZEBOX As Integer = 65536
    Const GWL_STYLE As Integer = -16&
    Const GWL_EXSTYLE As Integer = -20&
    Const WS_EX_DLGMODALFRAME As Integer = &H1L
    Const SWP_NOMOVE As Integer = &H2
    Const SWP_NOSIZE As Integer = &H1
    Const SWP_FRAMECHANGED As Integer = &H20
    Const MF_BYPOSITION As UInteger = &H400
    Const MF_REMOVE As UInteger = &H1000
    Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Declare Auto Function SetWindowPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
    Public Sub MakeExternalWindowBorderless(ByVal MainWindowHandle As IntPtr)
        Dim Style As Integer
        Style = GetWindowLong(MainWindowHandle, GWL_STYLE)
        Style = Style And Not WS_CAPTION
        Style = Style And Not WS_SYSMENU
        Style = Style And Not WS_THICKFRAME
        Style = Style And Not WS_MINIMIZE
        Style = Style And Not WS_MAXIMIZEBOX
        SetWindowLong(MainWindowHandle, GWL_STYLE, Style)
        Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE)
        SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style Or WS_EX_DLGMODALFRAME)
        SetWindowPos(MainWindowHandle, New IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED)
    End Sub
End Class

C Sharp(C#)

using System.Runtime.InteropServices;
public class Form1
{
    const int WS_BORDER = 8388608;
    const int WS_DLGFRAME = 4194304;
    const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
    const int WS_SYSMENU = 524288;
    const int WS_THICKFRAME = 262144;
    const int WS_MINIMIZE = 536870912;
    const int WS_MAXIMIZEBOX = 65536;
    const int GWL_STYLE = -16L;
    const int GWL_EXSTYLE = -20L;
    const int WS_EX_DLGMODALFRAME = 0x1L;
    const int SWP_NOMOVE = 0x2;
    const int SWP_NOSIZE = 0x1;
    const int SWP_FRAMECHANGED = 0x20;
    const uint MF_BYPOSITION = 0x400;
    const uint MF_REMOVE = 0x1000;
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    public void MakeExternalWindowBorderless(IntPtr MainWindowHandle)
    {
        int Style = 0;
        Style = GetWindowLong(MainWindowHandle, GWL_STYLE);
        Style = Style & ~WS_CAPTION;
        Style = Style & ~WS_SYSMENU;
        Style = Style & ~WS_THICKFRAME;
        Style = Style & ~WS_MINIMIZE;
        Style = Style & ~WS_MAXIMIZEBOX;
        SetWindowLong(MainWindowHandle, GWL_STYLE, Style);
        Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE);
        SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME);
        SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
    }
}

再次感谢 Alex

Well, Alex never elaborated with the code, well at least it wasn't a plug-n-play solution, but still the main credit for this goes to him... It IS kind of buggy unless you use "SetParent" to put it in some kind of container(such as a form or panel) Just thought I would share the result.

Visual Basic:

Option Strict On
Public Class Form1
    Const WS_BORDER As Integer = 8388608
    Const WS_DLGFRAME As Integer = 4194304
    Const WS_CAPTION As Integer = WS_BORDER Or WS_DLGFRAME
    Const WS_SYSMENU As Integer = 524288
    Const WS_THICKFRAME As Integer = 262144
    Const WS_MINIMIZE As Integer = 536870912
    Const WS_MAXIMIZEBOX As Integer = 65536
    Const GWL_STYLE As Integer = -16&
    Const GWL_EXSTYLE As Integer = -20&
    Const WS_EX_DLGMODALFRAME As Integer = &H1L
    Const SWP_NOMOVE As Integer = &H2
    Const SWP_NOSIZE As Integer = &H1
    Const SWP_FRAMECHANGED As Integer = &H20
    Const MF_BYPOSITION As UInteger = &H400
    Const MF_REMOVE As UInteger = &H1000
    Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Declare Auto Function SetWindowPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
    Public Sub MakeExternalWindowBorderless(ByVal MainWindowHandle As IntPtr)
        Dim Style As Integer
        Style = GetWindowLong(MainWindowHandle, GWL_STYLE)
        Style = Style And Not WS_CAPTION
        Style = Style And Not WS_SYSMENU
        Style = Style And Not WS_THICKFRAME
        Style = Style And Not WS_MINIMIZE
        Style = Style And Not WS_MAXIMIZEBOX
        SetWindowLong(MainWindowHandle, GWL_STYLE, Style)
        Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE)
        SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style Or WS_EX_DLGMODALFRAME)
        SetWindowPos(MainWindowHandle, New IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED)
    End Sub
End Class

C Sharp(C#)

using System.Runtime.InteropServices;
public class Form1
{
    const int WS_BORDER = 8388608;
    const int WS_DLGFRAME = 4194304;
    const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
    const int WS_SYSMENU = 524288;
    const int WS_THICKFRAME = 262144;
    const int WS_MINIMIZE = 536870912;
    const int WS_MAXIMIZEBOX = 65536;
    const int GWL_STYLE = -16L;
    const int GWL_EXSTYLE = -20L;
    const int WS_EX_DLGMODALFRAME = 0x1L;
    const int SWP_NOMOVE = 0x2;
    const int SWP_NOSIZE = 0x1;
    const int SWP_FRAMECHANGED = 0x20;
    const uint MF_BYPOSITION = 0x400;
    const uint MF_REMOVE = 0x1000;
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    public void MakeExternalWindowBorderless(IntPtr MainWindowHandle)
    {
        int Style = 0;
        Style = GetWindowLong(MainWindowHandle, GWL_STYLE);
        Style = Style & ~WS_CAPTION;
        Style = Style & ~WS_SYSMENU;
        Style = Style & ~WS_THICKFRAME;
        Style = Style & ~WS_MINIMIZE;
        Style = Style & ~WS_MAXIMIZEBOX;
        SetWindowLong(MainWindowHandle, GWL_STYLE, Style);
        Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE);
        SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME);
        SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
    }
}

Again, Thank you Alex

我三岁 2024-09-08 00:52:38

这与之前提出的问题非常相似,我很确定答案是你做不到。 (或者,如果可以的话,您需要深入研究 Windows API,这可能具有挑战性,具体取决于您的经验。)

如何将按钮添加到其他应用程序窗口标题栏(XP/Vista)

This is very similar to a previously asked question, and I'm pretty sure that the answer is you can't do it. (or, if you can, you need to dig into the Windows API, which can be challenging, depending on your experience.)

How to add button to other apps window title bar (XP/Vista)

水水月牙 2024-09-08 00:52:38

如果是同步融合,则不会。如果没有,是的。

VB:

Const WS_BORDER As Integer = 8388608
Const WS_DLGFRAME As Integer = 4194304
Const WS_CAPTION As Integer = WS_BORDER Or WS_DLGFRAME
Const WS_SYSMENU As Integer = 524288
Const WS_THICKFRAME As Integer = 262144
Const WS_MINIMIZE As Integer = 536870912
Const WS_MAXIMIZEBOX As Integer = 65536
Const GWL_STYLE As Integer = -16&
Const GWL_EXSTYLE As Integer = -20&
Const WS_EX_DLGMODALFRAME As Integer = &H1L
Const SWP_NOMOVE As Integer = &H2
Const SWP_NOSIZE As Integer = &H1
Const SWP_FRAMECHANGED As Integer = &H20
Const MF_BYPOSITION As UInteger = &H400
Const MF_REMOVE As UInteger = &H1000
Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Declare Auto Function SetWindowPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
Public Sub MakeExternalWindowBorderless(ByVal MainWindowHandle As IntPtr)
    Dim Style As Integer
    Style = GetWindowLong(MainWindowHandle, GWL_STYLE)
    Style = Style And Not WS_CAPTION
    Style = Style And Not WS_SYSMENU
    Style = Style And Not WS_THICKFRAME
    Style = Style And Not WS_MINIMIZE
    Style = Style And Not WS_MAXIMIZEBOX
    SetWindowLong(MainWindowHandle, GWL_STYLE, Style)
    Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE)
    SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style Or WS_EX_DLGMODALFRAME)
    SetWindowPos(MainWindowHandle, New IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED)
End Sub

C#:

const int WS_BORDER = 8388608;
const int WS_DLGFRAME = 4194304;
const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
const int WS_SYSMENU = 524288;
const int WS_THICKFRAME = 262144;
const int WS_MINIMIZE = 536870912;
const int WS_MAXIMIZEBOX = 65536;
const int GWL_STYLE = -16L;
const int GWL_EXSTYLE = -20L;
const int WS_EX_DLGMODALFRAME = 0x1L;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_FRAMECHANGED = 0x20;
const uint MF_BYPOSITION = 0x400;
const uint MF_REMOVE = 0x1000;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
public void MakeExternalWindowBorderless(IntPtr MainWindowHandle)
{
    int Style = 0;
    Style = GetWindowLong(MainWindowHandle, GWL_STYLE);
    Style = Style & ~WS_CAPTION;
    Style = Style & ~WS_SYSMENU;
    Style = Style & ~WS_THICKFRAME;
    Style = Style & ~WS_MINIMIZE;
    Style = Style & ~WS_MAXIMIZEBOX;
    SetWindowLong(MainWindowHandle, GWL_STYLE, Style);
    Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE);
    SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME);
    SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
}

If Syncfusion, no. If not, yes.

VB:

Const WS_BORDER As Integer = 8388608
Const WS_DLGFRAME As Integer = 4194304
Const WS_CAPTION As Integer = WS_BORDER Or WS_DLGFRAME
Const WS_SYSMENU As Integer = 524288
Const WS_THICKFRAME As Integer = 262144
Const WS_MINIMIZE As Integer = 536870912
Const WS_MAXIMIZEBOX As Integer = 65536
Const GWL_STYLE As Integer = -16&
Const GWL_EXSTYLE As Integer = -20&
Const WS_EX_DLGMODALFRAME As Integer = &H1L
Const SWP_NOMOVE As Integer = &H2
Const SWP_NOSIZE As Integer = &H1
Const SWP_FRAMECHANGED As Integer = &H20
Const MF_BYPOSITION As UInteger = &H400
Const MF_REMOVE As UInteger = &H1000
Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Declare Auto Function SetWindowPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
Public Sub MakeExternalWindowBorderless(ByVal MainWindowHandle As IntPtr)
    Dim Style As Integer
    Style = GetWindowLong(MainWindowHandle, GWL_STYLE)
    Style = Style And Not WS_CAPTION
    Style = Style And Not WS_SYSMENU
    Style = Style And Not WS_THICKFRAME
    Style = Style And Not WS_MINIMIZE
    Style = Style And Not WS_MAXIMIZEBOX
    SetWindowLong(MainWindowHandle, GWL_STYLE, Style)
    Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE)
    SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style Or WS_EX_DLGMODALFRAME)
    SetWindowPos(MainWindowHandle, New IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED)
End Sub

C#:

const int WS_BORDER = 8388608;
const int WS_DLGFRAME = 4194304;
const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
const int WS_SYSMENU = 524288;
const int WS_THICKFRAME = 262144;
const int WS_MINIMIZE = 536870912;
const int WS_MAXIMIZEBOX = 65536;
const int GWL_STYLE = -16L;
const int GWL_EXSTYLE = -20L;
const int WS_EX_DLGMODALFRAME = 0x1L;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_FRAMECHANGED = 0x20;
const uint MF_BYPOSITION = 0x400;
const uint MF_REMOVE = 0x1000;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
public void MakeExternalWindowBorderless(IntPtr MainWindowHandle)
{
    int Style = 0;
    Style = GetWindowLong(MainWindowHandle, GWL_STYLE);
    Style = Style & ~WS_CAPTION;
    Style = Style & ~WS_SYSMENU;
    Style = Style & ~WS_THICKFRAME;
    Style = Style & ~WS_MINIMIZE;
    Style = Style & ~WS_MAXIMIZEBOX;
    SetWindowLong(MainWindowHandle, GWL_STYLE, Style);
    Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE);
    SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME);
    SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
}
酒浓于脸红 2024-09-08 00:52:38

如果其他人需要,Alex 答案的 Python 版本:

import win32gui

#  get a handle to the window
windowHandle = win32gui.FindWindowEx(None, None, None, "Untitled - Notepad")

GWL_STYLE = -16  #  see https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlonga

#  get current window style
currentStyle = win32gui.GetWindowLong(windowHandle, GWL_STYLE)

#  remove titlebar elements
currentStyle = currentStyle & ~(0x00C00000)  #  WS_CAPTION
currentStyle = currentStyle & ~(0x00080000)  #  WS_SYSMENU
currentStyle = currentStyle & ~(0x00040000)  #  WS_THICKFRAME
currentStyle = currentStyle & ~(0x20000000)  #  WS_MINIMIZE
currentStyle = currentStyle & ~(0x00010000)  #  WS_MAXIMIZEBOX

#  apply new style
win32gui.SetWindowLong(windowHandle, GWL_STYLE, currentStyle)

Python version of Alex's answer if someone else needs:

import win32gui

#  get a handle to the window
windowHandle = win32gui.FindWindowEx(None, None, None, "Untitled - Notepad")

GWL_STYLE = -16  #  see https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlonga

#  get current window style
currentStyle = win32gui.GetWindowLong(windowHandle, GWL_STYLE)

#  remove titlebar elements
currentStyle = currentStyle & ~(0x00C00000)  #  WS_CAPTION
currentStyle = currentStyle & ~(0x00080000)  #  WS_SYSMENU
currentStyle = currentStyle & ~(0x00040000)  #  WS_THICKFRAME
currentStyle = currentStyle & ~(0x20000000)  #  WS_MINIMIZE
currentStyle = currentStyle & ~(0x00010000)  #  WS_MAXIMIZEBOX

#  apply new style
win32gui.SetWindowLong(windowHandle, GWL_STYLE, currentStyle)
我恋#小黄人 2024-09-08 00:52:38

一般来说,除非您正在启动的应用程序直接支持(例如,如果它需要一个命令行开关来删除标题栏),否则您不能这样做。

您只能控制 ProcessStartInfo 类(即打开一个新窗口、启动最小化/最大化等...)。

In general, you can't do that unless there is direct support for in by the application you are starting (for example, if it takes a command line switch for removal of the title bar).

You can only control the things that are already present on the ProcessStartInfo class (i.e. open a new window, start minimized/maximized etc...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文