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.

Q: Hide the application's taskbar icon

Answer:

First, let's get some terminology straight. The system tray is the little box in the corner of the task bar where programs can display small icons. Some examples include the SysAgent icon, and the Outlook icon when you get new mail. The taskbar is the toolbar that stretches across the screen. This is where program icons are located. To hide a program's taskbar icon, you call the ShowWindow function and pass the Application->Handle window handle.

ShowWindow(Application->Handle, SW_HIDE);

To make the taskbar icon re-appear, simply change SW_HIDE to SW_SHOW.

ShowWindow(Application->Handle, SW_SHOW);

Note: You can hide the main form by setting its Visible property to false.

Note: Hiding the form's taskbar icon with ShowWindow is not permanent. Certain actions will cause the taskbar icon to reappear. You can remove a program's taskbar icon and prevent it from ever appearing again by making the hidden application window a tool window. Tool windows never have a taskbar icon. Making the application window a tool window has the side effect of removing the program from the list of programs that appear when the user presses ALT-TAB. You can make the application window a tool window by calling the API GetWindowLong and SetWindowLong functions.

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){ DWORD dwExStyle = GetWindowLong(Application->Handle, GWL_EXSTYLE); dwExStyle |= WS_EX_TOOLWINDOW; SetWindowLong(Application->Handle, GWL_EXSTYLE, dwExStyle); try { Application->Initialize(); Application->CreateForm(__classid(TForm1), &Form1); Application->Run(); } catch (Exception &exception) { Application->ShowException(&exception); } return 0;}

Наши рекомендации