我在程序中用了DrawFocusRect,却看不到他画的线,求教他的颜色,线形等如何设置,
Canvas的Brush,pen等和DrawFocusRect的关系.

解决方案 »

  1.   

    Canvas.DrawFocusRect(rect(0,0,80,60));
    画的是虚线框
    pen是边线颜色,brush是你画的图形中除线以外部分的颜色
    基本上它们直接没关系
     Canvas.Pen.Color:=clred;
     Canvas.Brush.Color:=clblue;//如果这句在下一句前面,你就看不见下一句的效果
      Canvas.DrawFocusRect(rect(0,0,20,20));
    没有仔细看
      

  2.   

    DrawFocusRect works only in MM_TEXT mode. In other modes, this function does not draw the focus rectangle correctly, but it does not return error values.
    What 'MM_TEXT mode' means ?
      

  3.   

    默认状态下就是'MM_TEXT mode' ,这是GDI映射、坐标变换之类的东西跟你的问题没有关系。
    见:
    http://218.108.41.12/vchome/book/chap8_2.htm
      

  4.   

    怎么没什么人回答啊,问题太简单了吗?
    我给个例子大家看看,新建一工程,把下面的代码直接复制到Unit1,运行
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormResize(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      CellHeight,CellWidth:integer;
      oldCol,oldRow:integer;
    implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      Color := ClWindow;  oldCol := 999;
      oldRow := 999;
    end;procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
      Col,Row:integer;
      R: TRect;
    begin
      Col := X div CellWidth;
      Row := Y div CellHeight;
      //Canvas.Font.Color := clred;
      if (Col <> OldCol) or (Row <> OldRow) then
      begin
        R:= Bounds(CellWidth * oldcol,CellHeight * oldrow,CellWidth,CellHeight);
        Canvas.DrawFocusRect(R);
        R:= Bounds(CellWidth * col,CellHeight * row,CellWidth,CellHeight);
        Canvas.TextOut(r.Left,r.Top,'33');
        Canvas.DrawFocusRect(R);
        oldrow := row;
        oldCol := col;
      end;
    end;procedure TForm1.FormResize(Sender: TObject);
    begin
      CellWidth := ClientRect.Right div 10;
      CellHeight := ClientRect.Bottom div 10;
    end;end.当鼠标在窗体上移动时,就会有个虚框跟着移动,还写上了  '33'
    好了,现在把注释掉的那句//Canvas.Font.Color := clred;恢复,让他画红字;
    现在你在看看结果!!
    我的系统是windows2000 professional简体中文版,Delphi 7
      

  5.   

    Canvas.DrawFocusRect这个方法改变了画笔模式,用的XOR.
    在黑和白下看不出效果,如果指定为别的颜色就不对了.
      

  6.   

    to : fengyvn(我喜欢的人都不喜欢我) 这个我知道,但他画完会自己改回来的.不知道这是不是Delphi的一个BUG,因为用API是正常的
    把 Canvas.DrawFocusRect(R); 替换为 DrawFocusRect(canvas.handle,R); 就正常了.
      

  7.   

    看了VCL的源码,是这样的,也只是简单的调用了API;procedure TCanvas.Changing;
    begin
      if Assigned(FOnChanging) then FOnChanging(Self);
    end;procedure TCanvas.Changed;
    begin
      if Assigned(FOnChange) then FOnChange(Self);
    end;procedure TCanvas.DrawFocusRect(const Rect: TRect);
    begin
      Changing;
      RequiredState([csHandleValid, csBrushValid]);
      Windows.DrawFocusRect(FHandle, Rect);
      Changed;
    end;
    //**************************************
    看来问题出在这里!
      procedure TCanvas.RequiredState(ReqState: TCanvasState);
    var
      NeededState: TCanvasState;
    begin
      NeededState := ReqState - State;
      if NeededState <> [] then
      begin
        if csHandleValid in NeededState then
        begin
          CreateHandle;
          if FHandle = 0 then
            raise EInvalidOperation.CreateRes(@SNoCanvasHandle);
        end;
        if csFontValid in NeededState then CreateFont;
        if csPenValid in NeededState then CreatePen;
        if csBrushValid in NeededState then CreateBrush;
        State := State + NeededState;
      end;
    end;
    //**********************************************************************