C#模拟弹出窗体系统菜单介绍
using System.Runtime.InteropServices; const uint TPM_LEFTBUTTON = 0; const uint TPM_RIGHTBUTTON = 2; const uint TPM_LEFTALIGN = 0; const uint TPM_CENTERALIGN = 4; const uint TPM_RIGHTALIGN = 8; const uint TPM_TOPALIGN = 0; const uint TPM_VCENTERALIGN = 0x10; const uint TPM_BOTTOMALIGN = 0x20; const uint TPM_RETURNCMD = 0x100; const uint WM_SYSCOMMAND = 0x0112; #region DllImport [DllImport("User32.dll")] static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("User32.dll")] static extern bool GetCursorPos(out Point lpPoint); [DllImport("User32.dll")] static extern int TrackPopupMenu(IntPtr hMenu, uint uFlags, int x, int y, int nReserved, IntPtr hWnd, out Rectangle prcRect); [DllImport("User32.DLL")] public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); #endregion private void button1_Click(object sender, EventArgs e) { Point vPoint; Rectangle vRect; GetCursorPos(out vPoint); SendMessage(Handle, WM_SYSCOMMAND, TrackPopupMenu( GetSystemMenu(Handle, false), TPM_RETURNCMD | TPM_LEFTBUTTON, vPoint.X, vPoint.Y, 0, Handle, out vRect), 0); }
C#实现类似MSN Messenger的弹出提示窗体
也许有的朋友最近有些疑惑,我的Blog叫Windows Mobile开发历险,为什么最近的文章都是win32下的,其实我最近写的东西是Windows Mobile/Windows xp结构的,所以对于win32的某些技术用得到,也有了些自己的心得体会。后来想了想,不能让这些东西白白流失,还是写下来吧。以便让自己记得长久,还能跟大家分享。
今天的文章,我主要介绍MSN Messenger弹出窗体的实现方法,MSN Messenger的弹出窗体就是再联系人登录或者发送新消息时候桌面右下角弹出的窗体。实现这样的窗体很简单,delphi中我写过这样的组件,不过因为号称商业机密的东西不便发表。C#就自由了,随便发布,哈哈!
其实实现这个弹出窗体只需调用windows API函数即可,AnimateWindow——我们都可以顾名思义了,哈哈!就是动态窗口!那么接下来我们只需知道如何调用该函数,如果定义该函数的参数,就可以了。下面我给出一个实例,并配上简单的注释,这样就能实现我们所要的功能了。如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace FormHintTest { public partial class FormHint : Form { public FormHint() { InitializeComponent(); // 根据系统当前分辨率确定Hint窗体的位置 int x = Screen.PrimaryScreen.WorkingArea.Width - this.Width - 1; int y = Screen.PrimaryScreen.WorkingArea.Height - this.Height - 1; this.Location = new Point(x, y); } public void Popup() { //在public函数中调用API函数实现动态效果 AnimateWindow(this.Handle, 200, (uint)AnimateWindowFlags.AW_SLIDE | (uint)AnimateWindowFlags.AW_VER_NEGATIVE | (uint)AnimateWindowFlags.AW_ACTIVATE); this.Show(); } [DllImport("user32.dll")] //Invoke Windows API函数 AnimateWindow public extern static bool AnimateWindow(IntPtr hwnd,uint dwTime,uint dwFlags); public enum AnimateWindowFlags : uint //AnimateWindow的dwFlags参数定义 { AW_HOR_POSITIVE = 0x00000001, AW_HOR_NEGATIVE = 0x00000002, AW_VER_POSITIVE = 0x00000004, AW_VER_NEGATIVE = 0x00000008, AW_CENTER = 0x00000010, AW_HIDE = 0x00010000, AW_ACTIVATE = 0x00020000, AW_SLIDE = 0x00040000, AW_BLEND = 0x00080000 } } }