如何为WinForm,C#制作框架? [英] How to make a frame for WinForm, C#?
问题描述
我一直在研究更改Windows窗体的边框颜色,发现这是由Windows决定的,这是有道理的,所以然后我看到有人问过这个问题的人被告知去这里<一个href ="http://customerborderform.codeplex.com/" rel ="nofollow noreferrer"> http://customerborderform.codeplex.com/该网站目前无法使用.那我必须自己制作框架吗?如果是这样,我该去哪里弄清楚呢?我正在使用Visual Studio 2012.
I've been looking into changing the color of the border for the windows form and found out it's decided by windows, ok that makes sense, so then I see that people who have asked this question before are told to go here http://customerborderform.codeplex.com/ Looks like the site isn't usable at the moment. So would I have to make my own frame? If so where would I go to figure this, out? I'm using visual studio 2012.
推荐答案
下面是绘制自己的边框,可以调整大小和移动的窗体的示例..
Here is an example of a form that draws its own border, can be resized and moved..:
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 文档向您展示了如何模拟点击控件和调整大小框(如果需要).当然,您也应该以某种方式绘画它们!
The WM_NCHITTEST docs show you how to simulate hitting the control and resize boxes if you need those. You should paint them somehow as well, of course!
这篇关于如何为WinForm,C#制作框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!