Solved

Right print margin for RichEdit

Posted on 1999-01-08
5
646 Views
Last Modified: 2013-11-22
In the Delphi IDE a gray line in the editor indicates the right margin for the text should it be printed. How can i do this with the RichEdit control?
0
Comment
Question by:GVM
  • 4
5 Comments
 
LVL 5

Accepted Solution

by:
heathprovost earned 50 total points
ID: 1355219
Here is one way to do it that doesnt require creating a descendant control (although you can use this method in a descendant if that is the route you want to take):



unit RETest;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure MyWndProc(var Message: TMessage);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  OldWndProc: TWndMethod;
  PageWidth: integer; //width of page in characters
implementation

{$R *.DFM}

procedure TForm1.MyWndProc(var Message: TMessage);
var
  MyCanvas: TCanvas;
  TextMetric: TTextMetric;
begin
  OldWndProc(Message); //call original WndProc
  if Message.Msg = WM_PAINT then //Paint the line
  begin
        MyCanvas := TCanvas.Create;
        MyCanvas.Handle := GetDC(RichEdit1.Handle);
    GetTextMetrics(MyCanvas.Handle, TextMetric);
    MyCanvas.Pen.Color := clSilver;
    MyCanvas.pen.Width := 1;
    MyCanvas.MoveTo(TextMetric.tmAveCharWidth * PageWidth, 0);
    MyCanvas.LineTo(TextMetric.tmAveCharWidth * PageWidth, RichEdit1.ClientHeight);
            ReleaseDC(RichEdit1.Handle, MyCanvas.Handle);
        MyCanvas.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Top := 10;
  Edit1.Left := 10;
  Edit1.Width := 150;
  Button1.Left := 170;
  Button1.Top := 10;
  RichEdit1.Top := 20;
  RichEdit1.Align := alBottom;
  PageWidth := 80;
  OldWndProc := RichEdit1.WindowProc; //store original WndProc
  RichEdit1.WindowProc := MyWndProc; //Set new WndProc
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.Text <> '' then
  begin
    PageWidth := StrToInt(Edit1.Text);
    RichEdit1.Invalidate;
  end;
end;

end.unit RETest;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure MyWndProc(var Message: TMessage);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  OldWndProc: TWndMethod;
  PageWidth: integer; //width of page in characters
implementation

{$R *.DFM}

procedure TForm1.MyWndProc(var Message: TMessage);
var
  MyCanvas: TCanvas;
  TextMetric: TTextMetric;
begin
  OldWndProc(Message); //call original WndProc
  if Message.Msg = WM_PAINT then //Paint the line
  begin
        MyCanvas := TCanvas.Create;
        MyCanvas.Handle := GetDC(RichEdit1.Handle);
    GetTextMetrics(MyCanvas.Handle, TextMetric);
    MyCanvas.Pen.Color := clSilver;
    MyCanvas.pen.Width := 1;
    MyCanvas.MoveTo(TextMetric.tmAveCharWidth * PageWidth, 0);
    MyCanvas.LineTo(TextMetric.tmAveCharWidth * PageWidth, RichEdit1.ClientHeight);
            ReleaseDC(RichEdit1.Handle, MyCanvas.Handle);
        MyCanvas.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Top := 10;
  Edit1.Left := 10;
  Edit1.Width := 150;
  Button1.Left := 170;
  Button1.Top := 10;
  RichEdit1.Top := 20;
  RichEdit1.Align := alBottom;
  PageWidth := 80;
  OldWndProc := RichEdit1.WindowProc; //store original WndProc
  RichEdit1.WindowProc := MyWndProc; //Set new WndProc
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.Text <> '' then
  begin
    PageWidth := StrToInt(Edit1.Text);
    RichEdit1.Invalidate;
  end;
end;

end.



Just place a button, edit, and RichEdit on your form and then copy the code over your form code.  entering a number into the edit box changes the pagewidth so you can see what is happening.

Hope this is what you are looking for.

Heath
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355220
ooops, I pasted twice - just grab up to the first "end." Sorry about that.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1355221
Hello guys!

heathprovost, I'm just wondering what is the reason for saving the original WndProc() of the TRichEdit contro?!?!

-Viktor
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355222
viktornet, I am not sure I understand your question.  Maybe I am missing something (I know you probably know more about this kind of stuff than I do).  Dont you have to?  I mean how can you call the original WndProc without first storing its Address?  Is there a better way to do this?

Heath
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355223
BTW - I am still learning this subclassing stuff and Delphi makes it even harder than it is because it can be done so many different ways.  Every example I see is done with a different method.  Some use message procedures, some subclass the WndProc, and others us the CreateSubClass.  I am just confused I guess.  But I am only happy when I am confused :)

Heath
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question