创建可通过在背景区域拖动的任何位置移动的非矩形形式 [英] Creating non-rectangular forms that can be moved by dragging anywhere in the background area

查看:223
本文介绍了创建可通过在背景区域拖动的任何位置移动的非矩形形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过左键点击并按住的背景区将我的窗体窗口,就像我们通常做用的标题栏。

然后我也希望我的应用程序(例如,矩形窗)的形式窗口切换到我的自定义设计的图形化窗口。我已经看到这种类型的许多其它应用的窗口,所以我认为这是可能的。

解决方案

问题1:

要允许拖动其工作区时,窗体移动,你需要告诉窗口管理器来处理客户区就好像它是标题栏(标题区)。你认为你的问题类似的东西。

这可以在.NET中通过重写<一做href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc.aspx"><$c$c>WndProc你的形式方法,响应 WM_NCHITTEST 消息,并返回 HTCAPTION 来表示一切都应该是处理而不是默认的 HTCLIENT ,这说明它应该被视为窗体的客户区作为标题(标题)栏的一部分。添加以下code到你的窗体类:

 私人const int的WM_NCHITTEST =的0x84;
私人const int的HTCLIENT =为0x1;
私人const int的HTCAPTION = 0X2;

保护覆盖无效的WndProc(参考消息M)
{
    base.WndProc(REF米);

    如果(m.Msg == WM_NCHITTEST)
    {
        //转换HTCLIENT到HTCAPTION
        如果(m.Result.ToInt32()== HTCLIENT)
        {
            m.Result =(IntPtr的)HTCAPTION;
        }
    }
}
 

问2:

您可以通过设置<一个创建任意,非矩形形状的一种形式href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx"><$c$c>Region你的表单属性以自定义 区域 您选择。如果您有类似的Photoshop图形程序的经验,你可以认为这是一个设置裁剪区域为表单:您所指定的范围外的窗口管理器不会画什么。中描述的形状的像素这区域甚至可以是不连续的。

创建区域最简单的方法可能是使用<一个href="http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.aspx"><$c$c>GraphicsPath一流的,然后使用构造函数区域接受单个的GraphicsPath 对象作为参数。<​​/ P>

和我假设你已经知道了,给出的第一个问题,你必须设定<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle.aspx"><$c$c>FormBorderStyle物业以,以确保由窗口管理器绘制的默认边框消失了。

不幸的是,这些区域不能被抗失真。请参见汉斯的回答这个问题有关这些限制的更多细节。

最后,值得一提的是,后一种方法来创建非矩形形式可以产生一些非常难看的用户界面,根本不提高产品的易用性,像这样:

请谨慎使用这种技术,并运用良好的判断力。如果有疑问,矩形实际上是一个非常好的形状的窗口。

I want to move my form window by left-clicking and holding on the background area, just like we normally do using the title bar.

And then I also want to change the form window of my application (i.e., a rectangular window) to my custom-designed graphical window. I have seen this type of window in many other applications, so I believe it is possible.

解决方案

Question 1:

To allow a form to be moved when dragging its client area, you need to tell the window manager to treat the client area as if it were the title bar (caption area). You suggest something similar in your question.

This can be done in .NET by overriding the WndProc method of your form, responding to the WM_NCHITTEST message, and returning HTCAPTION to indicate that everything should be treated as part of the caption (title) bar, instead of the default HTCLIENT, which indicates that it should be treated as the form's client area. Add the following code to your form class:

private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    if (m.Msg == WM_NCHITTEST)
    {
        // Convert HTCLIENT to HTCAPTION
        if (m.Result.ToInt32() == HTCLIENT)
        {
            m.Result = (IntPtr)HTCAPTION;
        }
    }
}

Question 2:

You can create a form of an arbitrary, non-rectangular shape by setting the Region property of your form to the custom Region of your choice. If you have experience with graphics programs like Photoshop, you can think of this as setting a "clipping region" for your form: the window manager will not draw anything outside of the bounds you specify. The pixels in the shape described this Region can even be non-contiguous.

The simplest way to create your region is probably to use the GraphicsPath class, and then use the constructor for the Region class that accepts a single GraphicsPath object as a parameter.

And as I assume you already know, given the first question, you'll have to set the FormBorderStyle property to None to make sure that the default borders drawn by the window manager disappear.

Unfortunately, these regions cannot be anti-aliased. See Hans's answer to this question for more details on these limitations.

Finally, it is worth noting that this latter approach to creating non-rectangular forms can produce some downright ugly user interfaces that don't at all improve the usability of your product, like so:

Please use this technique sparingly and exercise good judgment. When in doubt, rectangles are actually a really good shape for windows.

这篇关于创建可通过在背景区域拖动的任何位置移动的非矩形形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆