Solved

Trying to customize height and width of twebbrowser component

Posted on 2011-02-23
16
610 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

15 Experts available now in Live!

Get 1:1 Help Now