Solved

Change another Applications' Listview ViewStyle

Posted on 2006-11-13
11
282 Views
Last Modified: 2010-04-04
When you go "File-Open" from within MS-Word, it shows the documents in a listview with vsList set as the default.
But i want it to always show as vsReport, so I can see the date modified column

I've tried googling, and have not found a workable solution, so I wish to make a small app that will watch for the dialog appearing, and will set it's viewstyle.

something like:-
FindWindow (ms-word's open file dialog box)
find listview control on the dialog
set the listview's viewstyle to vsReport

Can anyone think of a way to programattically set another application's ListView ViewStyle?
Thanks in advance
0
Comment
Question by:TheRealLoki
  • 5
  • 3
  • 2
11 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
Comment Utility
Providing you can get the listview handle, you should be able to change the window style for the listview control.

Russell

----

eg:

// Requires COMMCTRL in the uses clause
procedure SetListViewStyle(Handle: HWND; Style: DWORD);
var  dwStyle:       DWORD;
begin

  // Check window
  if IsWindow(Handle) then
  begin
     // Get window style
     dwStyle:=GetWindowLong(Handle, GWL_STYLE);
     // Remove the type mask from style
     dwStyle:=dwStyle and (not LVS_TYPEMASK);
     // OR in the desired style which should be one of
     // LVS_ICON, LVS_SMALLICON, LVS_LIST, LVS_REPORT
     dwStyle:=dwStyle or Style;
     // Set window long
     SetWindowLong(Handle, GWL_STYLE, dwStyle);
     // Update the list view
     ListView_Arrange(Handle, LVA_SNAPTOGRID);
  end;

end;
0
 
LVL 26

Assisted Solution

by:EddieShipman
EddieShipman earned 250 total points
Comment Utility
Going Rusell a bit farther, here's code that works on Explorer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, CommCtrl;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure FindLVHandle;
  end;

var
  Form1: TForm1;
  LVHandle: HWND;

implementation

{$R *.dfm}


 
function EnumChildProc(AHandle: hWnd; AnObject: TObject): BOOL; stdcall;
var
  tmpS : string;
  theClassName : string;
  theWinText : string;
begin
  Result := True;
  SetLength(theClassName, 256);
  GetClassName(AHandle, PChar(theClassName), 255);
  SetLength(theWinText, 256);
  GetWindowText(AHandle, PChar(theWinText), 255);
  tmpS := StrPas(PChar(theClassName));
  if theWinText <> EmptyStr then
    tmpS := tmpS + ' "'
            + StrPas(PChar(theWinText)) + '"'
  else
    tmpS := tmpS + '""';
  if Pos(UpperCase('SysListview32'), UpperCase(tmpS)) > 0 then
  begin
    LVHandle := AHandle;
  end;
end;

function WindowEnumProc(AHandle: hWnd; AnObject: TObject): BOOL; stdcall;
// callback for EnumWindows.
var
  theClassName: string;
  theWinText: string;
  tmpS: string;
begin
  Result := True;
  SetLength(theClassName, 256);
  GetClassName(AHandle, PChar(theClassName), 255);
  SetLength(theWinText, 256);
  GetWindowText(AHandle, PChar(theWinText), 255);
  tmpS := StrPas(PChar(theClassName));
  if theWinText <> EmptyStr then
    tmpS := tmpS + ' "'
            + StrPas(PChar(theWinText)) + '"'
  else
    tmpS := tmpS + '""';
  if Pos(UpperCase('ExploreWClass'), UpperCase(tmpS)) > 0 then
  begin
    EnumChildWindows(AHandle, @EnumChildProc, longInt(0));
  end;
end;

procedure SetListViewStyle(Handle: HWND; Style: DWORD);
var
  dwStyle:       DWORD;
begin
  // Check window
  if IsWindow(Handle) then
  begin
     // Get window style
     dwStyle:=GetWindowLong(Handle, GWL_STYLE);
     // Remove the type mask from style
     dwStyle:=dwStyle and (not LVS_TYPEMASK);
     // OR in the desired style which should be one of
     // LVS_ICON, LVS_SMALLICON, LVS_LIST, LVS_REPORT
     dwStyle:=dwStyle or Style;
     // Set window long
     SetWindowLong(Handle, GWL_STYLE, dwStyle);
     // Update the list view
     ListView_Arrange(Handle, LVA_SNAPTOGRID);
  end;
end;

procedure TForm1.FindLVHandle;
begin
  Screen.Cursor := crHourGlass;
  LVHandle := 0;
  try
    EnumWindows(@WindowEnumProc, LongInt(0));
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FindLVHandle;
  if LVHandle <> 0 then
  begin
    SetListViewStyle(LVHandle, LVS_SMALLICON);
  end;
end;

end.
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
I think the EnumProcs code is from Ralph Friedman on the Borland NG's.
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
>> Can anyone think of a way to programattically set another application's ListView ViewStyle?

The crux of the code (listview modification, which was what the question was about) was mine, yet a point split wasn't seen as an option? I will keep this in mind for future reference...

Russell
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
I agree, RealLoki, go to Community Support and ask for a re-open and split.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 17

Author Comment

by:TheRealLoki
Comment Utility
sorry Russel, honestly didn't notice it was the same code...
Don't be offended, just an oversight. have asked for a re-open/split

0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
You mean you didn't see my comment?

[quote]Going Rusell a bit farther, here's code that works on Explorer:[/quote]
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

NP, I just wanted to ensure that this was not an intentional slight.

Russell

0
 
LVL 17

Author Comment

by:TheRealLoki
Comment Utility
EddieShipman, maybe it's a regional thing, but I took that statement to mean, "my way is better" so I didn't think twice about it :-)
I really appreciate the skills of both of you btw
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
I suppose, what I really meant to say was "taking Russell's example a bit farther..."
which would have essentially meant, his example works, I just "enhanced" it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now