如何创建带边框的表单,但没有标题栏? (比如Windows 7上的音量控制)

时间:2020-12-20 15:49:28

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?

在Windows 7中,音量混合器窗口具有特定样式,具有粗糙透明边框,但没有标题栏。如何在winforms窗口中重新创建窗口样式?

如何创建带边框的表单,但没有标题栏? (比如Windows 7上的音量控制)

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

我尝试将Text设置为string.Empty,将ControlBox设置为false,这将删除标题栏,但边框也会消失:

如何创建带边框的表单,但没有标题栏? (比如Windows 7上的音量控制)

3 个解决方案

#1


32  

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):

对于固定大小的窗口,您仍应使用FormBorderStyle.SizableToolWindow,但您可以覆盖窗体的WndProc以忽略非客户端命中测试(用于切换到大小调整游标):

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.

如果要真正强制执行大小,还可以在表单上将MinimumSize设置为MaximumSize。

#2


1  

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.

因为“这个编辑旨在解决帖子的作者,并且没有任何意义作为编辑。它应该被写成评论或答案。”我将Chris的回答作为一个新答案进行编辑。

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.

代码的答案如上所述 - 除了它还可以防止任何客户区鼠标事件发生。您需要返回1(如在HTCLIENT中)来修复它。

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


0  

form.FormBorderStyle = FormBorderStyle.Fixed3D;

form.FormBorderStyle = FormBorderStyle.Fixed3D;

#1


32  

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):

对于固定大小的窗口,您仍应使用FormBorderStyle.SizableToolWindow,但您可以覆盖窗体的WndProc以忽略非客户端命中测试(用于切换到大小调整游标):

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.

如果要真正强制执行大小,还可以在表单上将MinimumSize设置为MaximumSize。

#2


1  

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.

因为“这个编辑旨在解决帖子的作者,并且没有任何意义作为编辑。它应该被写成评论或答案。”我将Chris的回答作为一个新答案进行编辑。

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.

代码的答案如上所述 - 除了它还可以防止任何客户区鼠标事件发生。您需要返回1(如在HTCLIENT中)来修复它。

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


0  

form.FormBorderStyle = FormBorderStyle.Fixed3D;

form.FormBorderStyle = FormBorderStyle.Fixed3D;