C++ Builder

La Isla Bonita



SendMessage Function and Windows Message: WM_VSCROLL
SendMessage Function and Windows Message: WM_VSCROLL


Description

This example illustrates how to scroll a TRichEdit control either UP or DOWN via the Win32 API function: SendMessage().
This is achieved by sending the WM_VSCROLL message though that function.

The SendMessage function is a Win32 API function

To scroll up we set wParam to SB_PAGEUP

    SendMessage(RichEdit1->Handle, WM_VSCROLL, SB_PAGEUP, 0);
To scroll down we set wParam to SB_PAGEDOWN

    SendMessage(RichEdit1->Handle, WM_VSCROLL, SB_PAGEDOWN, 0);
Note that to be able to scroll the RichEdit component, the focus has to be on this component.

Form1 has two TButtons and a TRichEdit component.

Files

SendMessageVSCROLL.cpp

#include <vcl.h>
#include 
#pragma hdrstop

#include "SendMessageVSCROLL.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    SendMessage(RichEdit1->Handle, WM_VSCROLL, SB_PAGEUP, 0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    SendMessage(RichEdit1->Handle, WM_VSCROLL, SB_PAGEDOWN, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RichEdit1KeyDown(TObject *Sender, WORD &Key,
                                         TShiftState Shift)
{
    if (Key == VK_F1)
    {
        SendMessage(RichEdit1->Handle, WM_VSCROLL, SB_PAGEUP, 0);
    }

    if (Key == VK_F2)
    {
        SendMessage(RichEdit1->Handle, WM_VSCROLL, SB_PAGEDOWN, 0);
    }
}
//---------------------------------------------------------------------------

SendMessageVSCROLL.h

//---------------------------------------------------------------------------
#ifndef SendMessageVSCROLLH
#define SendMessageVSCROLLH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TRichEdit *RichEdit1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall RichEdit1KeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

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