C#Winform窗口特效源码(1)
本文基于.Net开发,使用C#作为开发语言,分别包含以下效果: 移动无边框窗口、窗口移动限制(限制在屏幕内)、桌面贴边自动隐藏(仿QQ隐藏窗口)
1、移动无边框窗口 采用了消息的方式,可以实现通过窗口内的任何控件来移动窗口
- private const int WM_NCLBUTTONDOWN = 0x00A1;
- private const int HT_CAPTION = 0x002;
- private void Form_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- ((Control)sender).Capture = false;
- Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
- base.WndProc(ref msg);
- }
- }
使用方法: 将以上代码复制到Form的class内,将Form或Form中需要控制Form移动的控件的MouseDown事件关联到这个方法上,如: 对于Form:this.MouseDown+=new System.Windows.Forms.MouseEventHandler(Form_MouseDown); 对于其它控件:Control.MouseDown+=new System.Windows.Forms.MouseEventHandler(Form_MouseDown);
2、窗口移动限制
- private const int WM_MOVING = 0x0216;
- protected override void WndProc(ref Message m)
- {
- switch (m.Msg)
- {
- case WM_MOVING: // 窗体移动的消息,控制窗体不会移出屏幕外
- int left = Marshal.ReadInt32(m.LParam, 0);
- int top = Marshal.ReadInt32(m.LParam, 4);
- int right = Marshal.ReadInt32(m.LParam, 8);
- int bottom = Marshal.ReadInt32(m.LParam, 12);
- left = Math.Min(Math.Max(0, left), Screen.PrimaryScreen.Bounds.Width - Width);
- top = Math.Min(Math.Max(0, top), Screen.PrimaryScreen.Bounds.Height - Height);
- right = Math.Min(Math.Max(Width, right), Screen.PrimaryScreen.Bounds.Width);
- bottom = Math.Min(Math.Max(Height, bottom), Screen.PrimaryScreen.Bounds.Height);
- Marshal.WriteInt32(m.LParam, 0, left);
- Marshal.WriteInt32(m.LParam, 4, top);
- Marshal.WriteInt32(m.LParam, 8, right);
- Marshal.WriteInt32(m.LParam, 12, bottom);
- break;
- }
- base.WndProc(ref m);
- }
使用方法: 添加using System.Runtime.InteropServices; 在Form类定义中加入以上代码即可 3、桌面贴边自动隐藏
- public class FormAutoDock
- {
- public static void SideHideOrShow(Form DockableForm, ref int DockFormHeight, Timer _dockTimer)
- {
- if (DockableForm.WindowState != FormWindowState.Minimized)
- {
- _dockTimer.Interval = 1500;
- if (Cursor.Position.X > DockableForm.Left - 1 && Cursor.Position.X < DockableForm.Right && Cursor.Position.Y > DockableForm.Top - 1 && Cursor.Position.Y < DockableForm.Bottom)
- {
- if (DockableForm.Top <= 0 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
- {
- DockableForm.Top = 0;
- }
- else if (DockableForm.Left <= 0)
- {
- DockableForm.Left = 0;
- }
- else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
- {
- DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width;
- }
- else
- {
- if (DockFormHeight > 0)
- {
- DockableForm.Height = DockFormHeight;
- DockFormHeight = 0;
- }
- }
- }
- else
- {
- if (DockFormHeight < 1)
- {
- DockFormHeight = DockableForm.Height;
- }
- if (DockableForm.Top <= 4 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
- {
- DockableForm.Top = 3 - DockableForm.Height;
- if (DockableForm.Left <= 4)
- {
- DockableForm.Left = -5;
- }
- else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
- {
- DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width + 5;
- }
- }
- else if (DockableForm.Left <= 4)
- {
- DockableForm.Left = 3 - DockableForm.Width;
- }
- else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
- {
- DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - 3;
- }
- _dockTimer.Interval = 200;
- }
- }
- }
- }
使用方法: 将FormAutoDock.cs文件复制到与需要处理的Form相同目录下,并添加到项目中 在Form的类中增加以下内容: private Timer _dockTimer = null; private int DockFormHeight = 0; private void DockTimerAutoHide_Tick(object sender, EventArgs e) { FormAutoDock.SideHideOrShow(this, ref DockFormHeight, _dockTimer); Application.DoEvents(); }
在Form类构造方法中添加以下内容: _dockTimer = new Timer(); _dockTimer.Enabled = false; _dockTimer.Interval = 200; _dockTimer.Tick += new EventHandler(DockTimerAutoHide_Tick); _dockTimer.Start();
C#Winform窗口特效源码(1)的更多相关文章
- 多种的android进度条的特效源码
		多种的android进度条的特效源码,这个源码是在源码天堂那个网站上转载过来的,我已经修改一部分了,感觉很实用的,大家可以学习一下吧,我就不上传源码了,大家可以直接到那个网站上下载吧. 源码天堂下载地 ... 
- JavaScript特效源码(1、文字特效)
		注:本文以及以下关于Javascript特效源码都是分享自JavaScript源码大全. 1.逐隐逐现的的特效 逐隐逐现的文字特效[推荐使用][适用于IE4++] (修改显示的文字后根据说明进行共2步 ... 
- C#项目 学生选课系统 C#窗口 Winform项目 项目源码及使用说明
		这是一个学生选课信息管理系统,使用VS2010+SQL2008编写,VS2017正常使用. 项目源码下载地址 https://gitee.com/whuanle/xkgl 笔者录了两个视频,打开项目源 ... 
- 3D轮播切换特效 源码
		这个3D轮播切换特效是我2017年2月份写的 当初我 刚接触HTML不久,现在把源码分享给大家 源码的注释超级清楚 . <!-- 声明文档类型:html 作用:符合w3c统一标准规范 每个浏览器 ... 
- Android 类似360悬浮窗口实现源码
		当我们在手机上安装360安全卫士时,手机屏幕上时刻都会出现一个小浮动窗口,点击该浮动窗口可跳转到安全卫士的操作界面,而且该浮动窗口不受其他activity的覆盖影响仍然可见(多米音乐也有相关的和主界面 ... 
- android Activity实现底部滑动弹出窗口及源码下载地址
		在做微信.微博.qq等分享时,一般是点击分享按钮后会从底部弹出滑动窗口,然后选择要分享的社交平台进行分享.今日头条.腾讯新闻等内容App的评论也是从底部滑动弹出输入窗口,进行评论输入的.本篇文章就讲讲 ... 
- JavaScript特效源码(4、鼠标特效)
		1.鼠标感应--渐现特效 鼠标感应渐显图片[平时很模糊的图片鼠标一放上就显示清楚] [修改图片名称即可][共2步] ====1.将以下代码加入HTML的<head></head> ... 
- Jquery仿IGoogle实现可拖动窗口(源码)
		google可谓是ajax的特效用的淋漓尽致,google suggest, google map,igoogle 可拖动窗口等等...今天仿照iGoogle做了一个简单的小demo. 这个的demo ... 
- JavaScript特效源码(7、页面特效二)
		7.将站点加入频道栏 将站点加入频道栏[看详细说明] ====1.加入channel的方法:使用如下连接指向你的频道文件*.cdf. <a href="javascript:windo ... 
随机推荐
- linux下fflush(stdin)的使用问题
			参考自linux下如何清空(刷新)stdin缓冲区 首先,fflush在C/C++/POSIX标准中只定义了处理输出流的行为,对于像stdin这种输入流,这是未定义行为undefined behavi ... 
- HDU 1253 胜利大逃亡(三维BFS)
			点我看题目 题意 : 中文题不详述. 思路 :因为还牵扯到层的问题,所以用三维的解决,不过这个还是很简单的BFS,六个方向搜一下就可以了,一开始交的时候老是超时,怎么改都不对,后来看了一个人写的博客, ... 
- 一些简单的问题.  2的10次方与k   (涉及到b k m的要用乘来解读)
			2的10次方是k k就表示2的10次方 2的16次方,解读为 2的6次方(64)*2的10次方(k) 简写为64k 64k=64*k 同理2的20次方 解读为2的10次方*2的10次方 k ... 
- CDQ分治题目小结
			CDQ分治属于比较特殊的一类分治,许多问题转化为这类分治的时候,时空方面都会有很大节省,而且写起来没有这么麻烦. 这类分治的特殊性在于分治的左右两部分的合并,作用两部分在合并的时候作用是不同的,比如, ... 
- *[codility]Number-of-disc-intersections
			http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件, ... 
- 基于dojo模板的widget
			参考:http://niweiwei.iteye.com/blog/1539863 http://dojotoolkit.org/reference-guide/1.8/dijit/_Template ... 
- C++构造函数,复制构造函数和析构函数专题
			链接:http://wenku.baidu.com/view/d9316c0e52ea551810a6872a.html 本文作者:黄邦勇帅本文是学习 C++中的最基本的内容,因此学习 C++就应全部 ... 
- tlplayer for ios V1.1.2加密测试版本(修复1.1.1版本 for ios7播放闪退问题)
			此版本主要修复了ios7播放列表导致的程序闪退问题,方便大家测试加密与非加密视频. 此为tlplayer for ios版本,可以播放加密视频与非加密视频. 加密视频下载地址:http://blog. ... 
- MySQL 授权详解
			(1)确认一下3306是否对外开放,mysql默认状态下是不开放对外访问功能的.查看的办法如下: 1 2 3 4 5 6 7 netstat -an | grep 3306 tcp 0 ... 
- Linux kernel ‘qeth_snmp_command’函数缓冲区溢出漏洞
			漏洞名称: Linux kernel ‘qeth_snmp_command’函数缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-423 发布时间: 2013-11-29 更新时间: 201 ... 
