|
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Runtime; using System.Runtime.InteropServices;
namespace CustomerBrowser { public class nosystemmenuexception : System.Exception {}
//这些值来自于msdn
public enum itemflags { // the item ... mfunchecked = 0x00000000, // ... is not checked mfstring = 0x00000000, // ... contains a string as label mfdisabled = 0x00000002, // ... is disabled mfgrayed = 0x00000001, // ... is grayed mfchecked = 0x00000008, // ... is checked mfpopup = 0x00000010, // ... is a popup menu. pass the
// menu handle of the popup // menu into the id parameter.
mfbarbreak = 0x00000020, // ... is a bar break mfbreak = 0x00000040, // ... is a break mfbyposition = 0x00000400, // ... is identified by the position mfbycommand = 0x00000000, // ... is identified by its id mfseparator = 0x00000800 // ... is a seperator (string and
// id parameters are ignored). }
public enum windowmessages { wmsyscommand = 0x0112 }
// /// 帮助实现操作系统菜单的类的定义 ///.
//注意:用p/invoke调用动态链接库中非托管函数时,应执行如下步骤: //1,定位包含该函数的dll。 //2,把该dll库装载入内存。 //3,找到即将调用的函数地址,并将所有的现场压入堆栈。 //4,调用函数。 //
public class systemmenu { // 提示:c#把函数声明为外部的,而且使用属性dllimport来指定dll //和任何其他可能需要的参数。 // 首先,我们需要getsystemmenu() 函数 // 注意这个函数没有unicode 版本
[DllImport("user32", EntryPoint = "GetSystemMenu", SetLastError = true, CharSet=CharSet.Unicode , ExactSpelling=true, CallingConvention = CallingConvention.Winapi)]
private static extern IntPtr apiGetSystemMenu(IntPtr windowhandle, int breset); // 还需要appendmenu()。 既然 .net 使用unicode, // 我们应该选取它的unicode版本。
[DllImport("user32", EntryPoint = "AppendMenuW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention=CallingConvention.Winapi )]
private static extern int apiAppendMenu(IntPtr menuhandle, int flags, int newid, string item);
//还可能需要insertmenu()
[DllImport("user32", EntryPoint = "InsertMenuW", SetLastError = true, CharSet=CharSet.Unicode , ExactSpelling=true, CallingConvention=CallingConvention.Winapi )]
private static extern int apiInsertMenu(IntPtr hmenu, int position, int flags, int newid, string item); private IntPtr m_sysmenu = IntPtr.Zero ; // 系统菜单句柄
public systemmenu( ) {}
// 在给定的位置(以0为索引开始值)插入一个分隔条
public bool insertseparator ( int pos ) { return ( insertmenu(pos, itemflags.mfseparator |itemflags.mfbyposition, 0, "") ); }
// 简化的insertmenu(),前提――pos参数是一个0开头的相对索引位置
public bool insertmenu ( int pos, int id, string item ) { return ( insertmenu(pos, itemflags.mfbyposition |itemflags.mfstring, id, item) ); }
// 在给定位置插入一个菜单项。具体插入的位置取决于flags
public bool insertmenu ( int pos, itemflags flags, int id, string item ) { return (apiInsertMenu(m_sysmenu, pos, (Int32)flags, id, item) == 0); }
// 添加一个分隔条
public bool appendseparator ( ) { return appendmenu(0, "", itemflags.mfseparator); }
// 使用itemflags.mfstring 作为缺省值
public bool appendmenu ( int id, string item ) { return appendmenu(id, item, itemflags.mfstring); }
// 被取代的函数
public bool appendmenu ( int id, string item, itemflags flags ) { return ( apiAppendMenu(m_sysmenu, (int)flags, id, item) == 0 ); }
//从一个form对象检索一个新对象
public static systemmenu fromform ( Form frm ) { systemmenu csysmenu = new systemmenu(); csysmenu.m_sysmenu = apiGetSystemMenu(frm.Handle , 0); if ( csysmenu.m_sysmenu == IntPtr.Zero ) { // 一旦失败,引发一个异常 throw new nosystemmenuexception(); } return csysmenu; }
// 当前窗口菜单还原 public static void resetsystemmenu ( Form frm ) { apiGetSystemMenu(frm.Handle , 1); }
// 检查是否一个给定的id在系统菜单id范围之内 public static bool verifyitemid ( int id ) { return (bool)( id < 0xf000 && id > 0 ); } } }
调用systemmenu类
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; using System.Runtime.InteropServices;
namespace CustomerBrowser { public partial class Form1 : Form { // systemmenu 对象
private systemmenu m_systemmenu = null;
// id 常数定义
private const int m_ShangHaiID = 0x100; private const int m_JapanID = 0x102; private const int m_TopMostID = 0x103;
private const int m_resetid = 0x101;
private const string strShangHaiURL = "http://192.168.10.253/ImageViewer?Direction=&Resolution=640x480&Quality=Standard&Size=STD&PresetOperation=Move&Data=0&Frame2=PanTilt&Type=&Language=1&RPeriod=65535&Sound=Enable&Mode=JPEG&SendMethod=1&View=Full"; private const string strJapanURL = "http://203.141.137.185:50080/ImageViewer?Direction=&Resolution=320x240&Quality=Standard&Size=STD&PresetOperation=Move&Data=0&Frame2=PanTilt&Type=&Language=1&RPeriod=3&Sound=Enable&Mode=JPEG&SendMethod=1&View=Full";
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { try {
m_systemmenu = systemmenu.fromform(this);
// 添加一个separator ...
m_systemmenu.appendseparator();
m_systemmenu.appendmenu(m_ShangHaiID, "上海"); m_systemmenu.appendmenu(m_JapanID, "日本"); m_systemmenu.appendmenu(m_TopMostID, "置顶");
//// 在菜单顶部加上"复位"菜单项
//m_systemmenu.insertseparator(0);
//m_systemmenu.insertmenu(0, m_resetid, "复位系统菜单");
}
catch (nosystemmenuexception /* err */ ) {
// 建立你的错误处理器
} //this.webBrowser1.Navigate("http://192.168.10.253/ImageViewer?Direction=&Resolution=640x480&Quality=Standard&Size=STD&PresetOperation=Move&Data=0&Frame2=PanTilt&Type=&Language=1&RPeriod=65535&Sound=Enable&Mode=JPEG&SendMethod=1&View=Full"); }
//protected override void wndproc(ref Message msg) //{ // base.wndproc(ref msg); //}
protected override void WndProc(ref Message msg) { // 通过截取wm_syscommand消息并进行处理 // 注意,消息wm_syscommand被定义在windowmessages枚举类中 // 消息的wparam参数包含点击的项的id // 该值与通过上面类的insertmenu()或appendmenu()成员函数传递的一样
if (msg.Msg == (int)windowmessages.wmsyscommand) { switch (msg.WParam.ToInt32()) { //case m_resetid: // reset菜单项的id // { // if (MessageBox.Show(this, "/tare you sure?", "question", MessageBoxButtons.YesNo) == // DialogResult.Yes) // { // 复位系统菜单 // systemmenu.resetsystemmenu(this); // } // } break; case m_ShangHaiID: NaviURL(strShangHaiURL); break; case m_JapanID: NaviURL(strJapanURL); break; case m_TopMostID: this.TopMost = !this.TopMost; break; } } // 调用基类函数 base.WndProc(ref msg); }
private void NaviURL(string strURL) { this.webBrowser1.Navigate(strURL); } } }
|