如何让窗口看起来像一个框架?

时间:2013-01-12 01:52:58

标签: c# .net windows trayicon window-style

它应该看起来像单击托盘中的“操作中心”,“电源”,“网络”或“声音”图标时出现的小框。它需要没有标题栏的玻璃边框。

enter image description here

它还需要是固定大小而不是可调整大小。感谢任何能提供帮助的人! :)

1 个答案:

答案 0 :(得分:3)

您需要将ControlBox设置为false,清除标题文本并设置边框样式。由于您声明需要相当大的边框,但不允许调整大小,因此您也可以设置最小和最大尺寸。最后,为了防止鼠标光标显示调整大小光标,如果它们位于其中一个边框上,我们会覆盖WM_NCHITTEST结果:

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;
        }
    }
}