Solved

Right print margin for RichEdit

Posted on 1999-01-08
5
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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