?
Solved

Change another Applications' Listview ViewStyle

Posted on 2006-11-13
11
Medium Priority
?
298 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
10 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 1000 total points
ID: 17935370
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:Eddie Shipman
Eddie Shipman earned 1000 total points
ID: 17938358
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:Eddie Shipman
ID: 17938384
I think the EnumProcs code is from Ralph Friedman on the Borland NG's.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Expert Comment

by:Russell Libby
ID: 17952937
>> 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:Eddie Shipman
ID: 17955953
I agree, RealLoki, go to Community Support and ask for a re-open and split.
0
 
LVL 17

Author Comment

by:TheRealLoki
ID: 17958946
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:Eddie Shipman
ID: 17960250
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
ID: 17960438

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

Russell

0
 
LVL 17

Author Comment

by:TheRealLoki
ID: 17960853
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:Eddie Shipman
ID: 17961262
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

807 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