C#自定义Winform无边框窗体

  在实际项目中,WinForm窗体或者控件不能满足要求,所以就需要自己设计窗体等,当然设计界面可以用的东西很多,例如WPF、或者一些第三方的库等。本例中将采用WinForm设计一个扁平美观的窗体。

上一篇中我们制作了一个button按钮控件,刚好本例可采用

需要的可以参考:C#自定义Button按钮控件

窗体效果:

接下来就是窗体的设计

1.添加一个窗体继承原来的窗体Form

 public partial class FormEX : Form

2.添加窗体属性

      /// <summary>
/// 是否允许最大化
/// </summary>
private bool maxVisible = true;
[Description("是否允许最大化")]
public bool MaxVisible
{
get { return maxVisible; }
set
{
maxVisible = value;
if (!maxVisible)
{
this.btnEXMin.Location = new System.Drawing.Point(this.btnEXMax.Location.X, );
this.btnEXMax.Visible = false;
}
else
{
this.btnEXMin.Location = new System.Drawing.Point(this.btnEXMax.Location.X - , );
this.btnEXMax.Visible = true;
}
}
} /// <summary>
/// 窗体标题
/// </summary>
private string titleText;
[Description("窗体标题")]
public string TitleText
{
get { return titleText; }
set
{
titleText = value;
title.Text = titleText; }
}
/// <summary>
/// 窗体标题是否显示
/// </summary>
private bool titleVisible = true;
[Description("窗体标题是否显示")]
public bool TitleVisible
{
get { return titleVisible; }
set
{
titleVisible = value;
title.Visible = titleVisible;
}
} /// <summary>
/// 窗口默认大小
/// FormSize.NORMAL OR FormSize.MAX
/// </summary>
private FormSize defaultFormSize = FormSize.NORMAL;
[Description("窗口默认大小")]
public FormSize DefaultFormSize
{
get { return defaultFormSize; }
set
{
defaultFormSize = value;
if (defaultFormSize == FormSize.MAX)
{
//防止遮挡任务栏
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
//最大化图标切换
this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.MaxNormal;
}
}
}

3.窗体大小自由更改

         const int WM_NCHITTEST = 0x0084;
const int HTLEFT = ; //左边界
const int HTRIGHT = ; //右边界
const int HTTOP = ; //上边界
const int HTTOPLEFT = ; //左上角
const int HTTOPRIGHT = ; //右上角
const int HTBOTTOM = ; //下边界
const int HTBOTTOMLEFT = 0x10; //左下角
const int HTBOTTOMRIGHT = ; //右下角
  protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= )
if (vPoint.Y <= )
m.Result = (IntPtr)HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)HTBOTTOMLEFT;
else m.Result = (IntPtr)HTLEFT;
else if (vPoint.X >= ClientSize.Width - )
if (vPoint.Y <= )
m.Result = (IntPtr)HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)HTBOTTOMRIGHT;
else m.Result = (IntPtr)HTRIGHT;
else if (vPoint.Y <= )
m.Result = (IntPtr)HTTOP;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)HTBOTTOM;
break;
default:
base.WndProc(ref m);
break;
}
}

4.窗体按钮等事件添加

  /// <summary>
/// 最小化按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEXMin_ButtonClick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
} /// <summary>
/// 最大化按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEXMax_ButtonClick(object sender, EventArgs e)
{
this.MaxNormalSwitch();
} /// <summary>
/// 关闭按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEXClose_ButtonClick(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 鼠标按下标题栏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void titleBar_MouseDown(object sender, MouseEventArgs e)
{
mPoint = new Point(e.X, e.Y);
} /// <summary>
/// 鼠标在移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void titleBar_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
}
} private void titleBar_DoubleClick(object sender, EventArgs e)
{
this.MaxNormalSwitch();
} /// <summary>
/// 最大化和正常状态切换
/// </summary>
private void MaxNormalSwitch()
{
if (this.WindowState == FormWindowState.Maximized)//如果当前状态是最大化状态 则窗体需要恢复默认大小
{
this.WindowState = FormWindowState.Normal;
//
this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.Max;
}
else
{
//防止遮挡任务栏
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
//最大化图标切换
this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.MaxNormal;
}
this.Invalidate();//使重绘
} private void FormEX_Resize(object sender, EventArgs e)
{
this.Invalidate();//使重绘
}

窗体效果展示

工程源程序下载

C#自定义Winform无边框窗体的更多相关文章

  1. C#自定义按钮、自定义WinForm无边框窗体、自定义MessageBox窗体

    C#自定义按钮.自定义WinForm无边框窗体.自定义MessageBox窗体 C#自定义Button按钮控件 效果展示 C#自定义Winform无边框窗体 效果展示 C#自定义无边框MessageB ...

  2. C#WinForm无边框窗体移动方法、模仿鼠标单击标题栏移动窗体位置

    C#WinForm无边框窗体移动方法.模仿鼠标单击标题栏移动窗体位置 这里介绍俩种办法 方法一:直接通过修改窗体位置从而达到移动窗体的效果 方法二:直接伪装发送单击任务栏消息,让应用程序误以为单击任务 ...

  3. WinForm无边框窗体移动方法

    C#WinForm无边框窗体移动方法.模仿鼠标单击标题栏移动窗体位置 这里介绍俩种办法 方法一:直接通过修改窗体位置从而达到移动窗体的效果 方法二:直接伪装发送单击任务栏消息,让应用程序误以为单击任务 ...

  4. C#WinForm无边框窗体移动----模仿鼠标单击标题栏移动窗体位置

    C#WinForm无边框窗体移动方法.模仿鼠标单击标题栏移动窗体位置 这里介绍俩种办法 方法一:直接通过修改窗体位置从而达到移动窗体的效果 方法二:直接伪装发送单击任务栏消息,让应用程序误以为单击任务 ...

  5. WinForm 无边框窗体和timer控件

    一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入,移出,按下三个事件会让按钮改变样式 如何获取图 ...

  6. WinForm 无边框窗体改变尺寸及移动窗体

    #region 无边框窗体移动改变大小 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); ...

  7. Winform无边框窗体(FormBorderStyle属性设为None)自定义移动

    为了界面的好看,有时候需要将窗体FormBorderStyle属性设为None,这样就可以根据自己的喜欢来设计界面.但这样窗体无法进行移动的.而且默认的窗体(FormBorderStyle=Sizab ...

  8. WinForm - 无边框窗体自定义移动

    为了界面的好看,有时候需要将窗体FormBorderStyle属性设为None,这样就可以根据自己的喜欢来设计界面.但这样窗体无法进行移动的.而且默认的窗体(FormBorderStyle=Sizab ...

  9. Winform无边框窗体的移动和阴影

    //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport ...

随机推荐

  1. WebMagic编译时提示Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.18的解决方法

    问题描述:    从http://git.oschina.net/flashsword20/webmagic 下载最新代码,按照http://webmagic.io/docs/zh/posts/ch3 ...

  2. 终极解决方案:org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response

    一.项目 我的项目采用Spring MVC +JSP+EasyUI 做的老项目. 在做图片验证码方法时,向网页输出验证码图片的方法如下: @Override public void showCodeI ...

  3. js,html-点击直接跳转到页面底/顶部

    案例一:js控制,无滑动效果 <html> <body> <a href="javascript:void(0);" onclick="ja ...

  4. Android Studio 3.1 正式版

    欢迎大家推荐自己在Android开发过程中用的好用的工具.学习开发教程.用到设计素材.如果你觉得本站对你有用,你可以点击底部的分享按钮,把本站分享到社交网络让你的小伙伴和更多的人知道. 或者可以考虑捐 ...

  5. [Spring Unit Testing] Spring Unit Testing with a Java Context

    For example, we want to test against a implemataion: package com.example.in28minutes.basic; import o ...

  6. ORA-00959: tablespace 'TB01' does not exist

    当前的表空间如下: SQL> select name from v$tablespace; NAME ---------------------------------------------- ...

  7. phpBB3.2 自动检测浏览器语言

    这是根据HTTP request header里的Accept-Language信息来处理的. 首先看一下Accept-Language的格式 Accept-Language: <languag ...

  8. Newtonsoft.Json2.0下面序列化和反序列化

    序列化 string xml = JavaScriptConvert.SerializeObject(dataTable); 反序列化 JavaScriptConvert.DeserializeObj ...

  9. OpenCV 学习笔记 05 人脸检测和识别 AttributeError: module 'cv2' has no attribute 'face'

    1 环境设置: win10 python 3.6.8 opencv 4.0.1 2 尝试的方法 在学习人脸识别中,遇到了没有 cv2 中没有 face 属性.在网上找了几个方法,均没有成功解决掉该问题 ...

  10. 12C配置EM Express的https端口

    1.启动监听并查看监听信息 $ lsnrctl stat ora12 LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 07-FEB-2017 ...