Sample Application: CPPRichEditDrag.cpp

Project source
Form header (C++)
Form source (C++)
Form source (DFM)

Design-time form image
Project source: CPPRichEditDrag.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("CPPRichEditDrag.res");
USEFORM("fmCPPRichEditDrag.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    try
    {
        Application->Initialize();
        Application->CreateForm(__classid(TForm1), &Form1);
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    return 0;
}
//---------------------------------------------------------------------------
Back to top
Form header: fmCPPRichEditDrag.h

//---------------------------------------------------------------------------
#ifndef fmCPPRichEditDragH
#define fmCPPRichEditDragH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "uoole.hpp"
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TPanel *Panel1;
    TRichEdit *RichEdit1;
    TUOTextTarget *UOTextTarget1;
    void __fastcall UOTextTarget1Drop(TObject *Sender,
          TWinControl *Acceptor, const AnsiString dropText, int X, int Y);

    void __fastcall UOTextTarget1DragOver(TObject *Sender,
          TDropEffect effect, int X, int Y);
    
private:	// User declarations
    int __fastcall CharPosInRichEdit(TRichEdit* aRichEdit, int X, int Y);
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Back to top
Form source: fmRichEditDrag.cpp

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

#include "fmCPPRichEditDrag.h"
#include "uoutil.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "uoole"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::UOTextTarget1Drop(TObject *Sender,
      TWinControl *Acceptor, const AnsiString dropText, int X, int Y)
{
  // Someone has dropped either text or rich text on us.  If it's rich text,
  // make sure it shows up with all of its formatting intact.
  // AddContentToRichEdit is in uoutil.
  TUOTextTarget* uott;
  uott = (TUOTextTarget*) Sender;

  AddContentToRichEdit(dropText, (TRichEdit*) Acceptor,
    (uott->DroppedTextFormat == dtfRichText), TRUE);
}
//---------------------------------------------------------------------------

int __fastcall TForm1::CharPosInRichEdit(TRichEdit* aRichEdit, int X, int Y)
// Get the current character position (0-based) in the TRichEdit aRichEdit
// corresponding to the client coordinates X, Y.  Returns -1 if there
// is no corresponding character.
// ****THERE IS A BUG IN THE MICROSOFT WIN32 HELP**** regarding this - the
// parameters are passed differently from the EM_CHARFROMPOS of an edit (TMemo,
// TEdit) control.  Same goes for EM_POSFROMCHAR.  See MS Knowledgebase
// article PSS ID Number: Q137805  See PixelPosInRichEdit below for the inverse
// function.
{
TPoint aPt;
  aPt.x = X;  aPt.y = Y;
  return aRichEdit->Perform(EM_CHARFROMPOS, 0, (LPARAM) &aPt);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::UOTextTarget1DragOver(TObject *Sender,
      TDropEffect effect, int X, int Y)
// Handle keeping track of the caret location during dragging
{ int aCharPos;
  // Find the current character position.
  aCharPos = CharPosInRichEdit(RichEdit1, X, Y);

  // If it makes sense, then put the caret there.
  if (aCharPos != -1) {
    RichEdit1->SelStart = aCharPos; // Set the selection start
    RichEdit1->SelLength = 0; // No actual selection
    RichEdit1->SetFocus();  // Focus it, to see the caret
  }
}
//---------------------------------------------------------------------------
Back to top
Form source: fmCPPRichEditDrag.dfm

object Form1: TForm1
  Left = 233
  Top = 225
  Width = 453
  Height = 282
  Caption = 'Drag Rich Text to or from this TRichEdit control'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 445
    Height = 255
    Align = alClient
    BevelOuter = bvNone
    BorderWidth = 3
    TabOrder = 0
    object RichEdit1: TRichEdit
      Left = 3
      Top = 3
      Width = 439
      Height = 249
      Align = alClient
      TabOrder = 0
    end
  end
  object UOTextTarget1: TUOTextTarget
    AcceptorControl = RichEdit1
    AcceptTextFormats = [dtfRichText, dtfText]
    OnDragOver = UOTextTarget1DragOver
    OnDrop = UOTextTarget1Drop
    Left = 232
    Top = 64
  end
end
Back to top

Back to the examples page