(十九)c#Winform自定义控件-停靠窗体
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">
目录
https://www.cnblogs.com/bfyx/p/11364884.html
准备工作
这是一个可停靠在指定位置或停靠在某个控件旁边的无焦点窗体,市区焦点会关闭
开始
添加一个Form,命名为FrmAnchor,实现接口IMessageFilter
有2个构造函数
#region 构造函数
/// <summary>
/// 功能描述:构造函数
/// 作 者:HZH
/// 创建日期:2019-02-27 11:49:08
/// 任务编号:POS
/// </summary>
/// <param name="parentControl">父控件</param>
/// <param name="childControl">子控件</param>
/// <param name="deviation">偏移</param>
public FrmAnchor(Control parentControl, Control childControl, Point? deviation = null)
{
m_parentControl = parentControl;
InitializeComponent();
this.Size = childControl.Size;
this.HandleCreated += FrmDownBoard_HandleCreated;
this.HandleDestroyed += FrmDownBoard_HandleDestroyed;
this.Controls.Add(childControl);
childControl.Dock = DockStyle.Fill;
Point p = parentControl.Parent.PointToScreen(parentControl.Location);
int intX = ;
int intY = ;
if (p.Y + parentControl.Height + childControl.Height > Screen.PrimaryScreen.Bounds.Height)
{
intY = p.Y - childControl.Height - ;
blnDown = false;
}
else
{
intY = p.Y + parentControl.Height + ;
blnDown = true;
} if (p.X + childControl.Width > Screen.PrimaryScreen.Bounds.Width)
{
intX = Screen.PrimaryScreen.Bounds.Width - childControl.Width; }
else
{
intX = p.X;
}
if (deviation.HasValue)
{
intX += deviation.Value.X;
intY += deviation.Value.Y;
}
this.Location = new Point(intX, intY);
} public FrmAnchor(Control parentControl, Size size, Point? deviation = null)
{
m_parentControl = parentControl;
InitializeComponent();
this.Size = size;
this.HandleCreated += FrmDownBoard_HandleCreated;
this.HandleDestroyed += FrmDownBoard_HandleDestroyed; Point p = parentControl.Parent.PointToScreen(parentControl.Location);
int intX = ;
int intY = ;
if (p.Y + parentControl.Height + size.Height > Screen.PrimaryScreen.Bounds.Height)
{
intY = p.Y - size.Height - ;
blnDown = false;
}
else
{
intY = p.Y + parentControl.Height + ;
blnDown = true;
} if (p.X + size.Width > Screen.PrimaryScreen.Bounds.Width)
{
intX = Screen.PrimaryScreen.Bounds.Width - size.Width; }
else
{
intX = p.X;
}
if (deviation.HasValue)
{
intX += deviation.Value.X;
intY += deviation.Value.Y;
}
this.Location = new Point(intX, intY);
} #endregion
消息筛选器处理一下
private void FrmDownBoard_HandleDestroyed(object sender, EventArgs e)
{
Application.RemoveMessageFilter(this);
} private void FrmDownBoard_HandleCreated(object sender, EventArgs e)
{
Application.AddMessageFilter(this);
} public bool PreFilterMessage(ref Message m)
{
if (m.Msg != 0x0201 || this.Visible == false)
return false;
var pt = this.PointToClient(MousePosition);
this.Visible = this.ClientRectangle.Contains(pt);
return false;
}
无焦点处理
#region 无焦点窗体 [System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
private const int WM_ACTIVATE = 0x006;
private const int WM_ACTIVATEAPP = 0x01C;
private const int WM_NCACTIVATE = 0x086;
private const int WA_INACTIVE = ;
private const int WM_MOUSEACTIVATE = 0x21;
private const int MA_NOACTIVATE = ;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = new IntPtr(MA_NOACTIVATE);
return;
}
else if (m.Msg == WM_NCACTIVATE)
{
if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
{
if (m.LParam != IntPtr.Zero)
{
SetActiveWindow(m.LParam);
}
else
{
SetActiveWindow(IntPtr.Zero);
}
}
}
base.WndProc(ref m);
} #endregion
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Owner != null)
{
Form frm = this.Owner as Form;
IntPtr _ptr = ControlHelper.GetForegroundWindow();
if (_ptr != frm.Handle)
{
this.Hide();
}
}
}
显示和关闭动画
private void FrmAnchor_VisibleChanged(object sender, EventArgs e)
{
timer1.Enabled = this.Visible;
if (Visible)
{
if (blnDown)
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_POSITIVE);
else
{
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_NEGATIVE);
}
}
else
{
if (blnDown)
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_NEGATIVE | ControlHelper.AW_HIDE);
else
{
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_POSITIVE | ControlHelper.AW_HIDE); }
}
}
再看一下完整代码
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:FrmAnchor.cs
// 创建日期:2019-08-15 16:04:24
// 功能描述:FrmAnchor
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Forms
{
/// <summary>
/// 功能描述:停靠窗体
/// 作 者:HZH
/// 创建日期:2019-02-27 11:49:03
/// 任务编号:POS
/// </summary>
public partial class FrmAnchor : Form, IMessageFilter
{
Control m_parentControl = null;
private bool blnDown = true;
#region 构造函数
/// <summary>
/// 功能描述:构造函数
/// 作 者:HZH
/// 创建日期:2019-02-27 11:49:08
/// 任务编号:POS
/// </summary>
/// <param name="parentControl">父控件</param>
/// <param name="childControl">子控件</param>
/// <param name="deviation">偏移</param>
public FrmAnchor(Control parentControl, Control childControl, Point? deviation = null)
{
m_parentControl = parentControl;
InitializeComponent();
this.Size = childControl.Size;
this.HandleCreated += FrmDownBoard_HandleCreated;
this.HandleDestroyed += FrmDownBoard_HandleDestroyed;
this.Controls.Add(childControl);
childControl.Dock = DockStyle.Fill;
Point p = parentControl.Parent.PointToScreen(parentControl.Location);
int intX = ;
int intY = ;
if (p.Y + parentControl.Height + childControl.Height > Screen.PrimaryScreen.Bounds.Height)
{
intY = p.Y - childControl.Height - ;
blnDown = false;
}
else
{
intY = p.Y + parentControl.Height + ;
blnDown = true;
} if (p.X + childControl.Width > Screen.PrimaryScreen.Bounds.Width)
{
intX = Screen.PrimaryScreen.Bounds.Width - childControl.Width; }
else
{
intX = p.X;
}
if (deviation.HasValue)
{
intX += deviation.Value.X;
intY += deviation.Value.Y;
}
this.Location = new Point(intX, intY);
} public FrmAnchor(Control parentControl, Size size, Point? deviation = null)
{
m_parentControl = parentControl;
InitializeComponent();
this.Size = size;
this.HandleCreated += FrmDownBoard_HandleCreated;
this.HandleDestroyed += FrmDownBoard_HandleDestroyed; Point p = parentControl.Parent.PointToScreen(parentControl.Location);
int intX = ;
int intY = ;
if (p.Y + parentControl.Height + size.Height > Screen.PrimaryScreen.Bounds.Height)
{
intY = p.Y - size.Height - ;
blnDown = false;
}
else
{
intY = p.Y + parentControl.Height + ;
blnDown = true;
} if (p.X + size.Width > Screen.PrimaryScreen.Bounds.Width)
{
intX = Screen.PrimaryScreen.Bounds.Width - size.Width; }
else
{
intX = p.X;
}
if (deviation.HasValue)
{
intX += deviation.Value.X;
intY += deviation.Value.Y;
}
this.Location = new Point(intX, intY);
} #endregion private void FrmDownBoard_HandleDestroyed(object sender, EventArgs e)
{
Application.RemoveMessageFilter(this);
} private void FrmDownBoard_HandleCreated(object sender, EventArgs e)
{
Application.AddMessageFilter(this);
} #region 无焦点窗体 [System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
private const int WM_ACTIVATE = 0x006;
private const int WM_ACTIVATEAPP = 0x01C;
private const int WM_NCACTIVATE = 0x086;
private const int WA_INACTIVE = ;
private const int WM_MOUSEACTIVATE = 0x21;
private const int MA_NOACTIVATE = ;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = new IntPtr(MA_NOACTIVATE);
return;
}
else if (m.Msg == WM_NCACTIVATE)
{
if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
{
if (m.LParam != IntPtr.Zero)
{
SetActiveWindow(m.LParam);
}
else
{
SetActiveWindow(IntPtr.Zero);
}
}
}
base.WndProc(ref m);
} #endregion public bool PreFilterMessage(ref Message m)
{
if (m.Msg != 0x0201 || this.Visible == false)
return false;
var pt = this.PointToClient(MousePosition);
this.Visible = this.ClientRectangle.Contains(pt);
return false;
} private void FrmAnchor_Load(object sender, EventArgs e)
{ } private void FrmAnchor_VisibleChanged(object sender, EventArgs e)
{
timer1.Enabled = this.Visible;
if (Visible)
{
if (blnDown)
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_POSITIVE);
else
{
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_NEGATIVE);
}
}
else
{
if (blnDown)
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_NEGATIVE | ControlHelper.AW_HIDE);
else
{
ControlHelper.AnimateWindow(this.Handle, , ControlHelper.AW_VER_POSITIVE | ControlHelper.AW_HIDE); }
}
} private void timer1_Tick(object sender, EventArgs e)
{
if (this.Owner != null)
{
Form frm = this.Owner as Form;
IntPtr _ptr = ControlHelper.GetForegroundWindow();
if (_ptr != frm.Handle)
{
this.Hide();
}
}
} }
}
namespace HZH_Controls.Forms
{
partial class FrmAnchor
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmAnchor));
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// FrmAnchor
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(, );
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FrmAnchor";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "FrmAnchor";
this.TopMost = true;
this.Load += new System.EventHandler(this.FrmAnchor_Load);
this.VisibleChanged += new System.EventHandler(this.FrmAnchor_VisibleChanged);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Timer timer1;
}
}
用处及效果
用处:弹出菜单,文本框弹出键盘等
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧
(十九)c#Winform自定义控件-停靠窗体的更多相关文章
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十)c#Winform自定义控件-有后退的窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十二)c#Winform自定义控件-半透明窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十三)c#Winform自定义控件-等待窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (五十)c#Winform自定义控件-滑块
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (四十九)Quartz2D自定义控件
利用Quartz2D来自定义UIImageView: 模仿UIImageView: 设置frame,设置图片. 注意一个细节,自定义的imageView,应该通过重写set方法来设置图片并且重绘,否则 ...
- (十)c#Winform自定义控件-横向列表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (六十)c#Winform自定义控件-鼓风机(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (七十)c#Winform自定义控件-饼状图
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
随机推荐
- 十代雅阁广东车友群,雅阁广州车友群,深圳雅阁车友群,雅阁微信群、雅阁车友群、十代雅阁交流微信QQ群
最近一直在关注第十代雅阁,不论是普通汽油版本还是油电混动版本都很不错,在网上看到很多评测文章和视频 后续都会整理发布到微信群中. 由于论坛发帖,博客发文都不是很方便,为了及时沟通,先创建了微信群,方便 ...
- 剑指offer第二版-总结:排序算法
1.排序算法比较: 2.java实现 快排: /** * 快排 * * @since 2019年2月26日 下午1:37:34 * @author xuchao */ public class Qui ...
- 剑指offer第二版-7.重建二叉树
描述:输入某二叉树的前序遍历和中序遍历结果,重建该二叉树.假设前序遍历或中序遍历的结果中无重复的数字. 思路:前序遍历的第一个元素为根节点的值,据此将中序遍历数组拆分为左子树+root+右子树,前序遍 ...
- 性能测试-实例讲解VU、RPS、RT公式换算
概述 今天看到一篇文章讲解VU.RPS.RT,中间有一个公式如下图 并发数 = RPS * 响应时间 于是我在本地做了几次实验,试图验证一下公式的准确性 实验网站 www.baidu.com 第一次 ...
- Neo4j配置文件neo4j.conf
机器配置为256G内存,48核(物理核24)cpu,4T SAS盘(建议磁盘使用SSD) 图数据库Neo4j配置文件neo4j.conf 中常用参数: dbms.active_database=gra ...
- R035---偷个懒,用UiPath录制电脑操作过程,迅速实现流程自动化
一.缘起 UiPath可以录制你操作电脑的过程,从而实现自动化. 目前有点鸡肋,因为有些操作过程无法录制,例如: 键盘快捷键 修改键 右键点击 鼠标悬停 即便如此,录制功能有时候还是可以用一下,特别 ...
- C#控制台打开VM虚拟机
添加引用->VixCOM.dll (在vix文件夹下) VixWrapper.cs using System; using System.Collections.Generic; using S ...
- C#3.0新增功能06 对象和集合初始值设定项
连载目录 [已更新最新开发文章,点击查看详细] 使用 C# 可以在单条语句中实例化对象或集合并执行成员分配. 对象初始值设定项 使用对象初始值设定项,你可以在创建对象时向对象的任何可访问字段或属 ...
- Django自带的后台管理系统
1.准备工作: 1-1.创建django项目和应用 1-2.修改settings.py配置文件: #应用配置: INSTALLED_APPS = [ 'django.contrib.admin', ' ...
- 【Java中级】(三)IO
1. 流分为字节流和字符流 2. 字节流下面常用的又有数据流和对象流 3. 字符流下面常用的又有缓存流 文件对象 文件和文件夹都用File表示 //file path : 文件的绝对路径或相对路径Fi ...