Title: capture a column resize event in a TListView?
{
Question:
HowdoIcaptureacolumnresizeeventinTListView,theOnResize
onlyworkswhentheListViewischanged?
Answer:
Theeventcanbeaddedwithabitofwork.SeethecustomTListviewderivative
below.Ithas3newevents:
OnColumnResize,OnBeginColumnResize,OnEndColumnResize
}
{
Frage:
Wiekannichherausfinden,wennineinerTListVieweineSpaltengre
verndertwird?OnResizewirdja nurdannausgelst,wennsichdieGre
derTListViewndert.
Antwort:
EinOnColumnResizeEreigniskannmiteinwenigArbeiteinerTListView
hinzugefgtwerden.InstallierediePBExListviewKomponente,
welchevonderTListViewabgeleitet ist.
DieKomponentebesitzt3neueEreignisse:
OnColumnResize,OnBeginColumnResize,OnEndColumnResize
}
unitPBExListview;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,
Dialogs,ComCtrls;
type
TLVColumnResizeEvent=procedure(Sender:TCustomListview;
columnindex:Integer;
columnwidth:Integer)ofobject;
TPBExListview=class(TListview)
private
FBeginColumnResizeEvent:TLVColumnResizeEvent;
FEndColumnResizeEvent:TLVColumnResizeEvent;
FColumnResizeEvent:TLVColumnResizeEvent;
protected
procedureDoBeginColumnResize(columnindex,columnwidth:Integer);
virtual;
procedureDoEndColumnResize(columnindex,columnwidth:Integer);
virtual;
procedureDoColumnResize(columnindex,columnwidth:Integer);
virtual;
procedureWMNotify(varMsg:TWMNotify);messageWM_NOTIFY;
functionFindColumnIndex(pHeader:pNMHdr):Integer;
functionFindColumnWidth(pHeader:pNMHdr):Integer;
procedureCreateWnd;override;
published
propertyOnBeginColumnResize:TLVColumnResizeEvent
readFBeginColumnResizeEventwriteFBeginColumnResizeEvent;
propertyOnEndColumnResize:TLVColumnResizeEvent
readFEndColumnResizeEventwriteFEndColumnResizeEvent;
propertyOnColumnResize:TLVColumnResizeEvent
readFColumnResizeEventwriteFColumnResizeEvent;
end;
procedureRegister;
implementation
usesCommCtrl;
procedureRegister;
begin
RegisterComponents('PBGoodies',[TPBExListview]);
end;
procedureTPBExListview.DoBeginColumnResize(columnindex,columnwidth:Integer);
begin
ifAssigned(FBeginColumnResizeEvent)then
FBeginColumnResizeEvent(Self,columnindex,columnwidth);
end;
procedureTPBExListview.DoEndColumnResize(columnindex,columnwidth:Integer);
begin
ifAssigned(FEndColumnResizeEvent)then
FEndColumnResizeEvent(Self,columnindex,columnwidth);
end;
procedureTPBExListview.DoColumnResize(columnindex,columnwidth:Integer);
begin
ifAssigned(FColumnResizeEvent)then
FColumnResizeEvent(Self,columnindex,columnwidth);
end;
functionTPBExListview.FindColumnIndex(pHeader:pNMHdr):Integer;
var
hwndHeader:HWND;
iteminfo:THdItem;
ItemIndex:Integer;
buf:array[0..128]ofChar;
begin
Result:=-1;
hwndHeader:=pHeader^.hwndFrom;
ItemIndex:=pHDNotify(pHeader)^.Item;
FillChar(iteminfo,SizeOf(iteminfo),0);
iteminfo.Mask:=HDI_TEXT;
iteminfo.pszText:=buf;
iteminfo.cchTextMax:=SizeOf(buf)-1;
Header_GetItem(hwndHeader,ItemIndex,iteminfo);
ifCompareStr(Columns[ItemIndex].Caption,iteminfo.pszText)=0then
Result:=ItemIndex
else
begin
forItemIndex:=0toColumns.Count-1do
ifCompareStr(Columns[ItemIndex].Caption,iteminfo.pszText)=0then
begin
Result:=ItemIndex;
Break;
end;
end;
end;
procedureTPBExListview.WMNotify(varMsg:TWMNotify);
begin
inherited;
caseMsg.NMHdr^.codeof
HDN_ENDTRACK:
DoEndColumnResize(FindColumnIndex(Msg.NMHdr),
FindColumnWidth(Msg.NMHdr));
HDN_BEGINTRACK:
DoBeginColumnResize(FindColumnIndex(Msg.NMHdr),
FindColumnWidth(Msg.NMHdr));
HDN_TRACK:
DoColumnResize(FindColumnIndex(Msg.NMHdr),
FindColumnWidth(Msg.NMHdr));
end;
end;
procedureTPBExListview.CreateWnd;
var
wnd:HWND;
begin
inherited;
wnd:=GetWindow(Handle,GW_CHILD);
SetWindowLong(wnd,GWL_STYLE,
GetWindowLong(wnd,GWL_STYLE)andnotHDS_FULLDRAG);
end;
functionTPBExListview.FindColumnWidth(pHeader:pNMHdr):Integer;
begin
Result:=-1;
ifAssigned(PHDNotify(pHeader)^.pItem)and
((PHDNotify(pHeader)^.pItem^.maskandHDI_WIDTH)0)then
Result:=PHDNotify(pHeader)^.pItem^.cxy;
end;
end.