procedure TForm1.FormShow(Sender: TObject);
var
    mnitem, submnitem: TMenuItem;
begin
     MainMenu1.ownerDraw := True;
     mnitem := TmenuItem.Create(Form1);
     mnitem.Caption := '业务(&M)';
     mnitem.OnDrawItem := DrawMenuItem;
    
     MainMenu1.Items.Add(mnitem);     submnitem := TmenuItem.Create(Form1);
     submnitem.Caption := 'ddddd';
     mnitem.Add(submnitem);  
end;procedure TForm1.DrawMenuItem(Sender: TObject; ACanvas: TCanvas;
  ARect: TRect; Selected: Boolean);
var
  aPaintArear: TRect;
  aTextHeight: integer;
  aText: string;
begin
  aText := TMenuItem(Sender).Caption;  aTextHeight := ACanvas.TextHeight(aText);  if MainMenu1.Items.Items[MainMenu1.Items.Count - 1] = TMenuItem(Sender) then
    aPaintArear := Rect(ARect.Left,ARect.Top,Self.Width - 4, ARect.Bottom+1);  ACanvas.Brush.Style := bsClear;
  ACanvas.Brush.Color := clSkyBlue;
  ACanvas.FillRect(aPaintArear);
  ACanvas.Font.Color := clBlack;
  ACanvas.TextOut(ARect.Left + 3,ARect.Top + ( ARect.Bottom - ARect.Top  - aTextHeight ) div 2,aText);
end;1. 我自己重绘菜单,但是 "业务(&M)"  里面的快捷键也给重绘成字符了,要怎么才能保留快捷键呢?
2. aPaintArear := Rect(ARect.Left,ARect.Top,Self.Width - 4, ARect.Bottom+1)  这一句里面,最后一个参数,是ARect.Bottom的时候,没有问题,但是ARect.Bottom+1 就不能自动刷新,必须要把鼠标放到菜单上,才能刷新出 +1 的那一行,请问是怎么回事啊?

解决方案 »

  1.   

    1. 试试下面这个?Result := DrawState(
        Canvas.Handle,            // device context to draw on
        0,                        // brush (not required)
        nil,                      // output function (not required)
        Integer(PChar(Text)),     // text to be rendered
        Length(Text),             // length of text to be rendered
        X, Y,                     // co-ords of top left of text
        0, 0,                     // size of text (not required)
        DST_PREFIXTEXT            // text type and state
      );2. 建议:aPaintArear := Rect(ARect.Left, ARect.Top, ARect.Right - 4, ARect.Bottom)
    顺便说说,你在事件中使用的Self.Width,引用的实际是Form的宽度……