Csharp/C#教程:仅垂直移动表单分享


仅垂直移动表单

如何创建一个只能垂直移动TitleBar的WinForms表单?

您必须拦截Windows发送的WM_MOVING通知消息。 这是代码:

using System.Runtime.InteropServices; ... public partial class Form1 : Form { public Form1() { InitializeComponent(); } private struct RECT { public int left, top, right, bottom; } protected override void WndProc(ref Message m) { if (m.Msg == 0x216) { // Trap WM_MOVING var rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); int w = rc.right - rc.left; rc.left = this.Left; rc.right = rc.left + w; Marshal.StructureToPtr(rc, m.LParam, false); } base.WndProc(ref m); } } 

这样做(但它不漂亮):

  private void MainForm_Move(object sender, EventArgs e) { this.Left = 100; } 

您可以通过将表单的位置重置为初始X值和移动的Y值来快捷移动操作。 这个解决方案很简单,但会稍微闪烁一下。

上述就是C#学习教程:仅垂直移动表单分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 protected Point StartPosition { get; set; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); StartPosition = this.Location; } protected override void OnMove(EventArgs e) { if (StartPosition == new Point()) return; var currentLocation = Location; Location = new Point(StartPosition.X, currentLocation.Y); base.OnMove(e); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1013906.html

(0)
上一篇 2021年12月31日
下一篇 2021年12月31日

精彩推荐