没有控制框的 Windows 窗体对话框图标

Vaj*_*jda 5 .net c# winforms

我想知道是否有办法在禁用控制框、最小化框和最大化框的情况下在自定义对话框的左上角显示图标?单击图标时我不需要任何功能(关于、关闭、移动等)。我只是想要它看起来更好看。

Mik*_*ant 3

没有控制框=>没有图标...

当 ControlBox 被禁用时,窗体 windowstyle WS_SYSMENU 标志被(以某种方式以一种很远的方式)删除,因此 Windows 无法显示图标。实际上,我还没有找到关于为什么(以及如何)右上角图标在没有 WS_SYSMENU 的情况下继续存在的最终解释......但找到了一个更适合您需求的好解决方案)

    private const int GWL_STYLE = -16;
    private const int WS_CLIPSIBLINGS = 1 << 26;

    [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")]
    public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, HandleRef dwNewLong);
    [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "GetWindowLong")]
    public static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);

    protected override void OnLoad(EventArgs e) {
        int style = (int)((long)GetWindowLong32(new HandleRef(this, this.Handle), GWL_STYLE));
        SetWindowLongPtr32(new HandleRef(this, this.Handle), GWL_STYLE, new HandleRef(null, (IntPtr)(style & ~WS_CLIPSIBLINGS)));

        base.OnLoad(e);
    }
Run Code Online (Sandbox Code Playgroud)