本文基于.Net开发,使用C#作为开发语言,分别包含以下效果: 移动无边框窗口、窗口移动限制(限制在屏幕内)、桌面贴边自动隐藏(仿QQ隐藏窗口)

1、移动无边框窗口 采用了消息的方式,可以实现通过窗口内的任何控件来移动窗口

  1. private const int WM_NCLBUTTONDOWN = 0x00A1;
  2. private const int HT_CAPTION = 0x002;
  3. private void Form_MouseDown(object sender, MouseEventArgs e)
  4. {
  5. if (e.Button == MouseButtons.Left)
  6. {
  7. ((Control)sender).Capture = false;
  8. Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
  9. base.WndProc(ref msg);
  10. }
  11. }

使用方法: 将以上代码复制到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、窗口移动限制

  1. private const int WM_MOVING = 0x0216;
  2. protected override void WndProc(ref Message m)
  3. {
  4. switch (m.Msg)
  5. {
  6. case WM_MOVING: // 窗体移动的消息,控制窗体不会移出屏幕外
  7. int left = Marshal.ReadInt32(m.LParam, 0);
  8. int top = Marshal.ReadInt32(m.LParam, 4);
  9. int right = Marshal.ReadInt32(m.LParam, 8);
  10. int bottom = Marshal.ReadInt32(m.LParam, 12);
  11. left = Math.Min(Math.Max(0, left), Screen.PrimaryScreen.Bounds.Width - Width);
  12. top = Math.Min(Math.Max(0, top), Screen.PrimaryScreen.Bounds.Height - Height);
  13. right = Math.Min(Math.Max(Width, right), Screen.PrimaryScreen.Bounds.Width);
  14. bottom = Math.Min(Math.Max(Height, bottom), Screen.PrimaryScreen.Bounds.Height);
  15. Marshal.WriteInt32(m.LParam, 0, left);
  16. Marshal.WriteInt32(m.LParam, 4, top);
  17. Marshal.WriteInt32(m.LParam, 8, right);
  18. Marshal.WriteInt32(m.LParam, 12, bottom);
  19. break;
  20. }
  21. base.WndProc(ref m);
  22. }

使用方法: 添加using System.Runtime.InteropServices; 在Form类定义中加入以上代码即可 3、桌面贴边自动隐藏

  1. public class FormAutoDock
  2. {
  3. public static void SideHideOrShow(Form DockableForm, ref int DockFormHeight, Timer _dockTimer)
  4. {
  5. if (DockableForm.WindowState != FormWindowState.Minimized)
  6. {
  7. _dockTimer.Interval = 1500;
  8. if (Cursor.Position.X > DockableForm.Left - 1 && Cursor.Position.X < DockableForm.Right && Cursor.Position.Y > DockableForm.Top - 1 && Cursor.Position.Y < DockableForm.Bottom)
  9. {
  10. if (DockableForm.Top <= 0 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
  11. {
  12. DockableForm.Top = 0;
  13. }
  14. else if (DockableForm.Left <= 0)
  15. {
  16. DockableForm.Left = 0;
  17. }
  18. else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
  19. {
  20. DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width;
  21. }
  22. else
  23. {
  24. if (DockFormHeight > 0)
  25. {
  26. DockableForm.Height = DockFormHeight;
  27. DockFormHeight = 0;
  28. }
  29. }
  30. }
  31. else
  32. {
  33. if (DockFormHeight < 1)
  34. {
  35. DockFormHeight = DockableForm.Height;
  36. }
  37. if (DockableForm.Top <= 4 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
  38. {
  39. DockableForm.Top = 3 - DockableForm.Height;
  40. if (DockableForm.Left <= 4)
  41. {
  42. DockableForm.Left = -5;
  43. }
  44. else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
  45. {
  46. DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width + 5;
  47. }
  48. }
  49. else if (DockableForm.Left <= 4)
  50. {
  51. DockableForm.Left = 3 - DockableForm.Width;
  52. }
  53. else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
  54. {
  55. DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - 3;
  56. }
  57. _dockTimer.Interval = 200;
  58. }
  59. }
  60. }
  61. }

使用方法: 将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)的更多相关文章

  1. 多种的android进度条的特效源码

    多种的android进度条的特效源码,这个源码是在源码天堂那个网站上转载过来的,我已经修改一部分了,感觉很实用的,大家可以学习一下吧,我就不上传源码了,大家可以直接到那个网站上下载吧. 源码天堂下载地 ...

  2. JavaScript特效源码(1、文字特效)

    注:本文以及以下关于Javascript特效源码都是分享自JavaScript源码大全. 1.逐隐逐现的的特效 逐隐逐现的文字特效[推荐使用][适用于IE4++] (修改显示的文字后根据说明进行共2步 ...

  3. C#项目 学生选课系统 C#窗口 Winform项目 项目源码及使用说明

    这是一个学生选课信息管理系统,使用VS2010+SQL2008编写,VS2017正常使用. 项目源码下载地址 https://gitee.com/whuanle/xkgl 笔者录了两个视频,打开项目源 ...

  4. 3D轮播切换特效 源码

    这个3D轮播切换特效是我2017年2月份写的 当初我 刚接触HTML不久,现在把源码分享给大家 源码的注释超级清楚 . <!-- 声明文档类型:html 作用:符合w3c统一标准规范 每个浏览器 ...

  5. Android 类似360悬浮窗口实现源码

    当我们在手机上安装360安全卫士时,手机屏幕上时刻都会出现一个小浮动窗口,点击该浮动窗口可跳转到安全卫士的操作界面,而且该浮动窗口不受其他activity的覆盖影响仍然可见(多米音乐也有相关的和主界面 ...

  6. android Activity实现底部滑动弹出窗口及源码下载地址

    在做微信.微博.qq等分享时,一般是点击分享按钮后会从底部弹出滑动窗口,然后选择要分享的社交平台进行分享.今日头条.腾讯新闻等内容App的评论也是从底部滑动弹出输入窗口,进行评论输入的.本篇文章就讲讲 ...

  7. JavaScript特效源码(4、鼠标特效)

    1.鼠标感应--渐现特效 鼠标感应渐显图片[平时很模糊的图片鼠标一放上就显示清楚] [修改图片名称即可][共2步] ====1.将以下代码加入HTML的<head></head> ...

  8. Jquery仿IGoogle实现可拖动窗口(源码)

    google可谓是ajax的特效用的淋漓尽致,google suggest, google map,igoogle 可拖动窗口等等...今天仿照iGoogle做了一个简单的小demo. 这个的demo ...

  9. JavaScript特效源码(7、页面特效二)

    7.将站点加入频道栏 将站点加入频道栏[看详细说明] ====1.加入channel的方法:使用如下连接指向你的频道文件*.cdf. <a href="javascript:windo ...

随机推荐

  1. oracle学习之-----操作表中的数据

    1. 向表中添加数据(Insert 语句): 添加的语法: INSERT INTO table_name(column1,column2,column3,......) VALUES(value1,v ...

  2. 1920-Jangbi的Rush

    描述 最后一届的OSL决赛由神族的Jangbi对阵人族Fantasy.Jangbi5BG爆叉叉准备一波rush,但是范特西早有防备,在地图上埋下了许多地雷.但是Jangbi显然不是毕姥爷那样的无脑平A ...

  3. UVA 658 It's not a Bug, it's a Feature!

    这个题目巧妙之处在于用二进制的每个位1,0分别表示bug的有无,以及实施补丁对相应bug的要求以及实施后的对bug的影响. 软件bug的状态:1表示相应bug仍然存在,0表示已经修复.这样可以将软件的 ...

  4. ANDROID_MARS学习笔记_S01原始版_017_绑定SERVICE

    一.流程 1.编写service,重写onBind(Intent intent),返回自定义的Binder 2.自写义Binder,提供一个可访问的方法,以传递数据 3.点击界面按钮会开启servic ...

  5. 《ArcGIS Engine+C#实例开发教程》第五讲 鹰眼的实现

    原文:<ArcGIS Engine+C#实例开发教程>第五讲 鹰眼的实现 摘要:所谓的鹰眼,就是一个缩略地图,上面有一个矩形框,矩形框区域就是当前显示的地图区域,拖动矩形框可以改变当前地图 ...

  6. 点点滴滴-NET下的常用框架

    刘冬的博客:http://www.cnblogs.com/GoodHelper/category/214139.html (Spring.net和Nhibernate) Kyo-yo  : http: ...

  7. [Quick-x]移动CCEditbox的父对象导致输入框位置偏移问题

    CCEditbox对象添加到某个layer,当layer移动时候,editbox输入状态下输入光标保持在原位,看起来就是光标发生了偏移 如果开始时添加的editbox不在屏幕内的话,光标会出现在屏幕边 ...

  8. 基于SXSSF (Streaming Usermodel API)的写文件

    在POI3.8中SXSSF仅仅支持excel2007格式是对XSSF的一种流的扩展.目的在生成excel时候,需要生成大量的数据的时候,通过刷新的方式将excel内存信息刷新到硬盘的方式,提供写入数据 ...

  9. WPF——执行命令清空文本框

    一.造一个窗体,在窗体里面先造一个StackPanel,然后再StackPanel里面放好按钮和文本框,注意给所有的控件和容器起名字 <Grid> <StackPanel Name= ...

  10. [转] 弱校ACM奋斗史

    转载来自:http://blog.163.com/lx_zz0o0/blog/static/236205116201442604234538/ 弱校ACM奋斗史  2014-05-26 00:42:3 ...