最簡單的方法(因爲這似乎不是一個實際的菜單)是創建一個無國界的形式,並添加陰影到它:
public class ShadowForm : Form
{
// Define the CS_DROPSHADOW constant
private const int CS_DROPSHADOW = 0x00020000;
// Override the CreateParams property
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
}
關於位置,沒有太多吧。只需檢查Cursor.Position
或使用您的MouseUp
事件處理程序中的參數設置座標。
完整代碼看起來是這樣的:
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
protected override OnMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var menu = new CustomMenu();
menu.Location = PointToScreen(e.Location);
menu.Show(this);
}
}
}
,併爲 「菜單」 的形式:
public partial class CustomMenu : Form
{
public CustomMenu()
{
InitializeComponent();
this.StartPosition = FormStartPosition.Manual;
}
private const int CS_DROPSHADOW = 0x00020000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
protected override void OnLostFocus(EventArgs e)
{
this.Close();
base.OnLostFocus(e);
}
}
我想你在談論的WinForms嗎? – 2012-02-29 11:34:36
顯然你錯過了工具箱中的ContextMenuStrip控件。 – adelphus 2012-02-29 11:39:09
不,我沒有錯過,我想創造我自己的,做更多,然後選擇其中的一個項目。是的這是關於C#中的WinForms。 – amnesyc 2012-02-29 11:41:17