winform 获取NotifyIcon的位置
Winform 获取NotifyIcon的位置
主要代码:
public class NotifyIconHelper
{public static Rectangle GetIconRect(NotifyIcon icon){RECT rect = new RECT();NOTIFYICONIDENTIFIER notifyIcon = new NOTIFYICONIDENTIFIER();notifyIcon.cbSize = Marshal.SizeOf(notifyIcon);//use hWnd and id of NotifyIcon instead of guid is needednotifyIcon.hWnd = GetHandle(icon);notifyIcon.uID = GetId(icon);int hresult = Shell_NotifyIconGetRect(ref notifyIcon, out rect);//rect now has the position and size of iconreturn new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);}[StructLayout(LayoutKind.Sequential)]private struct RECT{public Int32 left;public Int32 top;public Int32 right;public Int32 bottom;}[StructLayout(LayoutKind.Sequential)]private struct NOTIFYICONIDENTIFIER{public Int32 cbSize;public IntPtr hWnd;public Int32 uID;public Guid guidItem;}[DllImport("shell32.dll", SetLastError = true)]private static extern int Shell_NotifyIconGetRect([In]ref NOTIFYICONIDENTIFIER identifier, [Out]out RECT iconLocation);private static FieldInfo windowField = typeof(NotifyIcon).GetField("window", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);private static IntPtr GetHandle(NotifyIcon icon){if (windowField == null) throw new InvalidOperationException("[Useful error message]");NativeWindow window = windowField.GetValue(icon) as NativeWindow;if (window == null) throw new InvalidOperationException("[Useful error message]"); // should not happen?return window.Handle;}private static FieldInfo idField = typeof(NotifyIcon).GetField("id", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);private static int GetId(NotifyIcon icon){if (idField == null) throw new InvalidOperationException("[Useful error message]");return (int)idField.GetValue(icon);}
}
参考资料:
1、https://stackoverflow.com/questions/26153810/get-the-applications-notifyicon-rectangle
2、http://pinvoke.net/default.aspx/shell32.Shell_NotifyIconGetRect
winform 获取NotifyIcon的位置相关推荐
- Winform窗口弹出位置设置
Winform窗口弹出位置控制 窗体的弹出位置可以由属性StartPosition来指定,默认值有: Manural 自定义,由属性Location指定: CenterScreen 屏幕中央: Win ...
- Winform窗口弹出位置控制
Winform窗口弹出位置控制 窗体的弹出位置可以由属性StartPosition来指定,默认值有: Manural 自定义,由属性Location指定: CenterScreen 屏幕中央: Win ...
- H5使用百度地图SDK获取用户当前位置并且标记显示在地图
代码实现功能: H5使用百度地图SDK获取用户当前位置并且标记显示在地图,点击该标记弹出一层自定义的HTML. 效果图: 代码: <!DOCTYPE html> <html>& ...
- Swift 使用CoreLocation获取定位与位置信息
大多数情况下APP会在开启应用的时候获取当前的位置,所以我写在APPDelegate里 第一步 import CoreLocationvar locationManager = CLLocationM ...
- 根据经纬度获取用户当前位置信息
根据上篇文章获取的经纬度获取用户当前的位置信息 //获取用户所在位置信息ADDRESS func getUserAddress() { let latitude : CLLocationDegrees ...
- C# WinForm获取当前路径汇总
Winform获取应用程序的当前路径的方法集合,具体如下,值得收藏//获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Loca ...
- html自动获取用户位置,HTML5 - 使用Geolocation(地理定位)获取用户的位置
一.Geolocation(地理定位) 1,基本介绍 (1)虽然 Geolocation 经常以 HTML5 的名义提到,但地理定位实际上是一个单独的标准,而且也不是经由 WHATWG 制定的. (2 ...
- 原生js--兼容获取窗口滚动条位置和窗口大小的方法
各个浏览器对获取获取窗口滚动条位置和窗口大小没有提供统一的API,以下是对其封装,解决兼容性问题 /** * 获取浏览器视口的大小(显示文档的部分) * */ function getViewP ...
- Winform获取应用程序的当前路径的方法集合,具体如下,值得收藏
Winform获取应用程序的当前路径的方法集合,具体如下,值得收藏//获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Loca ...
- Oracle 获取字符的位置并截取字符串
1.获取字符所在位置 用法:instr (字符串, 查找的字符) select instr('福建省/厦门市/思明区', '/') from dual; 返回值:4 2.截取字符串 用法:substr ...
最新文章
- Android ListView 横向滑动删除 Item
- C++ 类访问控制(public/protected/private)小结
- 好用的Markdown编辑器推荐
- .NET Core授权失败如何自定义响应信息?
- 需求分析师与产品经理的区别
- (96)Verilog HDL:点灯设计
- 力扣35.搜索插入位置
- bigdata是什么
- 随机函数(Pascal入门)
- bigdecimal java 最大值_Java中的数学运算BigDecimal
- 历时半载,我的新课上线了
- java 中类超时_Java中httpClient中的三种超时设置总结
- 证券交易业务基础知识
- Canny边缘检测非极大值抑制法在双立方插值(Bicubic)图像边缘优化
- 汉语数字转换成阿拉伯数字
- Navicat-在创建函数时提示Access violation at address ... in module ‘navicat.exe‘
- 数据可视化之大数据平台可视化
- Android避坑指南,Gson与Kotlin碰撞出一个不安全的操作
- 投资收购微盟、博易智软后,要做中国Salesforce+ Palantir的天马股份再发智能商业π型战略
- 非常详细的SIP协议总结