原文:wpf无边框窗体移动和大小调整

 

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Interop;
  4. namespace Wpftest
  5. {
  6. public partial class MainWindow : Window
  7. {
  8. public MainWindow()
  9. {
  10. this.InitializeComponent();
  11. // 在此点下面插入创建对象所需的代码。
  12. }
  13. protected override void OnSourceInitialized(EventArgs e)
  14. {
  15. base.OnSourceInitialized(e);
  16. HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
  17. if (hwndSource != null)
  18. {
  19. hwndSource.AddHook(new HwndSourceHook(this.WndProc));
  20. }
  21. }
  22. private const int WM_NCHITTEST = 0x0084;
  23. private readonly int agWidth = 12; //拐角宽度
  24. private readonly int bThickness = 4; // 边框宽度
  25. private Point mousePoint = new Point(); //鼠标坐标
  26. public enum HitTest : int
  27. {
  28. HTERROR = -2,
  29. HTTRANSPARENT = -1,
  30. HTNOWHERE = 0,
  31. HTCLIENT = 1,
  32. HTCAPTION = 2,
  33. HTSYSMENU = 3,
  34. HTGROWBOX = 4,
  35. HTSIZE = HTGROWBOX,
  36. HTMENU = 5,
  37. HTHSCROLL = 6,
  38. HTVSCROLL = 7,
  39. HTMINBUTTON = 8,
  40. HTMAXBUTTON = 9,
  41. HTLEFT = 10,
  42. HTRIGHT = 11,
  43. HTTOP = 12,
  44. HTTOPLEFT = 13,
  45. HTTOPRIGHT = 14,
  46. HTBOTTOM = 15,
  47. HTBOTTOMLEFT = 16,
  48. HTBOTTOMRIGHT = 17,
  49. HTBORDER = 18,
  50. HTREDUCE = HTMINBUTTON,
  51. HTZOOM = HTMAXBUTTON,
  52. HTSIZEFIRST = HTLEFT,
  53. HTSIZELAST = HTBOTTOMRIGHT,
  54. HTOBJECT = 19,
  55. HTCLOSE = 20,
  56. HTHELP = 21,
  57. }
  58. protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  59. {
  60. switch (msg)
  61. {
  62. case WM_NCHITTEST:
  63. {
  64. this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);
  65. this.mousePoint.Y = (lParam.ToInt32() >> 16);
  66. //告诉系统你已经处理过该消息,不然设置为false
  67. handled = true;
  68. #region 测试鼠标位置
  69. // 窗口左上角
  70. if (this.mousePoint.Y - this.Top <= this.agWidth
  71. && this.mousePoint.X - this.Left <= this.agWidth)
  72. {
  73. return new IntPtr((int)HitTest.HTTOPLEFT);
  74. }
  75. // 窗口左下角  
  76. else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth
  77. && this.mousePoint.X - this.Left <= this.agWidth)
  78. {
  79. return new IntPtr((int)HitTest.HTBOTTOMLEFT);
  80. }
  81. // 窗口右上角
  82. else if (this.mousePoint.Y - this.Top <= this.agWidth
  83. && this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth)
  84. {
  85. return new IntPtr((int)HitTest.HTTOPRIGHT);
  86. }
  87. // 窗口右下角
  88. else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth
  89. && this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth)
  90. {
  91. return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
  92. }
  93. // 窗口左侧
  94. else if (this.mousePoint.X - this.Left <= this.bThickness)
  95. {
  96. return new IntPtr((int)HitTest.HTLEFT);
  97. }
  98. // 窗口右侧
  99. else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.bThickness)
  100. {
  101. return new IntPtr((int)HitTest.HTRIGHT);
  102. }
  103. // 窗口上方
  104. else if (this.mousePoint.Y - this.Top <= this.bThickness)
  105. {
  106. return new IntPtr((int)HitTest.HTTOP);
  107. }
  108. // 窗口下方
  109. else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.bThickness)
  110. {
  111. return new IntPtr((int)HitTest.HTBOTTOM);
  112. }
  113. else // 窗口移动(也可在窗体MouseLeftButtonDown事件中调用DragMove()方法),双击最大化,所有剩余鼠标操作映射到标题栏
  114. {
  115. return new IntPtr((int)HitTest.HTCAPTION);
  116. }
  117. #endregion
  118. }
  119. }
  120. return IntPtr.Zero;
  121. }
  122. }
  123. }

弊端 窗体 中控件 事件 失效

wpf无边框窗体移动和大小调整的更多相关文章

  1. WPFの无边框窗体以及控件的移动

    对于WPF,一旦隐藏了标题栏,就无法移动,这时候需要重写移动方法,下面列举常见的三种方式方式. 方式一:重写OnMouseLeftButtonDown protected override void ...

  2. C# 改变无边框窗体的尺寸大小

    以下代码为修改窗体尺寸的代码: const int HTLEFT = 10; ; ; ; ; ; const int HTBOTTOMLEFT = 0x10; ; protected override ...

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

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

  4. 【WPF】无边框窗体

    之前写了一个支持尺寸变换的无边框窗体的一个基窗体,代码如下: public class LBaseWindow : Window { /// <summary> /// 基窗体 /// & ...

  5. 【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体

    WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式.但是AllowTransparency ...

  6. Qt 无边框窗体改变大小 完美实现(全部自己实现)

    近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...

  7. 利用WPF创建含多种交互特性的无边框窗体

    咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...

  8. 01.WPF中制作无边框窗体

    [引用:]http://blog.csdn.net/johnsuna/article/details/1893319   众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...

  9. Qt 无边框窗体改变大小 完美实现

    近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...

随机推荐

  1. 计算机图形学(二)输出图元_3_画线算法_2_DDA算法

    DDA算法        数字微分分析仪(digital differential analyzer, DDA)方法是一种线段扫描转换算法.基于使用等式(3.4)或等式(3.5)计算的&x或& ...

  2. P2P网络借贷系统简要解读

    P2P网络借贷系统的核心功能其实很简单,就是若干投资者(借款人)集体把钱贷给贷款人,到期后,贷款人把利息和本金按时还给借款人.平台的主要作用是"撮合",收取一定的服务费和管理费. ...

  3. Win10安装后必做的优化,解决磁盘100%占用

    Win10安装后必做的优化,解决磁盘100%占用 01关闭家庭组 控制面板–管理工具–服务– HomeGroup Listener和HomeGroup Provider禁用. 02关闭磁盘碎片整理.自 ...

  4. [TypeScript] Understand lookup types in TypeScript

    Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...

  5. Android 获取签名证书的具体信息(Eclipse和Android studio通用)

    今天要用到签名证书的MD5,可是这个仅仅有在第一次生成的时候我看到了,这可怎么办呢,幸亏我们有google,我们执行以下的命令就OK了. keytool -list -v -keystore 签名证书 ...

  6. 将Sublime Text2 加入右键菜单

    在googleread里面看有人推荐sublime text2.说开发很方便.就下载一个试试.写html还真的挺爽. 于是按照vim加入鼠标右键的方法.果然可以.这里和大家分享 1. 运行中输入 re ...

  7. Javascript 获取页面高度(多种浏览器)

    //2015年8月13日11:00:50 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: d ...

  8. [SCSS] Use Standard Built-in SCSS Functions for Common Operations

    We can use javascript for color and opacity variations, math, list and map logic or to see if someth ...

  9. GCD网络多线程---同步运行,异步运行,串行队列,并行队列

    总结:同步(无论是串行还是并行)----不又一次开辟子线程 异步(无论是串行还是并行)----开辟子线程 GCD: dispatch queue 主线程的main queue 并行队列 global ...

  10. source insight totalcmd 中文目录

    @echo off for /F "usebackq delims=" %%a in (`echo %1^^^|iconv -f utf-8 -t gb18030`) do ( s ...