一:窗体圆角的处理
对于无边框窗体圆角矩形的处理,我这里采用的是使用API函数CreateRoundRectRgn,相比于自己用GDI+写的处理圆角的函数,效果要稍微好点,至少线条在圆角处过渡的比较平滑,为了便于复用,我把其封装到窗体自绘辅助类RenderHlper的SetFormRoundRectRgn函数中:
View Code
/// <summary>
/// 设置窗体的圆角矩形
/// </summary>
/// <param name="form">需要设置的窗体</param>
/// <param name="rgnRadius">圆角矩形的半径</param>
public static void SetFormRoundRectRgn(Form form, int rgnRadius)
{
int hRgn = 0;
hRgn = Win32CreateRoundRectRgn(0, 0, formWidth + 1, formHeight + 1, rgnRadius, rgnRadius);
Win32SetWindowRgn(formHandle, hRgn, true);
Win32DeleteObject(hRgn);
}
此处需要把所需要的API函数引用到类Win32中,引用的时候注意添加 SystemRuntimeInteropServices 命名空间:
View Code
[DllImport("gdi32dll")]
public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
[DllImport("user32dll")]
public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
[DllImport("gdi32dll", EntryPoint = "DeleteObject", CharSet = CharSetAnsi)]
public static extern int DeleteObject(int hObject);
重写窗体的OnSizeChanged事件,并在此事件中调用SetFormRoundRectRgn,此处的Radius参数为定义的窗体圆角半径属性:
View Code
protected override void OnSizeChanged(EventArgs e)
{
baseOnSizeChanged(e);
RenderHelperSetFormRoundRectRgn(this, Radius);
}
二:无边框窗体大小的改变与移动
当把窗体的FormBorderStyle属性调整为FormBorderStyleNone时,此时,窗体的大小改变不了,同时窗体不能移动。要想实现无边框窗体大小的改变与移动,可采用如下方法:
(1)重写窗体的过程WndProc:
主要是对WM_NCHITTEST消息进行处理,根据事件的发生位置来进行不同方向箭头的调整,窗体大小改变与移动的函数:
View Code
//调整窗体大小
private void WmNcHitTest(ref Message m)
{
int wparam = mLParamToInt32();
Point mouseLocation = new Point(RenderHelperLOWORD(wparam),RenderHelperHIWORD(wparam));
mouseLocation = PointToClient(mouseLocation);
if (WindowState != FormWindowStateMaximized )
{
if (CanResize == true)
{
if (mouseLocationX < 5 && mouseLocationY < 5)
{
mResult = new IntPtr(Win32HTTOPLEFT);
return;
}
if (mouseLocationX > Width - 5 && mouseLocationY < 5)
{
mResult = new IntPtr(Win32HTTOPRIGHT);
return;
}
if (mouseLocationX < 5 && mouseLocationY > Height - 5)
{
mResult = new IntPtr(Win32HTBOTTOMLEFT);
return;
}
if (mouseLocationX > Width - 5 && mouseLocationY > Height - 5)
{
mResult = new IntPtr(Win32HTBOTTOMRIGHT);
return;
}
if (mouseLocationY < 3)
{
mResult = new IntPtr(Win32HTTOP);
return;
}
if (mouseLocationY > Height - 3)
{
mResult = new IntPtr(Win32HTBOTTOM);
return;
}
if (mouseLocationX < 3)
{
mResult = new IntPtr(Win32HTLEFT);
return;
}
if (mouseLocationX > Width - 3)
{
mResult = new IntPtr(Win32HTRIGHT);
return;
}
}
}
mResult = new IntPtr(Win32HTCAPTION);
}
重写窗体过程:
View Code
protected override void WndProc(ref Message m)
{
switch (mMsg)
{
case Win32WM_NCHITTEST:
WmNcHitTest(ref m);
break;
default:
baseWndProc(ref m);
break;
}
}
(2)对于仅仅只想实现窗体的移动而不改变窗体的大小,可以重写OnMouseDown事件中发送HTCAPTION消息来实现无边框窗体的移动,具体的实现代码如下:
View Code
/// <summary>
/// 移动窗体
/// </summary>
public static void MoveWindow(Form form)
{
Win32ReleaseCapture();
Win32SendMessage(formHandle, Win32WM_NCLBUTTONDOWN, Win32HTCAPTION, 0);
}
调用窗体移动函数:
View Code
protected override void OnMouseDown(MouseEventArgs e)
{
baseOnMouseDown(e);
if (eButton == MouseButtonsLeft)
{
RenderMoveWindow(this);
}
}
三:窗体边框的绘制与边框阴影的实现
边框的绘制:边框的绘制使用用PS制作好的来进行贴图 *** 作,在贴图的过程中使用九宫图贴图方法,保证此边框能满足任何大小的窗体。
窗体边框的实现:此部分主要涉及到对CS_DropSHADOW的了解,只要在窗口的ClassStyle添加此样式即可,关键代码如下:
View Code
protected override CreateParams CreateParams
{
get
{
CreateParams cp = baseCreateParams;
if (!DesignMode)
{
cpClassStyle |= (int) ClassStyleCS_DropSHADOW;
}
return cp;
}
}
以上就是关于c#怎么使windows窗口美化全部的内容,包括:c#怎么使windows窗口美化、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)