Solved

Minimize/restore any window from my app

Posted on 2004-09-16
3
1,517 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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