Solved

Minimize/restore any window from my app

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

734 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