How to create a form with a border, but no title bar? (like volume control on Windows 7)

坚强是说给别人听的谎言 提交于 2019-11-26 15:54:42

问题


In Windows 7, the volume mixer windows has a specific style, with a thick, transparent border, but no title bar. How do i recreate that window style in a winforms window?

I tried setting Text to string.Empty, and ControlBox to false, which removes the titlebar, but then the border also disappears:


回答1:


form.Text = string.Empty;
form.ControlBox = false;
form.FormBorderStyle = FormBorderStyle.SizableToolWindow;

For a fixed size window, you should still use FormBorderStyle.SizableToolWindow, but you can override the form's WndProc to ignore non-client hit tests (which are used to switch to the sizing cursors):

protected override void WndProc(ref Message message)
{
    const int WM_NCHITTEST = 0x0084;

    if (message.Msg == WM_NCHITTEST)
        return;

    base.WndProc(ref message);
}

If you want to really enforce the size, you could also set MinimumSize equal to MaximumSize on the form.




回答2:


Since "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer." I present an edit to Chris' answer as a new answer.

The code his answer works as described - except that it also prevents any client area mouse event to occur. You need to return 1 (as in HTCLIENT) to fix that.

protected override void WndProc(ref Message message)
{
    const int WM_NCHITTEST = 0x0084;
    const int HTCLIENT = 0x01;

    if (message.Msg == WM_NCHITTEST)
    {
        message.Result = new IntPtr(HTCLIENT);
        return;
    }

    base.WndProc(ref message);
}



回答3:


form.FormBorderStyle = FormBorderStyle.Fixed3D;



来源:https://stackoverflow.com/questions/3594086/how-to-create-a-form-with-a-border-but-no-title-bar-like-volume-control-on-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!