whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

14 Visitors Online


 
...prevent resizing of listview columns?
Autor: jiyong jang
[ Print tip ]  

Tip Rating (15):  
     


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;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners