Home Articles Books Downloads FAQs Tips

Q: Minimize or restore the program in code


Answer:

You can achieve this by using one of three different methods. First, you can send a windows message to to the main form's Handle property or to Application->Handle. The message would be WM_SYSCOMMAND with wParam set to SC_MINIMIZE or SC_RESTORE. You can send the message by calling the SendMessage API function.

    // To minimize, pass SC_MINIMIZE as the WPARAM
    SendMessage(Application->Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);

    // To restore, pass SC_RESTORE as the WPARAM
    SendMessage(Application->Handle, WM_SYSCOMMAND, SC_RESTORE, 0);

The second method is to call the ShowWindow API function. You must pass the ShowWindow function the handle of the global Application object. If you pass it the handle of the main form, the main form will minimize to the desktop instead of minimizing to the taskbar.

    // To minimize, pass SW_MINIMIZE to ShowWindow
    ShowWindow(Application->Handle, SW_MINIMIZE);

    // To restore, pass SW_RESTORE to ShowWindow
    ShowWindow(Application->Handle, SW_RESTORE);
The third method is to call the Minimize or Restore functions of the global Application object. Minimize and Restore are methods of TApplication.

    // To minimize the app, call Minimize
    Application->Minimize();

    // To restore the app, call Restore
    Application->Restore();

The Application methods are easy to use, but sending the WM_SYSCOMMAND message is more versatile. In addition to minimizing and restoring, the WM_SYSCOMMAND message allows you to maximize the application (restoring may or may not maximize the form), change the cursor to the help cursor, scroll the program, move a window, size a window, or simulate Alt-TAB my moving to the next window. Keep in mind that some of these functions, such as sizing and moving, are better served by the SetWindowPos API function.

Although calling ShowWindow does work, you probably don't want to use it to minimize or restore your program. ShowWindow causes the minimization animation to appear while the hidden application window is being minimized. This looks somewhate goofy, because the animation is off center from the position of the mainform of the program.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.