官网

http://www.hzhcontrols.com

前提

入行已经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自定义控件-停靠窗体的更多相关文章

  1. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (二十二)c#Winform自定义控件-半透明窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (二十三)c#Winform自定义控件-等待窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  5. (五十)c#Winform自定义控件-滑块

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (四十九)Quartz2D自定义控件

    利用Quartz2D来自定义UIImageView: 模仿UIImageView: 设置frame,设置图片. 注意一个细节,自定义的imageView,应该通过重写set方法来设置图片并且重绘,否则 ...

  7. (十)c#Winform自定义控件-横向列表

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (六十)c#Winform自定义控件-鼓风机(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (七十)c#Winform自定义控件-饼状图

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. Centos7 防护墙 设置端口

    Centos7中的防火墙调整为firewalld,试一下systemctl stop firewalld关闭防火墙. 命令:systemctl stop firewalld 命令:systemctl ...

  2. [译]试用新的System.Text.Json API

    译注 可能有的小伙伴已经知道了,在.NET Core 3.0中微软加入了对JSON的内置支持. 一直以来.NET开发者们已经习惯使用Json.NET这个强大的库来处理JSON. 那么.NET为什么要增 ...

  3. Jenkins部署Web项目到远程tomcat

    1.填加插件Deploy to container Plugin. 2.在构建任务中填加构建后操作.并做如下配置: WAR/EAR files:是war包的相对路径,如target/xxx.war c ...

  4. MYSQL Got error 28 from storage engine

    网络查找该异常信息,发现 Got error 28 from storage engine 查了一下,数据库文件所在的盘应该没事,应该是数据库用的临时目录空间不够 引用 磁盘临时空间不够导致. 解决办 ...

  5. shell_umask用法

    我曾经用touch命令创建一个文件script,默认的权限是"rw-r- -r- -",有的朋友就有可能问为什么是这种组合?其实,这正是umask命令捣的鬼.在linux上输入:u ...

  6. 一个内存不能被written的问题

    C程序面试中曾经面试过这样一道题: #include <stdio.h> int main() { char *p = "12345"; *p = '6'; print ...

  7. Go语言设计模式汇总

    目录 设计模式背景和起源 设计模式是什么 Go语言模式分类 个人观点 Go语言从面世就受到了业界的普遍关注,随着区块链的火热Go语言的地位也急速蹿升,为了让读者对设计模式在Go语言中有一个初步的了解和 ...

  8. 倍增求LCA学习笔记(洛谷 P3379 【模板】最近公共祖先(LCA))

    倍增求\(LCA\) 倍增基础 从字面意思理解,倍增就是"成倍增长". 一般地,此处的增长并非线性地翻倍,而是在预处理时处理长度为\(2^n(n\in \mathbb{N}^+)\ ...

  9. canvas动画:气泡上升效果

    HTML5中的canvas真是个很强大的东西呢! 这几天突发奇想想做一个气泡上升的动画,经过许久的思考和多次失败,终于做出了如下效果 由于是录制的gif图,看着会有点卡顿,实际演示是很自然的 想要做出 ...

  10. Android/AndroidStudio/idea使用教程之git使用(详细)(码云)

    已经安装好了AndroidStudio,安装教程 本教程是作者自己摸索出来的,有不足之处还请大家海涵.多多拍砖,互相学习. 第一步:下载git,安装git客户端 直接百度git,下载git ​ 安装g ...