Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Right print margin for RichEdit

Posted on 1999-01-08
5
Medium Priority
?
652 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 100 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

604 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