`
wsql
  • 浏览: 11788144 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

在系统菜单中添加自定义菜单项

阅读更多

在系统菜单中添加自定义菜单项

以下是我的代码.
看了一个似乎是MSDN上的文章..
大致看了下, 很不幸....不知道是不是这文章作者太武断..还是真的.Net翅膀还没长硬...

(我的E文很差,要是译错的地方,大家装做不知道就好了,给点面子哈...)

上面说到:“如果想在control box (system) menu上添加新项, 用.Net本身的代码是没有办法实现的.很幸运的,无论如何,用一点WindowsApi可以得到一样的结果.“....
(牢骚: 靠.....这还叫幸运?????????????? 如果高级功能都用API的话,还要C#的.WINFORM做啥呢???????????)

First, you need to include the System.Runtime.InteropServices namespace:
(第一步,你需要包含 System.Runtime.InteropServices命名空间. )

Next, define the functions necessary to add items to the system menu:
(接下来,定义...添加项目到系统菜单中...所必需的函数....)

也就是:
[C# .NET]
[DllImport("user32.dll")]
private static extern int GetSystemMenu(int hwnd, int bRevert);
[DllImport("user32.dll")]
private static extern int AppendMenu(int hMenu, int Flagsw, int IDNewItem, string lpNewItem);

[Visual Basic .NET]

Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Integer, _
ByVal bRevert As Integer) As Integer
Private Declare Function AppendMenu Lib "user32.dll" Alias "AppendMenuA" (ByVal hMenu As Integer, _
ByVal Flagsw As Integer, IDNewItem As Integer, ByVal lpNewItem As String) As Integer

(作者还说..他始终没有办法明白的是,为什么'别名' Alias "AppendMenuA" 在VB.NET中是必需的,如果你不包含它,.Net会抛出一个异常说,在调用AppendMenu的时候,在User32.dll中无法找到entry point ...)

我的话entry point 是啥来着..忘了..入口点?不晓得了哈.反正有错就是了..

Adding the items to the menu

Adding items to the menu is now pretty simple. To add a separator, you simply pass the arguments 0xA00, 0 and null (Nothing in Visual Basic) into the AppendMenu function. To create a menu item, you pass in 0, the ID to assign to the new the menu item (e.g., 100) and a string containing the text to display for the menu item. For example:
添加项目到菜单中是很容易的.要添加一个分隔符,你简单的传递参数 0xA00,0和null(在VB中是Nothing)到AppendMenu函数.要建立一个菜单项,你传递 0,分配给新菜单项的ID,和一个包含菜单项上显示的文本的字符串...例如:
[C# .NET]
// Get handle to system menu
int systemMenu = GetSystemMenu(this.Handle.ToInt32(), 0);

// Create a separator
AppendMenu(systemMenu, 0xA00, 0, null);

// Add the about menu item
AppendMenu(systemMenu, 0, 100, "&About...");

[Visual Basic .NET]
' Get handle to system menu
Dim systemMenu As Integer = GetSystemMenu(Me.Handle.ToInt32(), 0)

' Create a separator
AppendMenu(systemMenu, &HA000, 0, Nothing)

' Add the about menu item
AppendMenu(systemMenu, 0, 100, "&About...")

============================
上面是他的代码.
但是我自己试了试.
在添加分隔符的时候,参数2用 0也可以..一切正常.
还没搞懂为什么要指定 0xA00 .
嗬嗬...
下面是我的代码: 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;


namespace 包含程序
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
[DllImport("user32.dll")]
private static extern int GetSystemMenu(int hwnd, int bRevert);
[DllImport("user32.dll")]
private static extern int AppendMenu(int hMenu,int Flagsw,int IDNewItem,string lpNewItem);
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(571, 441);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
int m=GetSystemMenu(this.Handle.ToInt32(),0);
AppendMenu(m,0,0,null);
AppendMenu(m,0,901,"C# Menu");
}
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
if(m.Msg==0x112)
{
if(m.WParam.ToInt32()==901)
{
MessageBox.Show("被Click!");
}
}
}

}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics