Solved

Change another Applications' Listview ViewStyle

Posted on 2006-11-13
11
287 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
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:EddieShipman
EddieShipman earned 250 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:EddieShipman
ID: 17938384
I think the EnumProcs code is from Ralph Friedman on the Borland NG's.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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:EddieShipman
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:EddieShipman
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:EddieShipman
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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