![]() |
Delphi-Version: 2010
TListView.OnDrawItem
I wanted make list with recent files. I also wanted make grey these items, that match file not exists (click File-->Reopen-->Properties in Delphi to see what I want to do). This is what I have:
Delphi-Quellcode:
I want just grey text if file not exists. Any help with this? :(
begin
with lvRecentFiles, lvRecentFiles.Canvas do begin if FileExists(Item.Caption) then Pen.Color := clBlack else Pen.Color := clGrayText ; TextOut(Rect.Left + 2, Rect.Top + 2, Item.Caption); end; end; |
AW: TListView.OnDrawItem
Delphi-Quellcode:
if FileExists(Item.Caption) then
Font.Color := clBlack else Font.Color := clGrayText; |
Re: TListView.OnDrawItem
Oh, why I didn't think about this :D
Delphi-Quellcode:
There is grey, but can't be selected as when OwnerDraw is False (in system style). In Delphi can be. Any idea?
begin
with lvRecentFiles, lvRecentFiles.Canvas do begin if FileExists(Item.Caption) then Font.Color := clBlack else Font.Color := clGrayText ; //TextOut(Rect.Left + 2, Rect.Top + 2, Item.Caption); R := Classes.Rect(Rect.Left + 6, Rect.Top + 3, Rect.Right, Rect.Bottom - 2); S := Item.Caption; TextRect(R, S, [tfEndEllipsis]); end; end; |
AW: TListView.OnDrawItem
You have to evaluate the state
|
AW: TListView.OnDrawItem
The item gets selected, but you don't see it because it is your responsiblity to give the user visual feedback if you draw the items yourself.
|
Re: TListView.OnDrawItem
So, how I can draw it in current Windows's style?
|
AW: TListView.OnDrawItem
Markus gave you the answer.
|
Re: TListView.OnDrawItem
Yes, but how about in current system style. For example in XP there is solid color, but on Vista+ there is gradiented. I want to just grey text, all other should be "original". Are you understand what I mean ;)
|
AW: TListView.OnDrawItem
Call the standard Draw-Routine after you set the new Color
|
Re: TListView.OnDrawItem
What you mean? I'm not pro in owner drawing :( Could you give me an example?
|
AW: TListView.OnDrawItem
Have a look on
![]() |
Re: TListView.OnDrawItem
I tried OnCustomDraw and OnCustomDrawItem and set in both DefaultDraw to True. And I still don't have default selection. How to use it?
|
AW: TListView.OnDrawItem
TListview doesn't support the themed selection rectangle out of the box. You have to activate it manually by calling
Delphi-Quellcode:
SetWindowTheme(Listview.Handle, 'Explorer', nil);
|
Re: TListView.OnDrawItem
I added it to OnCreate, this is good procedere?
Still no effect :( OnDrawItem should be in OnCustomDrawItem? But this don't have Rect parameter. Don't have idea how to do it :( |
AW: TListView.OnDrawItem
Does it work if you don't use any owner/custom drawing code?
Btw: As you're already using
Delphi-Quellcode:
, why not use
clGrayText
Delphi-Quellcode:
for regular text as well? Depending on the settings, text may not always be black by default. In general, you should avoid combining environment colors with hard coded colors, because it can get ugly (e.g. black text on a black background) if someone uses different settings.
clWindowText
|
Re: TListView.OnDrawItem
If I don't use owner draw (in owner draw event I put my drawing code) items selection is as in all other listview components (system too). When enable owner drawing I don't have selection, though in custom draw event default draw is on.
|
AW: TListView.OnDrawItem
You could use
![]()
Delphi-Quellcode:
But if you do this, be careful to check if Themes are available or else your program will not work on older systems.
uses UxTheme;
TMyForm = class(TForm) private Theme: hTheme; end; procedure TMyForm.FormCreate(Sender: TObject); begin Theme := OpenThemeData(0, 'ListView'); end; procedure TMyForm.FormDestroy(Sender: TObject); begin CloseThemeData(Theme); end; procedure TMyForm.ListViewCustomDraw(...); var StateId: Integer; begin StateId := LIS_SELECTED; // LIS_SELECTEDNOTFOCUS // LIS_NORMAL DrawThemeBackground( Theme, ListView.Canvas.Handle, LVP_LISTITEM, StateId, Rect, nil ); { Other drawing code... } end; [edit] In order to see any difference, you have to execute the SetWindowTheme-code from above first (before you acquire the theme handle). Or you could replace
Delphi-Quellcode:
with
'ListView'
Delphi-Quellcode:
. But the former version is preferable.
'Explorer::ListView'
[/edit] |
Re: TListView.OnDrawItem
Don't working, when try select item, all what is inside control is erased, so component is blank.
Maybe I'm calling in wrong order? I have: OwnerDraw is True, OnDrawItem to make grey text, OnCustom* event with DefaultDraw set to True. Now selection is don't visible, but items are with black and grey text. Then I added your first code for themes to form OnCreate - result was the same, then repleaced it with next example - control area is cleared. :cry: |
AW: TListView.OnDrawItem
Maybe you should post your source code along with a screenshot.
|
Re: TListView.OnDrawItem
Liste der Anhänge anzeigen (Anzahl: 1)
Form:
Delphi-Quellcode:
Source:
object Form20: TForm20
Left = 0 Top = 0 Caption = 'Form20' ClientHeight = 202 ClientWidth = 331 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object lvRecentFiles: TListView Left = 8 Top = 8 Width = 225 Height = 175 Columns = < item Caption = 'Recent files' Width = 221 end> ColumnClick = False DoubleBuffered = True Groups = < item Header = 'Recent files' GroupID = 0 State = [lgsNormal] HeaderAlign = taLeftJustify FooterAlign = taLeftJustify TitleImage = -1 ExtendedImage = -1 end> Items.ItemData = { 03C60000000300000000000000FFFFFFFFFFFFFFFF0000000000000000000000 001F43003A005C00570069006E0064006F00770073005C007300790073007400 65006D00330032005C005300680065006C006C00330032002E0064006C006C00 00000000FFFFFFFFFFFFFFFF0000000000000000000000000B43003A005C0062 006F006F0074002E0069006E00690000000000FFFFFFFFFFFFFFFF0000000000 000000000000001258003A005C006E006F006E00650078006900730074006500 6E0074002E00640061007400} OwnerDraw = True GroupView = True ReadOnly = True RowSelect = True ParentDoubleBuffered = False ShowColumnHeaders = False TabOrder = 0 ViewStyle = vsReport OnCustomDraw = lvRecentFilesCustomDraw OnCustomDrawItem = lvRecentFilesCustomDrawItem OnDrawItem = lvRecentFilesDrawItem end end
Delphi-Quellcode:
Group header has selection, items not :(
unit Unit20;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, UxTheme; type TForm20 = class(TForm) lvRecentFiles: TListView; procedure lvRecentFilesDrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState); procedure lvRecentFilesCustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean); procedure lvRecentFilesCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private Theme: hTheme; public { Public declarations } end; var Form20: TForm20; implementation {$R *.dfm} procedure TForm20.FormCreate(Sender: TObject); begin SetWindowTheme(lvRecentFiles.Handle, 'Explorer', nil); Theme := OpenThemeData(0, 'ListView'); end; procedure TForm20.FormDestroy(Sender: TObject); begin CloseThemeData(Theme); end; procedure TForm20.lvRecentFilesCustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean); //var // StateId: Integer; begin // StateId := LIS_SELECTED; // LIS_SELECTEDNOTFOCUS // LIS_NORMAL // DrawThemeBackground( // Theme, // lvRecentFiles.Canvas.Handle, // LVP_LISTITEM, // StateId, // ARect, // nil // ); DefaultDraw := True; end; procedure TForm20.lvRecentFilesCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); begin DefaultDraw := True; end; procedure TForm20.lvRecentFilesDrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState); var R: TRect; S: string; begin S := Item.Caption; with lvRecentFiles, lvRecentFiles.Canvas do begin if FileExists(S) then Font.Color := clWindowText else Font.Color := clGrayText ; R := Classes.Rect(Rect.Left + 6, Rect.Top + 3, Rect.Right, Rect.Bottom - 2); TextRect(R, S, [tfEndEllipsis]); end; end; end. Screen attached. |
AW: TListView.OnDrawItem
One thing I immediately spotted:
Delphi-Quellcode:
has to be
procedure TForm20.FormCreate(Sender: TObject);
begin SetWindowTheme(lvRecentFiles.Handle, 'Explorer', nil); Theme := OpenThemeData(0, 'ListView'); end;
Delphi-Quellcode:
Because, as I said, you have to manually activate the "explorer style" for each window by calling SetWindowTheme. If you do this for a specific window, but then open the theme data of a different window handle (0), of course it won't work.
procedure TForm20.FormCreate(Sender: TObject);
begin SetWindowTheme(lvRecentFiles.Handle, 'Explorer', nil); Theme := OpenThemeData(lvRecentFiles.Handle, 'ListView'); end; Not saying, that this is the only issue (might be, might not be), but at least it's one reason why it can't work. If it doesn't work after changing this, try to set
Delphi-Quellcode:
to
DefaultDraw
Delphi-Quellcode:
because it's possible that the default drawing routine overdraws the background again (I don't know about that and I'm too lazy to look it up in the VCL sources).
False
Also, I don't know what the difference between
Delphi-Quellcode:
and
OnDrawItem
Delphi-Quellcode:
is, and in which order they are called. But I would suggest moving the
OnCustomDrawItem
Delphi-Quellcode:
-code to the
DrawThemedBackground
Delphi-Quellcode:
routine.
OnDrawItem
|
Re: AW: TListView.OnDrawItem
Now working :D Thanks so much!
Delphi-Quellcode:
Yes, OnCustom* are not required, just OnDrawItem is needed. This cod I putten between font color and text render. And now I have list liki in Delphi :D
Brush.Style := bsClear;
FillRect(Rect); if odSelected in State then begin DrawThemeBackground(Theme, lvRecentFiles.Canvas.Handle, LVP_LISTITEM, LIS_SELECTED, Rect, nil ); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:30 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz