簡體   English   中英

無法在 Windows 窗體 C# 中完全隱藏頂部欄

[英]Can't Fully Hide Top Bar in Windows Form C#

我在 C# 中工作。 我知道這個問題很常見,只是我仍然無法完全隱藏頂部欄。 這是我將表單文本字符串設置為 "" 和controlbox = false

還是想要陰影效果:

還是想要陰影效果

所以你可以看到側面的邊框消失了(太棒了!)並且有通常的陰影(太棒了!)但頂部邊框有這條奇怪的白線,我似乎無法刪除。

我不想將表單邊框屬性設置為“無”,因為我喜歡集成的可觀控件和表單陰影,所以這不是一個選項。 對此還有其他建議嗎?

提前致謝!

(我應該指定右上角的按鈕由我生成並顯示我的可編輯表單的邊緣。上面的空白是我想要刪除的。)

我知道這篇文章很舊,但我偶然發現了這個問題並最終解決了它,所以我將它發布在這里,以供任何可以提供幫助的人使用。

因此,我使用自己的控制欄和按鈕制作了一個自定義的可調整大小的無邊框 Form 類,類似於 OP。 這個想法是讓那個帶有所有相關WndProc代碼的表單作為項目中所有其他對話框表單的基類。

當我運行我的項目時,即使我已經正確設置了所有適當的表單屬性,我還是在表單頂部看到了完全相同的白線。

問題最終是通過 Form 繼承,所有派生的 Forms 也有自己的私有InitializeComponent方法。 我不知道 VS IDE 在派生的 Form 類中將 FormBorderStyle 屬性設置為Sizable ,因為我只關注自定義基類。

如果您為Form使用自定義基類,則在派生類中正確設置FormBorderStyle解決問題。

如果單擊Form ,請轉到屬性,然后將出現FormBorderSyle部分並將其設置為False

如果您只是沒有將 FormBorderStyle 設置為 None 來表示陰影,我已經在這里回答了如何輕松制作陰影: 在無邊框 Winform 上放置陰影-無閃爍或消失

這是我的回答:

請嘗試以下步驟並恢復任何錯誤:

將以下代碼添加到名為 DropShadow.cs 的新代碼文件中;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Core
{
    public class DropShadow
    {
        #region Shadowing

        #region Fields

        private bool _isAeroEnabled = false;
        private bool _isDraggingEnabled = false;
        private const int WM_NCHITTEST = 0x84;
        private const int WS_MINIMIZEBOX = 0x20000;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;
        private const int CS_DBLCLKS = 0x8;
        private const int CS_DROPSHADOW = 0x00020000;
        private const int WM_NCPAINT = 0x0085;
        private const int WM_ACTIVATEAPP = 0x001C;

        #endregion

        #region Structures

        [EditorBrowsable(EditorBrowsableState.Never)]
        public struct MARGINS
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;
        }

        #endregion

        #region Methods

        #region Public

        [DllImport("dwmapi.dll")]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

        [DllImport("dwmapi.dll")]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        [DllImport("dwmapi.dll")]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

        [EditorBrowsable(EditorBrowsableState.Never)]
        public static bool IsCompositionEnabled()
        {
            if (Environment.OSVersion.Version.Major < 6) return false;

            bool enabled;
            DwmIsCompositionEnabled(out enabled);

            return enabled;
        }

        #endregion

        #region Private

        [DllImport("dwmapi.dll")]
        private static extern int DwmIsCompositionEnabled(out bool enabled);

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect,
            int nTopRect,
            int nRightRect,
            int nBottomRect,
            int nWidthEllipse,
            int nHeightEllipse
         );

        private bool CheckIfAeroIsEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                int enabled = 0;
                DwmIsCompositionEnabled(ref enabled);

                return (enabled == 1) ? true : false;
            }
            return false;
        }

        #endregion

        #region Overrides

        public void ApplyShadows(Form form)
        {
            var v = 2;

            DwmSetWindowAttribute(form.Handle, 2, ref v, 4);

            MARGINS margins = new MARGINS()
            {
                bottomHeight = 1,
                leftWidth = 0,
                rightWidth = 0,
                topHeight = 0
            };

            DwmExtendFrameIntoClientArea(form.Handle, ref margins);
        }

        #endregion

        #endregion

        #endregion
    }
}

在您的表單中,在 InitializeComponent(); 下方添加這一行;

(new Core.DropShadow()).ApplyShadows(this);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM