禁止改变ListView的列宽
prevent resizing of listview columns?
Author: jiyong jang
unit TestUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormShow(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
WndMethod: TWndMethod;
function GetIndex(aNMHdr: pNMHdr): Integer;
procedure CheckMesg(var aMesg: TMessage);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
CommCtrl;
{$R *.dfm}
function TForm1.GetIndex(aNMHdr: pNMHdr): Integer;
var
hHWND: HWND;
HdItem: THdItem;
iIndex: Integer;
iResult: Integer;
iLoop: Integer;
sCaption: string;
sText: string;
Buf: array [0..128] of Char;
begin
Result := -1;
hHWND := aNMHdr^.hwndFrom;
iIndex := pHDNotify(aNMHdr)^.Item;
FillChar(HdItem, SizeOf(HdItem), 0);
with HdItem do
begin
pszText := Buf;
cchTextMax := SizeOf(Buf) - 1;
Mask := HDI_TEXT;
end;
Header_GetItem(hHWND, iIndex, HdItem);
with ListView1 do
begin
sCaption := Columns[iIndex].Caption;
sText := HdItem.pszText;
iResult := CompareStr(sCaption, sText);
if iResult = 0 then
Result := iIndex
else
begin
iLoop := Columns.Count - 1;
for iIndex := 0 to iLoop do
begin
iResult := CompareStr(sCaption, sText);
if iResult <> 0 then
Continue;
Result := iIndex;
break;
end;
end;
end;
end;
procedure TForm1.CheckMesg(var aMesg: TMessage);
var
HDNotify: ^THDNotify;
NMHdr: pNMHdr;
iCode: Integer;
iIndex: Integer;
begin
case aMesg.Msg of
WM_NOTIFY:
begin
HDNotify := Pointer(aMesg.lParam);
iCode := HDNotify.Hdr.code;
if (iCode = HDN_BEGINTRACKW) or
(iCode = HDN_BEGINTRACKA) then
begin
NMHdr := TWMNotify(aMesg).NMHdr;
// chekck column index
iIndex := GetIndex(NMHdr);
// prevent resizing of columns if index's less than 3
if iIndex < 3 then
aMesg.Result := 1;
end
else
WndMethod(aMesg);
end;
else
WndMethod(aMesg);
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
WndMethod := ListView1.WindowProc;
ListView1.WindowProc := CheckMesg;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
ListView1.WindowProc := WndMethod;
end;