C++ Builder

La Isla Bonita



Adjust Form to the taskbar
Adjust Form to the taskbar


Description

This example shows how to adjust a form to the taskbar automatically. Form1 contains a timer (Timer1). The form dimensions are 800X600

AdjustToTaskBar.cpp

#include "AdjustToTaskbar.h"
//----------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
    Width  = MAINFORM_WIDTH;
    Height = MAINFORM_HEIGHT;
    Timer1->Enabled = true;
    Timer1->Interval = 200;
}
//----------------------------------------------------------------------

void __fastcall TForm1::Set_FormDimensions( void )
{
    HWND hTaskBar = FindWindow("Shell_TrayWnd", NULL);
    int taskbar_top;
    int taskbar_bottom;
    int taskbar_height;
    int taskbar_width;

    if(hTaskBar != NULL)
    {
        RECT r;
        GetWindowRect(hTaskBar, &r);
        taskbar_top    = r.top;
        taskbar_bottom = r.bottom;
        taskbar_width  = r.right - r.left;
        taskbar_height = r.bottom - r.top;

        if ( taskbar_height >= taskbar_width )
        {
            //VERTICAL TASKBAR
            Top   = 0;
            Width  = MAINFORM_WIDTH  - ( taskbar_width  );
            Height = MAINFORM_HEIGHT;

            if ( r.left > 10 )
            {
                //VERTICAL ON THE RIGHT OF THE SCREEN
                Left    = 0;
            }
            else
            {
                //VERTICAL ON THE LEFT OF THE SCREEN
                Left    = r.right;
            }
        }
        else
        {   //HORIZONTAL TASKBAR
            Left   = 0;
            Width  = MAINFORM_WIDTH;
            Height = MAINFORM_HEIGHT - ( taskbar_height );

            if ( r.top > 10 )
            {
                //HORIZONTAL ON THE BOTTOM OF THE SCREEN
                //TaskBar is shown
                Top    = 0;
            }
            else
            {
                //HORIZONTAL ON THE TOP OF THE SCREEN
                Top    = r.bottom;
            }
        }
    }
    else
    {
        Width  = MAINFORM_WIDTH;
        Height = MAINFORM_HEIGHT;
    }
}
//-----------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    Set_FormDimensions();
}

AdjustToTaskBar.h

#ifndef AdjustToTaskbarH
#define AdjustToTaskbarH
//---------------------------------------------------------------------------

#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\ExtCtrls.hpp>

//---------------------------------------------------------------------------
int const MAINFORM_WIDTH = 800;
int const MAINFORM_HEIGHT = 600;

class TForm1 : public TForm
{
__published:	// IDE-managed Components
	TTimer *Timer1;
	
	void __fastcall Timer1Timer(TObject *Sender);
private:	// User declarations
public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
    void __fastcall Set_FormDimensions();    
};
//----------------------------------------------------------------
extern TForm1 *Form1;
//----------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.