且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ContextMenu重绘

更新时间:2022-09-04 12:03:18

private void cm_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
Color cms1 = SkinForm.GetColor("gv", "abcolor");
Color cms2 = SkinForm.GetColor("gv", "rhbcolor");
Color cms3 = SkinForm.GetColor("gv", "afcolor");

MenuItem mnuItem = (MenuItem)sender;

SolidBrush Brush1 = new SolidBrush(cms1);
SolidBrush Brush2 = new SolidBrush(cms2);
SolidBrush Brush3 = new SolidBrush(cms3);

Pen cmPen = new Pen(SkinForm.GetColor("gv", "asfcolor"));

StringFormat Format = new StringFormat();

if ((e.State & DrawItemState.Selected) != 0)
{
if (this.Enabled)
{
e.Graphics.FillRectangle(Brush1, e.Bounds);
e.Graphics.DrawRectangle(cmPen, e.Bounds.Left, e.Bounds.Y, e.Bounds.Width - 2, e.Bounds.Height - 2);
}
}
else
{
e.Graphics.FillRectangle(Brush2, e.Bounds);
}

if (this.Text != "-")
{
Rectangle DisplayRect = new Rectangle(e.Bounds.Left, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Bottom);
Format.Alignment = StringAlignment.Center;

if (this.Enabled)
{
e.Graphics.DrawString(mnuItem.Text, gv_songlist.Font, Brush3, DisplayRect, Format);
}
else
{
e.Graphics.DrawString(mnuItem.Text, gv_songlist.Font, Brush3, DisplayRect, Format);
}
}
else
{
e.Graphics.DrawLine(cmPen, e.Bounds.Left, e.Bounds.Top, e.Bounds.Right, e.Bounds.Top);
}
}
catch
{ }
}

private void cm_MeasureItem(object sender, MeasureItemEventArgs e)
{
try
{
MenuItem mnuItem = (MenuItem)sender;
e.ItemHeight = (int)e.Graphics.MeasureString(Text, gv_songlist.Font).Height + 3;
e.ItemWidth = (int)e.Graphics.MeasureString(Text, gv_songlist.Font).Width + 3;
}
catch
{ }
}

显示:
Win32.POINT point = new Win32.POINT();
Win32.GetCursorPos(ref point);
Win32.SetForegroundWindow(this.Handle);
Win32.TrackPopupMenuEx(this.cm.Handle, 0x40, point.x, point.y, this.Handle, new Win32.TPMPARAMS());

----------------------

public class Win32
{

[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern int SetForegroundWindow(IntPtr hwnd);

[DllImport("user32.dll", EntryPoint = "TrackPopupMenuEx")]
public static extern int TrackPopupMenuEx(
IntPtr hMenu,
int un,
int n1,
int n2,
IntPtr hWnd,
TPMPARAMS lpTPMParams
);


[StructLayout(LayoutKind.Sequential)]
public struct TPMPARAMS
{
public int cbSize;
public RECT rcExclude;
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}


[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public static extern int GetCursorPos(ref POINT lpPoint);

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/09/24/1573129.html,如需转载请自行联系原作者