对不起,少上网,有时间我会详细解释。。。
const
CM_MENUPOP = WM_USER+1;
procedure SubItemDraw(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
State: TOwnerDrawState);
....
procedure cmMenuPop(var msg: Tmessage); message CM_MENUPOP;
...
procedure SubMenuItemDraw(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
State: TOwnerDrawState);
var
s: string;
R: TRect;
Item: TMenuitem;
x, y: integer;
mWnd: Thandle;
DC: HDC;
pt1, pt2: Tpoint;
mm: TMenu;
begin
item:=TMenuitem(sender);
mm:=item.GetParentMenu;
s:=Item.Caption;
if (odSelected in State) and not item.Enabled then exit;
R:=ARect;
if (odSelected in State) and Item.Enabled then
ACanvas.Brush.Color:=clSelected
else
ACanvas.Brush.Color:=clMenuitem;
ACAnvas.FillRect(ARect);
if (odSelected in State) and Item.Enabled then
ACanvas.Brush.Color:=clmsSelect
else
ACanvas.Brush.Color:=clMenuSide;
R.Right:=R.Left+22;
ACAnvas.FillRect(R);
x:=R.Left+2;
y:=R.Top+(R.Bottom-R.Top-dm.mm.height) div 2;
if (mm.Images<>nil) and (Item.ImageIndex<>-1) then
begin
if Item.Enabled then
mm.Images.Draw(ACanvas, x, y, Item.imageIndex)
else
DrawDisabledImage(Acanvas, Bounds(x, y, 18, 18), TMenu(item.Owner).Images,
item.ImageIndex, false);
end;
if item.Checked then dm.mm.Draw(ACanvas, x, y, 7);
R:=ARect;
R.Left:=R.Left+28;
if Item.IsLine then
begin
ACanvas.Pen.Color:=clEnd;
ACanvas.MoveTo(22, R.Top+(R.Bottom-R.Top) div 2);
ACanvas.LineTo(R.Right, R.Top+(R.Bottom-R.Top) div 2);
exit;
end;
ACanvas.Brush.Style:=bsClear;
if Item.Enabled then
ACanvas.Font.Color:=clBlack
else
ACanvas.Font.Color:=clGrayText;
drawText(ACAnvas.Handle, pchar(s), length(s), R, DT_SINGLELINE or DT_VCENTER);
if (odSelected in State) and Item.Enabled then
begin
ACanvas.Pen.Color:=clslEnd;
ACanvas.Rectangle(Arect);
end;
//画菜单的边框;
Dc:=ACanvas.Handle;
mWnd:=WindowFromDc(DC);
if mWnd=0 then
[red]postmessage(application.MainForm.Handle, CM_MENUPOP, 0, integer(item))[/red]
else
begin
getWindowRect(mWnd, R);
Dc:=getWindowDC(mWnd);
MapWindowPoints(0, mWnd, R, 2);
offsetRect(R, 3, 3);
FrameRect(Dc, R, CreateSolidBrush(clDark));
inflateRect(R, -2, -2);
FrameRect(Dc, R, CreateSolidBrush(clEnd));
end;
end;
procedure TfmMain.SubItemDraw(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; State: TOwnerDrawState);
begin
SubMenuItemDraw(sender, ACanvas, Arect, state);
end;
procedure TfmMain.cmMenuPop(var msg: Tmessage);
var
item: TmenuItem;
begin
item:=TMenuitem(msg.LParam);
if item.Count=0 then
with item do
begin
enabled:=not enabled;
enabled:=not enabled;
end;
end;