form中有一个panel,panel上有5个checkbox,我想要的效果是:用鼠标拖动一个范围,这个范围内的checkbox的checkbox.font.color:=clred;
就像在window的文件夹中用鼠标拖动一个范围,这个范围内的文件夹和文件就被选中一个道理。

解决方案 »

  1.   

    有好几个人问过这个问题,后来都没有完美的解决办法,是否可以用ListView来代替?
      

  2.   

    Panel处理消息比较迟钝吧,我直接在Form上试了试基本满足,楼主你可以参考一下,注释掉的部分你参考http://topic.csdn.net/u/20090701/14/bead8779-a5a7-4a43-9417-cf3498c8c3df.html把这个判读加上试试。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        CheckBox1: TCheckBox;
        CheckBox2: TCheckBox;
        CheckBox3: TCheckBox;
        RadioButton1: TRadioButton;
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      fDrag: Boolean;
      fRect: TRect;
      implementation{$R *.dfm}procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft  then
      begin
        fDrag := True;
        SetCapture(Handle);
        fRect.Left := X;
        fRect.Top := Y;
        fRect.BottomRight := fRect.TopLeft;
        Canvas.DrawFocusRect(fRect);
      end;
    end;procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
      i: Integer;
    begin
      if  fDrag  then
      begin
        Canvas.DrawFocusRect(fRect);
        fRect.Right := X;
        fRect.Bottom := Y;
        Canvas.DrawFocusRect(fRect);
        for i := 0 to self.ControlCount - 1 do
        begin
          if self.Controls[i] is TCheckBox then
            // if CheckBox本身的矩形范围 与 鼠标拖动范围重叠 then
            (self.Controls[i] as TCheckBox).Font.Color := clRed;
        end;
      end;
    end;procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if fDrag then
      begin
        ReleaseCapture;
        fDrag := False;
        Invalidate;
      end;
    end;end.
      

  3.   

    kampan,试了你的代码,好像没有任何效果
      

  4.   

     // if CheckBox本身的矩形范围 与 鼠标拖动范围重叠 then
    这里要写个判断,判断控件的位置是否和fRect重合
      

  5.   

    补充一下,有个API可以判断。
    BOOL IntersectRect(
      LPRECT lprcDst,        // intersection buffer
      CONST RECT *lprcSrc1,  // first rectangle
      CONST RECT *lprcSrc2   // second rectangle
    );Return Values
    If the rectangles intersect, the return value is nonzero.
    If the rectangles do not intersect, the return value is zero. 
      

  6.   


    我自己试过的(直接在Form而非Panel)是可以的,CheckBox字体都变红。
      

  7.   

    真是奇怪,我跟踪,看到(self.Controls[i] as TCheckBox).Font.Color := clRed;
    这句代码是有执行的,但就是不变红奇怪了。。
      

  8.   


    你是在哪里执行的代码?self是什么?
      

  9.   


    刚才那个不变红是我自己的问题。已解决了。。
    现在是要解决   判断控件的位置是否和fRect重合的问题了。还有没有高手能赐教的?
      

  10.   

    kampan,你那个只能从上到下,从左至右地拉那个虚线框。。