Solved

Minimize/restore any window from my app

Posted on 2004-09-16
3
1,374 Views
Last Modified: 2008-01-09
Hello!
I want to make a single button which would minimize/restore some window (it's not in my application) (first click - minimizing, second click - restoring etc).
I found 2 ways how to do it if I maximize window, but I can't find how to do it with minimizing...
How can I:
1) minimize a window properly like MS Windows does it?
2) check if window is minimized?
3) restore the window to its normal position and size if it is minimized?

Thanks..
0
Comment
Question by:ZhaawZ
3 Comments
 
LVL 4

Accepted Solution

by:
MrJul earned 65 total points
ID: 12078443
This little function should do the trick :


// Takes window handle as a parameter
procedure MinRestoreWindow(hWnd: Cardinal);
var
    WP: WINDOWPLACEMENT;
begin

    // Check if window is minimized
    WP.length := SizeOf(WP);
    GetWindowPlacement(hWnd, @WP);
    if (WP.showCmd = SW_SHOWMINIMIZED) or (WP.showCmd = SW_SHOWMINNOACTIVE) then
        WP.showCmd := SW_RESTORE // Yes, then restore it
    else
        WP.showCmd := SW_MINIMIZE; // No, then minimize it
    WP.flags := 0;

    // Go
    SetWindowPlacement(hWnd, @WP);

end;
0
 
LVL 7

Assisted Solution

by:LRHGuy
LRHGuy earned 60 total points
ID: 12078575
Here's how I did it...replace "NameOfWindow" with the window of interest...

procedure TForm2.Button3Click(Sender: TObject);
var
  H:HWnd;
  Placement: TWindowPlacement;
begin
  H:=FindWindow(0,'NameOfWindow');
  if H<>0 then begin
    Placement.length := SizeOf(TWindowPlacement);
    GetWindowPlacement(H, @Placement);
    case Placement.showCmd of
      SW_SHOWMINIMIZED: Placement.ShowCmd:=SW_ShowNormal;
      SW_SHOWMAXIMIZED: Placement.ShowCmd:=SW_ShowMinimized;
      else  Placement.ShowCmd:=SW_ShowMaximized;
    end;
    SetWindowPlacement(H,@Placement);
  end;
end;

GetWindowPlacement will tell you the current state of the window.
0
 
LVL 11

Author Comment

by:ZhaawZ
ID: 12078748
Thanks!
Booth examples work almost fine. There was only one problem in booth of them - when restoring window it doesn't stay maximized if it was maximized before minimizing. I solved it by replacing SetWindowPlacement to sending WM_SYSCOMMAND message.

var
  wnd : hwnd;
  info : WINDOWPLACEMENT;
begin
wnd := FindWindow('mirc', nil);
info.length := SizeOf(TWindowPlacement);
GetWindowPlacement(wnd, @info);
if (info.showCmd = SW_SHOWMINIMIZED) or (info.showCmd = SW_SHOWMINNOACTIVE) then begin
  SendMessage(wnd, WM_SYSCOMMAND, SC_RESTORE, 0)
end else begin
  SendMessage(wnd, WM_SYSCOMMAND, SC_MINIMIZE, 0)
end;
end;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

18 Experts available now in Live!

Get 1:1 Help Now