我一直在探索如何更改 Windows 窗体的边框颜色,并发现这是由 Windows 决定的。好吧,这很合理。然后我看到之前有人问过同样的问题,被告知要去这里http://customerborderform.codeplex.com/。看起来这个网站目前无法使用。那么我是否需要自己制作边框?如果是这样,我该去哪里找相关信息呢?我正在使用 Visual Studio 2012。
我一直在探索如何更改 Windows 窗体的边框颜色,并发现这是由 Windows 决定的。好吧,这很合理。然后我看到之前有人问过同样的问题,被告知要去这里http://customerborderform.codeplex.com/。看起来这个网站目前无法使用。那么我是否需要自己制作边框?如果是这样,我该去哪里找相关信息呢?我正在使用 Visual Studio 2012。
public partial class BorderForm : Form
{
public BorderForm()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
BorderColor = Color.DarkSlateGray;
}
private const int hWidth = 12; // resize handle width
private const int bWidth = 28; // border width
public Color BorderColor { get; set; }
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
protected override void OnPaint(PaintEventArgs e)
{
// draw the border..
using (Pen pen = new Pen(BorderColor, bWidth)
{ Alignment = System.Drawing.Drawing2D.PenAlignment.Inset})
e.Graphics.DrawRectangle(pen, ClientRectangle);
// now maybe draw a title text..
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84) // Trap WM_NCHITTEST
{
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
pos = PointToClient(pos);
bool isTop = pos.Y <= hWidth;
bool isBottom = pos.Y >= ClientSize.Height - hWidth;
bool isRight = pos.X >= ClientSize.Width - hWidth;
bool isLeft = pos.X <= hWidth;
m.Result = (IntPtr)1;
if (isTop) m.Result =
isLeft ? (IntPtr)13 : isRight ? (IntPtr)14 : (IntPtr)12;
else if (isBottom) m.Result =
isLeft ? (IntPtr)16 : isRight ? (IntPtr)17 : (IntPtr)15;
else if (isLeft) m.Result = (IntPtr)10;
else if (isRight) m.Result = (IntPtr)11;
if ( m.Result != (IntPtr)1) return;
}
base.WndProc(ref m);
}
}
WM_NCHITTEST文档向您展示如何模拟控件和调整大小框的点击,如果需要的话。当然,您还应该以某种方式绘制它们!
我猜你想制作自定义的winform。在这种情况下,你可能不想渲染默认的窗口。在Photoshop中绘制所需的winform,并将其用作应用程序的背景。这种方法的问题是你需要设计自己的最小化、最大化和关闭按钮。
你可以使用FormBorderStyle来使它消失。
这个问题比较复杂,但如果你想的话,可以创建自己的Windows Forms库,而不使用Windows Form项目。使用Windows GDI文档和PInvoke.Net调用GDI API函数来创建窗口等,并设计自己的表单系统。
你需要查看的具体部分是Window Functions:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632679(v=vs.85).aspx