Solved

Trying to customize height and width of twebbrowser component

Posted on 2011-02-23
16
608 Views
Last Modified: 2012-05-11
Hi

I am trying to change some default values of the twebbrowser component and some succeed but especially the width and height will not succeed. Can anyone tell me why this fails?

The Align property works but the height and width do not
unit NullWebBrowser;

interface

uses
  SysUtils, Classes, Controls, OleCtrls, SHDocVw;

type
  TNullWebBrowser = class(TWebBrowser)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
   constructor Create(AOwner: TComponent); override;

  published
    property Width default 800;
    property Height default 500;
    property Align default alBottom;
  end;

procedure Register;

implementation

constructor TNullWebbrowser.Create(AOwner: TComponent);
   begin
    inherited Create(AOwner);
     Width := 800;
     Height := 500;
     Align := alBottom;
end;


procedure Register;
begin
  RegisterComponents('nullified', [TNullWebBrowser]);
end;

Open in new window

0
Comment
Question by:PeterdeB
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 34959236

I made a simple test with TWebbrowser :

     align and height width setting by placing directly on a form works fine , also for you ???
0
 
LVL 8

Expert Comment

by:BdLm
ID: 34959282
OK,  what di you pass in the create function as an argument ??


nil ???  that should not work
0
 
LVL 8

Expert Comment

by:BdLm
ID: 34959328
I copied your code and installed that component , worked fine as expected.....

do your create the BullWebbrowser at run time, if yes then show your code for that ....  
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34959365
I am not sure but may be you need to create a wrapper from the TOleControl/TWinControl/TControl (I am just providing the properties you need to work with, it requires some work):
  TControl = class(TComponent)
  private
    FLeft: Integer;
    FTop: Integer;
    FWidth: Integer;
    FHeight: Integer;
    FAlign: TAlign;
    procedure SetAlign(Value: TAlign);
    procedure SetHeight(Value: Integer);
    procedure SetWidth(Value: Integer);
  public
    property Align: TAlign read FAlign write SetAlign default alNone; // default alBottom
  published
    property Width: Integer read FWidth write SetWidth; // default 800
    property Height: Integer read FHeight write SetHeight; // default 500
end;

procedure TControl.SetAlign(Value: TAlign);
var
  OldAlign: TAlign;
begin
  if FAlign <> Value then
  begin
    OldAlign := FAlign;
    FAlign := Value;
    Anchors := AnchorAlign[Value];
    if not (csLoading in ComponentState) and (not (csDesigning in ComponentState) or
      (Parent <> nil)) and (Value <> alCustom) and (OldAlign <> alCustom) then
      if ((OldAlign in [alTop, alBottom]) = (Value in [alRight, alLeft])) and
        not (OldAlign in [alNone, alClient]) and not (Value in [alNone, alClient]) then
        SetBounds(Left, Top, Height, Width)
      else
        AdjustSize;
  end;
  RequestAlign;
end;

procedure TControl.SetWidth(Value: Integer);
begin
  SetBounds(FLeft, FTop, Value, FHeight);
  Include(FScalingFlags, sfWidth);
end;

procedure TControl.SetHeight(Value: Integer);
begin
  SetBounds(FLeft, FTop, FWidth, Value);
  Include(FScalingFlags, sfHeight);
end;

constructor TControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidth:= 800;
  FHeight:= 500;
  FAlign:= [alNone, alTop, alBottom, alLeft, alRight, alClient, alCustom];
end;

Open in new window

0
 
LVL 3

Accepted Solution

by:
pasolo earned 500 total points
ID: 34959375
Try this, it may help

unit nullWebBrowser;

interface
uses
  SysUtils, Classes, Controls, OleCtrls, SHDocVw;

type
  TNullWebBrowser = class(TWebBrowser)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
   constructor Create(AOwner: TComponent); override;

  published
    property Width default 800;
    property Height default 500;
    property Align default alBottom;
  end;


implementation

constructor TNullWebbrowser.Create(AOwner: TComponent);
   begin
     inherited Create(AOwner);
     TwinControl(self).Parent := TwinControl(AOwner);
     TwinControl(Aowner).Width := 800;
     TWinControl(AOwner).Height := 500;
     Align := alClient;
     Navigate('http://www.microsoft.com');
end;

end.


unit browsertest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, NullWebBrowser, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    wb1 : TNullWebBrowser;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
       wb1 := TNullWebBrowser.Create(panel1);
end;

end.
0
 
LVL 3

Expert Comment

by:pasolo
ID: 34959431
Since you want to align to bottom, you need to align the container to bottom, so the constructor will become:

constructor TNullWebbrowser.Create(AOwner: TComponent);
   begin
     inherited Create(AOwner);
     TwinControl(self).Parent := TwinControl(AOwner);
     TwinControl(Aowner).Width := 800;
     TWinControl(AOwner).Height := 500;
     TWinControl(AOwner).Align := alBottom;
     align := alClient;
     Navigate('http://www.microsoft.com');
end;
0
 

Author Comment

by:PeterdeB
ID: 34959728
@BdLm:yes they do. but I have to create around 35 of such projects all are different but the size of the webbrowser window is the same, that is why I wondered if I could make that happen automatically thanks for your fast reply
0
 

Author Comment

by:PeterdeB
ID: 34963855
@Bdsm: again thanks for your fast reply, I could not get much further though

@jimyX, I dont mind at all if I have to work for it, thanks for your reply, but it compiled with so many errors, i had a hard time fixing the first :(

@pasolo thanks for your reply too, your code compiled without errors, it is almost perfect but i need to place some edit controls on the form too, and the component i created with your code, takes up all of the form

Will you be willing to help me further so that the component does not take up all of the space, the align property is not required, just as long as it stays 800 x 500, and leaves space for other things

I have worked with your code and customized it and attached the sample

Again thanks for all the help and support here

Grtz
unit nullWebBrowser;

interface
uses
  SysUtils, Classes, Controls, OleCtrls, SHDocVw;

type
  TNullWebBrowser = class(TWebBrowser)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
   constructor Create(AOwner: TComponent); override;

  published
    property Width default 800;
    property Height default 500;
    property Align default alBottom;
  end;

procedure Register;

implementation

constructor TNullWebbrowser.Create(AOwner: TComponent);
   begin
     inherited Create(AOwner);
     TwinControl(self).Parent := TwinControl(AOwner);
     TwinControl(Aowner).Width := 800;
     TWinControl(AOwner).Height := 500;
     Align := alClient;
end;

procedure Register;
begin
  RegisterComponents('nullified', [TNullWebBrowser]);
end;


end.

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 32

Expert Comment

by:ewangoya
ID: 34964222

You are trying to adjust the size of the parent control automatically???
Not a good idea

     TwinControl(self).Parent := TwinControl(AOwner);
     TwinControl(Aowner).Width := 800;
     TWinControl(AOwner).Height := 500;


What if the OWner is not a TWinControl, What happens to this piece of code?
You will get an invalid cast.

You should at least try and test the owner type
The Owner may also not have properties for Width and Height, think about that

One more thing, the Owner is not always the Parent, Parent and Owner can be different
0
 
LVL 3

Expert Comment

by:pasolo
ID: 34964478
What I have done is place your TWebBrowser descendant inside a panel and resize it to 800x500..
The panel is inside your form, That leaves space in the form for other controls, like edit boxes, buttons or anything else. Of course you can place the panel inside other component, like other panel.

@ewangoya:
I know, but what I have done is required and owners are not parents of couse..
0
 

Author Comment

by:PeterdeB
ID: 34964914
Well the component almost works, and saves me lots of time adjusting the width and height of the browser window
0
 

Author Comment

by:PeterdeB
ID: 34968738
Pasolo sorrry i completely misssed your comment! Thanks
0
 

Author Closing Comment

by:PeterdeB
ID: 34968846
Thanks for fixing my issue
0
 

Author Comment

by:PeterdeB
ID: 34968857
I removed one line and it works perfectly
unit nullWebBrowser;

interface
uses
  SysUtils, Classes, Controls, OleCtrls, SHDocVw;

type
  TNullWebBrowser = class(TWebBrowser)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
   constructor Create(AOwner: TComponent); override;

  published
    property Width default 800;
    property Height default 500;
    property Align default alBottom;
  end;


implementation

constructor TNullWebbrowser.Create(AOwner: TComponent);
   begin
     inherited Create(AOwner);
     TwinControl(self).Parent := TwinControl(AOwner);
     TwinControl(Aowner).Width := 800;
     TWinControl(AOwner).Height := 500;
     Align := alClient;
end;

end.

Open in new window

0
 

Author Comment

by:PeterdeB
ID: 34968865
By the way it worked perfectly already but i removed the navigate line so it will not browse automatically
0
 
LVL 3

Expert Comment

by:pasolo
ID: 34969441
I put the navigation to see clearly where it was placed..
Enjot:)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now