Drawing on top of all other controls 

Sometimes it is neccessary to draw on top of other controls.  To accomplish this, you'll need to get a handle to the device context of the screen (desktop).  Then you can draw on top of anything you want, including the Start Button, the System Tray, and other windows.  Since drawing on this DC requires screen coordinates, you can use the function ClientToScreen().  In the example below, I dropped two TImages on a form, and spaced them a considerable distance apart.  A blue line of width 5 is then drawn to connect the bottom left corner of Image1 to the top right corner of Image2... 

KEYWORDS: TCanvas, GetDC, ReleaseDC 
 

 

//in header file 
TCanvas *ScreenCanvas;  
 

 
 

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 

     ScreenCanvas = new TCanvas(); 

//--------------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender) 

    ScreenCanvas->Pen->Color = clBlue; 
    ScreenCanvas->Pen->Width = 4; 
    ScreenCanvas->Handle = GetDC(0); 

    TPoint P1, P2; 
    P1.x = Image1->BoundsRect.Left; 
    P1.y = Image1->BoundsRect.Bottom; 
    P2.x = Image2->BoundsRect.Right; 
    P2.y = Image2->BoundsRect.Top; 

    ScreenCanvas->MoveTo(ClientToScreen(P1).x, ClientToScreen(P1).y); 
    ScreenCanvas->LineTo(ClientToScreen(P2).x, ClientToScreen(P2).y); 

    ReleaseDC(0, ScreenCanvas->Handle); 

//--------------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) 

     delete ScreenCanvas;