WPF小笔记-Popup拖动
查看了下MSDN发现Popup没有类拟Drag相关的属性和方法,第一时间想了thumb。忙了一会未果,就想起了强大的google。
发现中文资料很少,英文的发现有两篇很不错的,所以笔记在博客园里,希望对园里的朋友有用。
楼主推荐使用social.msdm的方法,试过很好用:
<Popup Name="puSkills" Placement="MousePoint" PopupAnimation="Scroll" AllowsTransparency="True" IsEnabled="True" IsOpen="False"> <Border BorderBrush="DarkCyan" BorderThickness="3"> <StackPanel Background="AliceBlue" VerticalAlignment="Center" Height="400" Width="400"> <Label Name="lblCaption" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="Blue" Background="Blue" MouseLeftButtonDown="lblCaption_MouseLeftButtonDown">Move</Label> <StackPanel> <TextBlock HorizontalAlignment="Right"> Some Contents........... </TextBlock> </StackPanel> </StackPanel> </Border> </Popup>
后台CS代码:
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(POINT Point); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetCursorPos(out POINT lpPoint); [DllImportAttribute("user32.dll")] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImportAttribute("user32.dll")] public static extern bool ReleaseCapture(); [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; } public const int WM_NCLBUTTONDOWN = 0xA1; public const int HT_CAPTION = 0x2; private void lblCaption_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { POINT curPos; IntPtr hWndPopup; GetCursorPos(out curPos); hWndPopup = WindowFromPoint(curPos); ReleaseCapture(); SendMessage(hWndPopup, WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero); }
Click the Move Content in label.. then move the mouse. Thank you!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南