Solved

Minimize/restore any window from my app

Posted on 2004-09-16
3
1,347 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

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.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

27 Experts available now in Live!

Get 1:1 Help Now