How to Draw a Label in ListView client area - delphi

When there is no data to show in ListView, it is better to show message as "no data is changed now", which should be drawed in a TLabel.
How to draw a TLabel in the ListView client area?

You don't need a TLabel for that.
On Vista and later, you can subclass the ListView to handle the LVN_GETEMPTYMARKUP notification.
uses
..., CommCtrl;
private
PrevWndProc: TWndMethod;
procedure TMyForm.FormCreate(Sender: TObject);
begin
PrevWndProc := ListView.WndProc;
ListView.WndProc := ListViewWndProc;
end;
procedure TMyForm.ListViewWndProc(var Message: TMessage);
begin
if Message.Msg = CN_NOTIFY then
begin
if TWMNotifyLV(Message).NMHdr.code = LVN_GETEMPTYMARKUP then
begin
with PNMLVEmptyMarkup(TWMNotifyLV(Message).NMHdr)^ do
begin
dwFlags := EMF_CENTERED;
StrLCopy(szMarkup, 'My message here', L_MAX_URL_LENGTH);
end;
Message.Result := 1;
Exit;
end;
end;
PrevWndProc(Message);
end;
The downside is you have little control over where the text appears (only whether it is left-justified or centered) or how it is formatted, and you cannot change the text dynamically (you would have to add at least one item and then remove it to trigger a new notification).
On earlier versions, or if you need more control over the text position/format/behavior, you can use the ListView's OnCustomDraw event instead. You can draw whatever you want on the ListView's Canvas within the area designated by a TRect provided by the event. If needed, you can use ListView_GetHeader() to determine if the ListView's column headers are visible, and if so then use GetWindowRect() to get the header's height so you can subtract it from the top of the TRect before drawing.
For example:
procedure TMyForm.ListViewCustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean);
var
R, Temp: TRect;
Wnd: HWND;
begin
if Sender.Items.Count <= 0 then
begin
R := ARect;
Wnd := ListView_GetHeader(Sender.Handle);
if Wnd <> 0 then
begin
GetWindowRect(Wnd, #Temp);
R.Top := R.Top + (Temp.Bottom-Temp.Top);
end;
R.Top := R.Top + 10;
DrawText(Sender.Canvas.Handle, 'My message here', -1, #R, DT_CENTER or DT_SINGLELINE);
end;
end;

Related

Color Listbox.Item[N] where N is generated by code

I have a Listbox. I populate it with a file using this:
IF Opendialog1.Execute then
BEGIN
Listbox1.Items.LoadfromFile(OpenDialog1.FileName);
END;
The file loaded contains numbers, and numbers only (I assume).
To be 100 pct. sure, I now starts a scan: (pseudocode :)
for N := 0 til Listbox1.Items.Count -1 DO
BEGIN
NUM := ScanForNotNumberInListbox1Item(Listbox1.Items[N]);
//
// returns NUM = -1 if non digit is met..
//
IF NUM <> 0 then
begin
LISTBOX1.Items[N].BackGroundColor := RED;
Exit; (* or terminate *)
END;
END;
I know I have to use LIstbox1.DrawItem (); and have tried several af the examples shown here in Stack Exchange, but none of the used examples seems to be code-generated.
So how Can I do that ?
Kris
Introduction
You can store additional information about each list item in its associated "object". This can be a (pointer to a) real object, or you can use this pointer-sized integer to encode any simple information you want.
As a simple example, let's put the item's background colour in this field (uses Math):
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.BeginUpdate;
try
ListBox1.Clear;
for i := 1 to 100 do
ListBox1.Items.AddObject(i.ToString, TObject(IfThen(Odd(i), clSkyBlue, clMoneyGreen)));
finally
ListBox1.Items.EndUpdate;
end;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
ListBox: TListBox;
Canvas: TCanvas;
S: string;
begin
ListBox := Control as TListBox;
Canvas := ListBox.Canvas;
Canvas.Brush.Color := TColor(ListBox.Items.Objects[Index]);
Canvas.FillRect(Rect);
S := ListBox.Items[Index];
Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter]);
end;
Don't forget to set the list box's Style property to lbOwnerDrawFixed (say).
A more "advanced" approach would be to associate an actual object with each item:
type
TItemFormat = class
BackgroundColor: TColor;
TextColor: TColor;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
ItemFormat: TItemFormat;
begin
ListBox1.Items.BeginUpdate;
try
ListBox1.Clear;
for i := 1 to 100 do
begin
ItemFormat := TItemFormat.Create;
ItemFormat.BackgroundColor := IfThen(Odd(i), clSkyBlue, clMoneyGreen);
ItemFormat.TextColor := IfThen(Odd(i), clNavy, clGreen);
ListBox1.Items.AddObject(i.ToString, ItemFormat);
end;
finally
ListBox1.Items.EndUpdate;
end;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
ListBox: TListBox;
Canvas: TCanvas;
ItemFormat: TItemFormat;
S: string;
begin
ListBox := Control as TListBox;
Canvas := ListBox.Canvas;
ItemFormat := ListBox.Items.Objects[Index] as TItemFormat;
Canvas.Brush.Color := ItemFormat.BackgroundColor;
Canvas.FillRect(Rect);
S := ListBox.Items[Index];
Canvas.Font.Color := ItemFormat.TextColor;
Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter]);
end;
(In this case, you own the objects, so you are responsible for freeing them when they are no longer needed.)
Putting everything in action
In your particular case, I'd try something like
procedure TForm1.Button1Click(Sender: TObject);
var
i, dummy, FirstInvalidIndex: Integer;
begin
with TOpenDialog.Create(Self) do
try
Filter := 'Text files (*.txt)|*.txt';
Options := [ofPathMustExist, ofFileMustExist];
if Execute then
ListBox1.Items.LoadFromFile(FileName);
finally
Free;
end;
FirstInvalidIndex := -1;
ListBox1.Items.BeginUpdate;
try
for i := 0 to ListBox1.Count - 1 do
if not TryStrToInt(ListBox1.Items[i], dummy) then
begin
ListBox1.Items.Objects[i] := TObject(1);
if FirstInvalidIndex = -1 then
FirstInvalidIndex := i;
end;
finally
ListBox1.Items.EndUpdate;
end;
if FirstInvalidIndex <> -1 then
begin
ListBox1.ItemIndex := FirstInvalidIndex;
MessageBox(Handle, 'An invalid row was found.', PChar(Caption), MB_ICONERROR);
end;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
ListBox: TListBox;
Canvas: TCanvas;
S: string;
begin
ListBox := Control as TListBox;
Canvas := ListBox.Canvas;
Canvas.Font.Assign(ListBox.Font);
if odSelected in State then
begin
Canvas.Brush.Color := clHighlight;
Canvas.Font.Color := clHighlightText;
end
else
begin
Canvas.Brush.Color := clWindow;
Canvas.Font.Color := clWindowText;
end;
if ListBox.Items.Objects[Index] = TObject(1) then
begin
Canvas.Font.Color := clRed;
Canvas.Font.Style := [fsBold, fsStrikeOut]
end;
Canvas.FillRect(Rect);
S := ListBox.Items[Index];
Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter]);
end;
The fine print: Notice that the above snippets are only simple examples intended to demonstrate the basic approach. In a real application, you need to be more careful about the details. For instance, you cannot use a hard-coded red text colour if the background colour is a system colour (because that colour might very well be red too!).
In addition, what happens if the text file is empty (try it!)?
Set lbOwnerDrawFixed (or another ownerdraw) style for Listbox
Listbox items have auxiliary property Objects[] and you can set Objects[i] to non-nil value for invalid items
IF NUM <> 0 then
LISTBOX1.Objects[N] := TObject(1);
Use some example for OnDrawItem event treatment and use Objects[] to define background color during drawing

Add DBLookupCombobox to Delphi DBGrid

I'd like to add DBLookupComboboxes to certain columns in a DBGrid. There is a nice article on About.com on how to do this here. The problem is that with a table having many columns, if you select from the DBLookupCombobox in one column and then try to scroll left, the combobox will move left too as shown in the included images. How can the About.com code can be changed to prevent this behavior? A web search showed two others complaining of the exact same problem with no solution. Note that I want to use a DBLookupCombobox to show a name but enter the id, so using a simple picklist will not do.
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBLookupComboBox1.DataField then
DBLookupComboBox1.Visible := False
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBLookupComboBox1.DataField) then
with DBLookupComboBox1 do
begin
Left := Rect.Left + DBGrid1.Left + 2;
Top := Rect.Top + DBGrid1.Top + 2;
Width := Rect.Right - Rect.Left;
Width := Rect.Right - Rect.Left;
Height := Rect.Bottom - Rect.Top;
Visible := True;
end;
end
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key = Chr(9)) then Exit;
if (DBGrid1.SelectedField.FieldName = DBLookupComboBox1.DataField) then
begin
DBLookupComboBox1.SetFocus;
SendMessage(DBLookupComboBox1.Handle, WM_Char, word(Key), 0);
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with DBLookupComboBox1 do
begin
DataSource := DataSource1; // -> AdoTable1 -> DBGrid1
ListSource := DataSource2;
DataField := 'resource_id'; // from AdoTable1 - displayed in the DBGrid
KeyField := 'id';
ListField := 'resource_name; id';
Visible := False;
end;
DataSource2.DataSet := AdoQuery1;
AdoQuery1.Connection := AdoConnection1;
AdoQuery1.SQL.Text := 'SELECT id,resource_name FROM resources';
AdoQuery1.Open;
end;
Here is one solution using a neat hack from François.
type
// Hack to redeclare your TDBGrid here without the the form designer going mad
TDBGrid = class(DBGrids.TDBGrid)
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
end;
TForm1 = class(TForm)
[...]
procedure TDBGrid.WMHScroll(var Msg: TWMHScroll);
begin
if Form1.DBGrid1.SelectedField.FieldName = Form1.DBLookupComboBox1.DataField then begin
case Msg.ScrollCode of
SB_LEFT,SB_LINELEFT,SB_PAGELEFT: begin
Form1.DBGrid1.SelectedIndex := Form1.DBGrid1.SelectedIndex-1;
Form1.DBLookupComboBox1.Visible := False;
end;
SB_RIGHT,SB_LINERIGHT,SB_PAGERIGHT: begin
Form1.DBGrid1.SelectedIndex := Form1.DBGrid1.SelectedIndex+1;
Form1.DBLookupComboBox1.Visible := False;
end;
end;
end;
inherited; // to keep the expected behavior
end;

Delphi XE7: TEdit TextHint Color

i would like to change to gray color of the Texthint of my TEdits.
I allready found this https://stackoverflow.com/a/31550017/1862576 and tried to change to color via SendMessage like this
procedure TEdit.DoSetTextHint(const Value: string);
var
Font: TFont;
begin
if CheckWin32Version(5, 1) and StyleServices.Enabled and HandleAllocated then
begin
Font := TFont.Create;
try
Font.Assign(self.Font);
Font.Color := clGreen;
Font.Size := 20;
SendTextMessage(Handle, EM_SETCUEBANNER, WPARAM(1), Value);
SendMessage(Handle, WM_SETFONT, Integer(Font.Handle), Integer(True));
finally
// Font.Free;
end;
end;
end;
It changes the size of the font but not the color.
Thanks for your help.
The cue banner is a feature built in to the underlying Win32 EDIT control that TEdit wraps. It is not managed by the VCL at all. There is no Win32 API exposed to manage the coloring of the cue banner text. If you want custom coloring, you will have to stop using the native cue banner functionality and custom-draw the edit control manually by handling its WM_ERASEBKGND and/or WM_PAINT messages directly (see How do i custom draw of TEdit control text?). Otherwise, you will have to find a third-party Edit control that supports custom coloring. Or use TRichEdit instead of TEdit so you can set text colors as needed.
Definition:
Type
HitColor = class helper for tEdit
private
procedure SetTextHintColor(const Value: TColor);
function GetTextHintColor: TColor;
procedure fixWndProc(var Message: TMessage);
published
property TextHintColor : TColor read GetTextHintColor write SetTextHintColor;
end;
Implementation:
procedure HitColor.fixWndProc(var Message: TMessage);
var
dc : HDC ;
r : TRect ;
OldFont: HFONT;
OldTextColor: TColorRef;
Handled : boolean;
begin
Handled := false;
if (Message.Msg = WM_PAINT) and (Text = '') and not Focused then
begin
self.WndProc(Message);
self.Perform(EM_GETRECT, 0, LPARAM(#R));
dc := GetDC(handle);
try
OldFont := SelectObject(dc, Font.Handle );
OldTextColor := SetTextColor(DC, ColorToRGB(GetTextHintColor));
FillRect(dc,r,0);
DrawText(DC, PChar(TextHint), Length(TextHint), R, DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX);
finally
SetTextColor(DC, OldTextColor);
SelectObject(DC, OldFont);
ReleaseDC(handle,dc);
end;
Handled := true;
end;
if not Handled then WndProc(Message);
end;
function HitColor.GetTextHintColor: TColor;
begin
result := tag;
end;
procedure HitColor.SetTextHintColor(const Value: TColor);
begin
tag := Value;
WindowProc := fixWndProc ;
end;
Usage:
edit1.TextHintColor := clred;

Wrong hint showing on TListView with OwnerData and OwnerDraw set to True

I use Delphi 2007. I have a TListView with OwnerData and OwnerDraw set to True. ViewStyle is set to vsReport.
I have a record.
type TAList=record
Item:Integer;
SubItem1:String;
SubItem2:String;
end;
var
ModuleData: array of TAList;
procedure TForm1.ListView3Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := IntToStr(ModuleData[Item.Index].Item);
Item.SubItems.Add(ModuleData[Item.Index].SubItem1);
Item.SubItems.Add(ModuleData[Item.Index].SubItem2);
end;
procedure TForm1.ListView3DrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
LIndex : integer;
LRect: TRect;
LText: string;
TTListView: TListView;
begin
TTListView := TListView(Sender);
if (Item.SubItems[0] = '...') then
begin
TTListView.Canvas.Brush.Color := clHighlight;
TTListView.Canvas.Font.Color := clHighlightText;
end else
begin
TTListView.Canvas.Brush.Color := TTListView.Color;
TTListView.Canvas.Font.Color := TTListView.Font.Color;
end;
for LIndex := 0 to TTListView.Columns.Count - 1 do
begin
if (not(ListView_GetSubItemRect(TTListView.Handle, Item.Index, LIndex, LVIR_BOUNDS, #LRect))) then Continue;
TTListView.Canvas.FillRect(LRect);
if (LIndex = 0) then LText := Item.Caption else LText := Item.SubItems[LIndex - 1];
LRect.Left := LRect.Left + 6;
DrawText(TTListView.Canvas.Handle, PChar(LText), Length(LText), LRect, DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX or DT_END_ELLIPSIS);
end;
end;
I wish to show an hint when SubItem2 is truncated. On Windows XP, no hint is shown at all. On Windows Vista & Windows 7, when my mouse is over an item, it shows an hint that is totally off.
I have no special code to handle hints. Should there be one in OwnerData and OwnerDraw modes?
Here are images of what I get:
(source: noelshack.com)
(source: noelshack.com)
EDIT:
David asked why OwnerDraw was set to True. There are two reasons:
This way, I can "disallow" user selection.
If I set OwnerDraw to False, I get another problem. See Why do I get white column separators on my custom-drawn listview?
EDIT 2:
If I handle the OnInfoTip event as suggested by TLama, I get an unthemed balloon hint and the wrong hint from Windows Vista & 7.
1. Environment
Behavior described here I've experienced and tested only on Windows 7 SP1 64-bit Home Premium with most recent updates installed with application built in Delphi 2009 also with latest updates applied. In no other system I've tried this.
2. About the problem
Default item hints that you can see on your screenshot doesn't come from VCL. In certain circumstances whose you just hit, are those hints shown by the system in a wrong, probably somehow cached way. The text of the last item you hovered is shown as a hint for the item you're just hovering. Here is the property configuration (just the important part; the rest I kept in default component values):
ListView1.ShowHint := False;
ListView1.OwnerData := True;
ListView1.OwnerDraw := True;
ListView1.ViewStyle := vsReport;
The following events are handled:
OnData
OnDrawItem
Actually, you don't even need to handle the OnDrawItem to simulate the problem. The hints are shown by the texts given to the items in the OnData event. I'm not able to trace it more deeper, since it seems there's no notification handler (nor even system notification) that might be related to the hints you see in the VCL, which is the reason why I'm suspecting the system.
3. The way to solution
Nothing what I've tried didn't fix the problem keeping your current property configuration. Here's a list of what I've tried:
3.1. Remove the LVS_EX_LABELTIP style ?
As a hot favorite and actually the first what I've checked was excluding the LVS_EX_LABELTIP from the list view's style in a hope the item hint showing will stop and you'll be able to implement your own custom hints through the OnInfoTip event. The problem is, that this style is not implemented anywhere in the list view control, thus it's not included in the list view style.
3.2. Disable the OwnerDraw property ?
Setting the OwnerDraw property to False actually resolves the issue (hints are then shown with correct item texts by the actual hovered item), but you've said you need to use owner drawing, so it's also not a solution for you.
3.3. Remove the LVS_EX_INFOTIP style ?
Removing the LVS_EX_INFOTIP style from the list view's style finally stopped showing of the item hints by the system, but also caused that the control stopped to send to the parent the tooltip notifications. As a consequence of this is the OnInfoTip event with its functionality cutted off. In this case you need to implement the hint handling completely by yourself. And that's what I've tried in the following code.
4. Workaround
I've decided to disable all the system hints of a list view by excluding of the LVS_EX_INFOTIP style and implementing own tooltip handling. So far I know at least about the following problems:
when you use a regular Hint property and hover from an item with shortened caption to the empty area of a list view, the Hint is shown, but it doesn't hide unless you exit the control client rectangle or the hint show time interval elapses (even if you hover an item with shortened caption again). The problem is that I don't know how to specify the CursorRect for the THintInfo structure, so that you cover the whole client rectangle except items area rectangle.
you must use the same item rectangle extents as you use in your owner drawing event method since the system doesn't know, where you're rendering the text of your items. So, another disadvantage is to keep this in sync.
Here is the code of the main unit from a demo project, which you can download from here if you want:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, CommCtrl, StdCtrls;
type
TRecord = record
Item: Integer;
SubItem1: string;
SubItem2: string;
end;
type
TListView = class(ComCtrls.TListView)
private
procedure CMHintShow(var AMessage: TCMHintShow); message CM_HINTSHOW;
end;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
procedure ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
Rect: TRect; State: TOwnerDrawState);
procedure ListView1Data(Sender: TObject; Item: TListItem);
private
ModuleData: array of TRecord;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
ListColumn: TListColumn;
begin
SetLength(ModuleData, 3);
ModuleData[0].Item := 0;
ModuleData[0].SubItem1 := '[0;0] Subitem caption';
ModuleData[0].SubItem2 := '[1;0] Subitem caption';
ModuleData[1].Item := 1;
ModuleData[1].SubItem1 := '[0;1] Subitem caption';
ModuleData[1].SubItem2 := '[1;1] Subitem caption';
ModuleData[2].Item := 2;
ModuleData[2].SubItem1 := '[0;2] This is a long subitem caption';
ModuleData[2].SubItem2 := '[0;2] This is even longer subitem caption';
ListView1.OwnerData := True;
ListView1.OwnerDraw := True;
ListView1.ViewStyle := vsReport;
ListView_SetExtendedListViewStyle(
ListView1.Handle,
ListView_GetExtendedListViewStyle(ListView1.Handle) and not LVS_EX_INFOTIP);
ListColumn := ListView1.Columns.Add;
ListColumn.Caption := 'Col. 1';
ListColumn.Width := 50;
ListColumn := ListView1.Columns.Add;
ListColumn.Caption := 'Col. 2';
ListColumn.Width := 50;
ListColumn := ListView1.Columns.Add;
ListColumn.Caption := 'Col. 3';
ListColumn.Width := 50;
ListView1.Items.Add;
ListView1.Items.Add;
ListView1.Items.Add;
end;
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := IntToStr(ModuleData[Item.Index].Item);
Item.SubItems.Add(ModuleData[Item.Index].SubItem1);
Item.SubItems.Add(ModuleData[Item.Index].SubItem2);
end;
procedure TForm1.ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
Rect: TRect; State: TOwnerDrawState);
var
R: TRect;
S: string;
SubItem: Integer;
ListView: TListView;
begin
ListView := TListView(Sender);
if (Item.SubItems[0] = '...') then
begin
ListView.Canvas.Brush.Color := clHighlight;
ListView.Canvas.Font.Color := clHighlightText;
end
else
begin
ListView.Canvas.Brush.Color := ListView.Color;
ListView.Canvas.Font.Color := ListView.Font.Color;
end;
for SubItem := 0 to ListView.Columns.Count - 1 do
begin
if ListView_GetSubItemRect(ListView.Handle, Item.Index, SubItem,
LVIR_LABEL, #R) then
begin
ListView.Canvas.FillRect(R);
if (SubItem = 0) then
S := Item.Caption
else
begin
R.Left := R.Left + 6;
S := Item.SubItems[SubItem - 1];
end;
DrawText(ListView.Canvas.Handle, PChar(S), Length(S), R, DT_SINGLELINE or
DT_VCENTER or DT_NOPREFIX or DT_END_ELLIPSIS);
end;
end;
end;
{ TListView }
procedure TListView.CMHintShow(var AMessage: TCMHintShow);
var
R: TRect;
S: string;
Item: Integer;
SubItem: Integer;
HitTestInfo: TLVHitTestInfo;
begin
with AMessage do
begin
HitTestInfo.pt := Point(HintInfo.CursorPos.X, HintInfo.CursorPos.Y);
if ListView_SubItemHitTest(Handle, #HitTestInfo) <> -1 then
begin
Item := HitTestInfo.iItem;
SubItem := HitTestInfo.iSubItem;
if (Item <> -1) and (SubItem <> -1) and
ListView_GetSubItemRect(Handle, Item, SubItem, LVIR_LABEL, #R) then
begin
if (SubItem = 0) then
S := Items[Item].Caption
else
begin
R.Left := R.Left + 6;
S := Items[Item].SubItems[SubItem - 1];
end;
if ListView_GetStringWidth(Handle, PChar(S)) > R.Right - R.Left then
begin
MapWindowPoints(Handle, 0, R.TopLeft, 1);
MapWindowPoints(Handle, 0, R.BottomRight, 1);
HintInfo^.CursorRect := R;
HintInfo^.HintPos.X := R.Left;
HintInfo^.HintPos.Y := R.Top;
HintInfo^.HintMaxWidth := ClientWidth;
HintInfo^.HintStr := S;
AMessage.Result := 0;
end
else
AMessage.Result := 1;
end
else
AMessage.Result := 1;
end
else
inherited;
end;
end;
end.

add 2-line caption in a TListView?

in a label i can add a new line like this
Label.Caption:='First line'+#13#10+'SecondLine';
can this be done in a TListView?
listItem:=listView.Items.Add;
listItem.Caption:='First line'+#13#10+'SecondLine';
thanks
It is possible to have multiline strings in a standard TListView in vsReport style, but AFAIK it doesn't support varying row heights. However, if you have all rows with the same number of lines > 1 you can achieve that quite easily.
You need to set the list view to OwnerDraw mode, first so you can actually draw the multiline captions, and second to get a chance to increase the row height to the necessary value. This is done by handling the WM_MEASUREITEM message, which is sent only for owner-drawn list views.
A small example to demonstrate this:
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
procedure ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
Rect: TRect; State: TOwnerDrawState);
private
procedure WMMeasureItem(var AMsg: TWMMeasureItem); message WM_MEASUREITEM;
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.ViewStyle := vsReport;
ListView1.OwnerDraw := True;
ListView1.OwnerData := True;
ListView1.Items.Count := 1000;
with ListView1.Columns.Add do begin
Caption := 'Multiline string test';
Width := 400;
end;
ListView1.OnDrawItem := ListView1DrawItem;
end;
procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
Item: TListItem; Rect: TRect; State: TOwnerDrawState);
begin
if odSelected in State then begin
Sender.Canvas.Brush.Color := clHighlight;
Sender.Canvas.Font.Color := clHighlightText;
end;
Sender.Canvas.FillRect(Rect);
InflateRect(Rect, -2, -2);
DrawText(Sender.Canvas.Handle,
PChar(Format('Multiline string for'#13#10'Item %d', [Item.Index])),
-1, Rect, DT_LEFT);
end;
procedure TForm1.WMMeasureItem(var AMsg: TWMMeasureItem);
begin
inherited;
if AMsg.IDCtl = ListView1.Handle then
AMsg.MeasureItemStruct^.itemHeight := 4 + 2 * ListView1.Canvas.TextHeight('Wg');
end;
I know this is an old thread, and I can't take credit for figuring this out, but to adjust the row height in a TListView you can add an image list for the StateImages and then specify the image height by expanding the StateImages item in the properties window. You don't need to load any actual images.
Sorry I can't credit the actual person who figured it out - it was on a forum I visited a while back.
I don't seem to be able to achieve this using the TListView. But using the TMS TAdvListView, you can use HTML in the item text so this will put the caption onto 2 lines:
with AdvListView1.Items.Add do
begin
Caption := '<FONT color="clBlue">Line 1<BR>Line 2</font>';
end;

Resources