8

这与问题如何在 TListView 标题列中显示复选框有关?.

我想使用这个答案中的代码@Sertac Akyuz。(我也需要这个在 WinXP 中工作)

但我想让标题 CheckBox 不会从 ListView 或其他活动控件中窃取焦点。

一个快速的解决方法是将焦点始终设置到 ListView 中ListHeaderWndProc

...
FListHeaderChk.Checked := not FListHeaderChk.Checked;
ListView1.SetFocus;
// code that checks/clears all items

但这有点丑。因为CheckBox首先聚焦,然后焦点又回到ListView。另外,如果我单击 CheckBox 并将鼠标拖到 CheckBox 外,则无法接收到BN_CLICKED消息。

我也试过:

TCheckBox = class(StdCtrls.TCheckBox)
  private
    procedure WMMouseActivate(var Message: TWMMouseActivate); message WM_MOUSEACTIVATE;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    procedure DefaultHandler(var Message); override;
  end;

procedure TCheckBox.WMMouseActivate(var Message: TWMMouseActivate);
begin
  Message.Result := MA_NOACTIVATE; // no effect!
end;

procedure TCheckBox.CreateParams(var Params: TCreateParams);
const
  WS_EX_NOACTIVATE = $08000000;
begin
  inherited;
  Params.ExStyle := Params.ExStyle or WS_EX_NOACTIVATE; // no effect!
end;

procedure TCheckBox.DefaultHandler(var Message);
begin
  case TMessage(Message).Msg of
    WM_SETFOCUS:
    begin
      if IsWindow(TWMSetFocus(Message).FocusedWnd) then
      begin
        TMessage(Message).Result := 1; // ???
        // inherited // ??? 
        Windows.SetFocus(TWMSetFocus(Message).FocusedWnd);
        Exit;
        // Checkbox fails to receive `BN_CLICKED` message
      end;
    end;
  end;
  inherited;
end;

没有任何效果。我错过了什么?

4

1 回答 1

7

不要处理通知WM_COMMAND消息,否则BN_CLICKED您希望在按下按钮时单击然后在单击后向外拖动的行为将不起作用。单击包括按下和释放控件上的按钮。

相反,您可以查找是否在控件内单击了鼠标,如果是,则切换选中状态。之后您可以吃掉鼠标消息,因此控件不会获得焦点。但这应该在复选框的窗口过程中检查,而不是列表视图。修改后的代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    CheckBox1: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FListHeaderChk: TCheckBox;
    FSaveListHeaderChkWndProc: TWndMethod;
    FListHeaderWnd: HWND;
    procedure ListHeaderChkWndProc(var Msg: TMessage);
  end;

var
  Form1: TForm1;

implementation

uses
  commctrl;

{$R *.dfm}

function GetCheckSize: TPoint;     // from checklst.pas
begin
  with TBitmap.Create do
    try
      Handle := LoadBitmap(0, PChar(OBM_CHECKBOXES));
      Result.X := Width div 4;
      Result.Y := Height div 3;
    finally
      Free;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  CheckSize: TPoint;
  HeaderSize: TRect;
begin
  ListView1.HandleNeeded;
  FListHeaderWnd := ListView_GetHeader(ListView1.Handle);

  FListHeaderChk := TCheckBox.Create(nil);
  CheckSize := GetCheckSize;
  FListHeaderChk.Height := CheckSize.X;
  FListHeaderChk.Width := CheckSize.Y;

  ShowWindow(ListView1.Handle, SW_SHOWNORMAL);
  windows.GetClientRect(FListHeaderWnd, HeaderSize);
  FListHeaderChk.Top := (HeaderSize.Bottom - FListHeaderChk.Height) div 2;
  FListHeaderChk.Left := FListHeaderChk.Top;

  FListHeaderChk.Parent := Self;
  FListHeaderChk.TabStop := False;
  windows.SetParent(FListHeaderChk.Handle, FListHeaderWnd);
  FSaveListHeaderChkWndProc := FListHeaderChk.WindowProc;
  FListHeaderChk.WindowProc := ListHeaderChkWndProc;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FListHeaderChk.Free;
end;

procedure TForm1.ListHeaderChkWndProc(var Msg: TMessage);
begin
  if (Msg.Msg = WM_MOUSEACTIVATE) and (Msg.LParamLo = HTCLIENT) then begin
    Msg.Result := MA_NOACTIVATEANDEAT;
    FListHeaderChk.Checked := not FListHeaderChk.Checked;
    Exit;
  end;

  FSaveListHeaderChkWndProc(Msg);
end;

end.
于 2013-02-04T19:28:49.500 回答