How can I get a window to look like a frame?

205 views Asked by At

It should look like the little box that appears when you click the Action Center, Power, Network, or Sound icon in the tray. It needs to have that glass border without the title bar.

enter image description here

It also needs to be a fixed size and not resizable. Thanks to anyone who can help! :)

1

There are 1 answers

5
John Koerner On BEST ANSWER

You need to set the ControlBox to false, clear the title text, and set the border style. Since you stated you want the sizable border, but not allow it to be resized, you can set the min and max size as well. Finally to prevent the mouse cursor from showing the resize cursor, we override the WM_NCHITTEST result if they are on one of the borders:

private void Form1_Load(object sender, EventArgs e)
{
    this.ControlBox = false;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
    this.MinimumSize = this.MaximumSize = this.Size; 
    this.Text = "";                
}
const int WM_NCHITTEST = 0x0084;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 16;
const int HTBOTTOMRIGHT = 17;
const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTTOP = 12;
const int HTCLIENT = 1;
protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    if (m.Msg == WM_NCHITTEST)
    {
        Console.WriteLine(m.Result.ToString());
        switch (m.Result.ToInt32())
        {
            case HTBOTTOM:
            case HTBOTTOMLEFT:
            case HTBOTTOMRIGHT:
            case HTLEFT:
            case HTRIGHT:
            case HTTOPLEFT:
            case HTTOPRIGHT:
            case HTTOP:
                m.Result =(IntPtr) HTCLIENT;
                break;
        }
    }
}