11

I am working in C#. I know that this question is commonly asked, it's just that I still cannot fully hide the top bar when I set the the form text string to be "" and controlbox = false.

I still want the shadow effect:

shadow effect

The border on the side is gone and there is the usual shadow, but the top border has the odd white line that I remove.

The buttons on the upper right-hand side are generated by me and show the edge of my editable form. The white space above that is what I am trying to remove.

I do not want to set the form border property to none as I enjoy the integrated sizable controls and form shadow, so that is not an option.

Are there any other suggestions for this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user3811284
  • 123
  • 2
  • 7
  • 1
    Did you try this: [How to create a form with a border, but no title bar? (like volume control on Windows 7)](http://stackoverflow.com/questions/3594086/how-to-create-a-form-with-a-border-but-no-title-bar-like-volume-control-on-wi) – Reza Aghaei May 22 '16 at 02:21
  • @RezaAghaei Yes I did, I actually read that before posting this. form.FormBorderStyle = FormBorderStyle.SizableToolWindow; I what I have set as my border, yet it still looks the same. – user3811284 May 22 '16 at 04:08
  • Hi. Got any luck on this one? I'm facing the same issue, tried many solutions with no luck... – StackUseR May 15 '20 at 08:35

3 Answers3

0

I know this post is old but I stumbled on to this problem and finally solved it so I'm posting it here for anyone that it can help.

So, I made a custom resizable borderless Form class with my own control bar and buttons, similar to the OP. The idea was to have that Form, with all the related WndProc code, act as the base class for all the other dialog forms in the project.

As I was running my project I was getting the exact same white line at the top of my form even though I had set all the appropriate form properties correctly.

The problem ended up being that with Form inheritance, all derived Forms also have their own private InitializeComponent method. I had no idea that the VS IDE set the FormBorderStyle property to Sizable in the derived Form class because my attention was only on the custom base class.

If you are using a custom base class for your Form, setting FormBorderStyle correctly in the derived class fixes the problem.

drankin2112
  • 4,715
  • 1
  • 15
  • 20
  • Can you help me with a sample? I can't get your idea. – haiduong87 Dec 20 '19 at 03:44
  • While this appears to be a solution, it's not written well as an answer, it's written more as a comment. I'd recommend reading "[answer]" and its linked pages, then rewrite this more suscintly with some example code. – the Tin Man Feb 19 '22 at 20:34
0

If you click the Form, go to Properties to FormBorderSyle and put it in False.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mume
  • 130
  • 6
0

If you just don't set FormBorderStyle to None for a shadow, I already answered how to easily make shadows in "Drop shadow on Borderless Winform-No flicker or disappearance. " Here's my answer:

Try these steps and revert back if you have any errors:

  1. Add this code to a new file named 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
         }
     }
    
  2. In your form, add this line below InitializeComponent():

     (new Core.DropShadow()).ApplyShadows(this);
    
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
D J
  • 845
  • 1
  • 13
  • 27