Voici un morceau de programme permettant d'avoir toujours visible a l'ecran des composants places sur un TForm, un TImage beaucoup plus grand que l'ecran est place sur le TForm du coup les composants disparaissent de la partie visible quand on deplace le ScrollBar, un TPanel avec d'autre composants places dessus est deplacable et reste dans la position dans laquelle on la place quelque soit la position du ScrollBar, un exemple valant mieux qu'un long discour voici le code
Le .h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
     TButton *Button1;
     TImage *Image1;
     TLabel *Label1;
     TPanel *Panel1;
     TButton *Button2;
     void __fastcall Panel1MouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
     void __fastcall Button1Click(TObject *Sender);
     void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public:  // User declarations
     __fastcall TForm1(TComponent* Owner);
     void __fastcall WMVScroll(TMessage &Msg);
     BEGIN_MESSAGE_MAP
     MESSAGE_HANDLER(WM_VSCROLL, TMessage, WMVScroll)
     END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
le .cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
     : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMVScroll(TMessage &Msg)
{
int a = Panel1->Top; // doit etre place imperativement avant la ligne suivante
// afin de conserver la position sur l'ecran
TForm::Dispatch(&Msg); // imperatif sinon le ScrollBar ne defile pas
// (Form1->VertScrollBar->Increment / (Form1->VertScrollBar->Range - Form1->ClientHeight))
// formule permettant de calculer la position en hauteur dans la Form quelque soit
// la position du SCrollBar afin d'avoir le ou les composants toujours a l'ecra
Button1->Top = GetScrollPos(Form1->Handle, SB_VERT) * (Form1->VertScrollBar->Increment / (Form1->VertScrollBar->Range - Form1->ClientHeight));
Label1->Top = GetScrollPos(Form1->Handle, SB_VERT) * (Form1->VertScrollBar->Increment / (Form1->VertScrollBar->Range - Form1->ClientHeight));
Panel1->Top = a + GetScrollPos(Form1->Handle, SB_VERT) * (Form1->VertScrollBar->Increment / (Form1->VertScrollBar->Range - Form1->ClientHeight));
Label1->Caption = Msg.WParamLo;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
// permet de deplacer le Panel
ReleaseCapture();
SendMessage(Panel1->Handle, WM_SYSCOMMAND, 0xF012, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// affiche le Panel avec un effet ascenseur
Panel1->BringToFront();
Panel1->Top = 30;
Panel1->Left = 10;
Panel1->Height = 0;
for(int i = 0; i <= 130; i++)
     {
     Panel1->Height = i;
     Panel1->Repaint();
     }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// le Button2 est place sur le Panel
// efface le Panel avec un effet ascenseur
for(int i = 130; i >= 0; i--)
     {
     Panel1->Height = i;
     Panel1->Repaint();
     }
Panel1->Height = 0;
}
//---------------------------------------------------------------------------