问题
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.
回答1:
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);
}
}
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!
回答2:
I suppose you wants to make custom winform. In this case, you may wanna not render the default windows. Draw your desired winform in photoshop and use it as a background in your application. The issue with this approach is that you will need to design your own minimize, maximize and close buttons.
You may use the FormBorderStyle to make it disappear.
回答3:
It's REALLY involved, but if you want you can create your own Windows Forms library, that is don't use the Windows Form project at all. Use the Windows GDI Documentation and PInvoke.Net to call into the GDI API functions for creating windows etc and design your own form system.
The specific section you want to look at is Window Functions:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632679(v=vs.85).aspx
来源:https://stackoverflow.com/questions/35097308/how-to-make-a-frame-for-winform-c