Recent

Author Topic: [CLOSED] WebView4Delphi question  (Read 447 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1694
[CLOSED] WebView4Delphi question
« on: October 31, 2025, 12:18:42 pm »
Hello,

I'd like to make a form simply shows the HTML page made from my program using WebView4Delphi.
The form is created on fly as follows.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.    ts: string;
  4. begin
  5.    ts := aqfb5.printQnnr('100000042', -100);
  6.  
  7.    with TfrBrowser.Create(nil, ts) do  begin
  8.         Show;
  9.         wvbrowser1.navigatetostring(UTF8Decode(ts));  // but this does not work.
  10.    end;
  11. end;          
  12.  

The content of ts is displayed well on normal webbrowsers. What I want to do is to show this content in a pop-up window, e.g. Tfrbrowser created. 

But in the above example, I cannot execute wvbrowser1.navigatetostring(UTF8Decode(ts)); . I do not know why.  Possibly becasue of asynchronous process, etc.


Content of fbrowser.pas is as following. 

Code: Pascal  [Select][+][-]
  1. unit fbrowser;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.    Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  9.    uWVBrowser, uWVWindowParent, uWVLoader, uWVBrowserBase, uWVTypes, uWVEvents,
  10.    Messages;
  11.  
  12. type
  13.  
  14.    { TfrBrowser }
  15.  
  16.    TfrBrowser = class(TForm)
  17.       WVBrowser1: TWVBrowser;
  18.       WVWindowParent1: TWVWindowParent;
  19.       procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  20.       procedure FormResize(Sender: TObject);
  21.       procedure FormShow(Sender: TObject);
  22.       procedure WVBrowser1AfterCreated(Sender: TObject);
  23.    private
  24.       FTextContent: string;
  25.       procedure setBrowserContent(AValue: string);
  26.       procedure Initialize;
  27.  
  28.    public
  29.       property HTMLText : string write setBrowserContent;
  30.  
  31.       constructor Create(AnOwner:TComponent; TextContent: string);
  32.  
  33.    protected
  34.       procedure WMMove(var aMessage: TWMMove);  message WM_MOVE;
  35.       procedure WMMoving(var aMessage: TMessage); message WM_MOVING;
  36.  
  37.    end;
  38.  
  39. var
  40.    frBrowser: TfrBrowser;
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. procedure TfrBrowser.WVBrowser1AfterCreated(Sender: TObject);
  47. begin
  48.     WVWindowParent1.UpdateSize;
  49.     Caption := 'View responses';
  50. end;
  51.  
  52. procedure TfrBrowser.setBrowserContent(AValue: string);
  53. begin
  54.    WVBrowser1.NavigateToString(UTF8Decode(AValue));
  55. end;
  56.  
  57. procedure TfrBrowser.Initialize;
  58. begin
  59.   if GlobalWebView2Loader.InitializationError then
  60.     showmessage(UTF8Encode(GlobalWebView2Loader.ErrorMessage))
  61.    else
  62.     if GlobalWebView2Loader.Initialized then begin
  63.        WVBrowser1.CreateBrowser(WVWindowParent1.Handle);
  64.     end
  65.     else begin
  66.       showmessage('initialization failed');
  67.       exit;
  68.     end;
  69. end;
  70.  
  71. constructor TfrBrowser.Create(AnOwner: TComponent; TextContent: string);
  72. begin
  73.    inherited Create(AnOwner);
  74.  
  75.    FTextContent := TextContent;
  76. end;
  77.  
  78. procedure TfrBrowser.FormShow(Sender: TObject);
  79. begin
  80.   if GlobalWebView2Loader.InitializationError then
  81.     showmessage(UTF8Encode(GlobalWebView2Loader.ErrorMessage))
  82.    else
  83.     if GlobalWebView2Loader.Initialized then begin
  84.       WVBrowser1.CreateBrowser(WVWindowParent1.Handle);
  85.     end
  86.     else begin
  87.       showmessage('initialization failed');
  88.       exit;
  89.     end;
  90. end;
  91.  
  92. procedure TfrBrowser.FormResize(Sender: TObject);
  93. begin
  94.     WVBrowser1.NavigateToString(fTextContent);
  95. end;
  96.  
  97. procedure TfrBrowser.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  98. begin
  99.    CloseAction := caFree;
  100. end;
  101.  
  102. procedure TfrBrowser.WMMove(var aMessage : TWMMove);
  103. begin
  104.   inherited;
  105.  
  106.   if (WVBrowser1 <> nil) then
  107.     WVBrowser1.NotifyParentWindowPositionChanged;
  108. end;
  109.  
  110. procedure TfrBrowser.WMMoving(var aMessage : TMessage);
  111. begin
  112.   inherited;
  113.  
  114.   if (WVBrowser1 <> nil) then
  115.     WVBrowser1.NotifyParentWindowPositionChanged;
  116. end;
  117.  
  118.  
  119. initialization
  120.    GlobalWebView2Loader                := TWVLoader.Create(nil);
  121.    GlobalWebView2Loader.UserDataFolder := UTF8Decode(ExtractFileDir(Application.ExeName) + '\CustomCache');
  122.    GlobalWebView2Loader.StartWebView2;
  123.  
  124. end.                          

Problems are,

1)  WVBrowser executes well like TWVBrowser.Navigate or TWVBrowser.NavigateToString well, ONLY WHEN its defaultURL has been assigned. If it was left blank, I cannot open any page programmatically.

2) If the contents of FormShow is moved to FormCreate, they are not executed correctly. I can put them within OnformActivate.

3) When this Window is opened, it displays the content of WVBrowser.defaultURL.  I assigned FormResize event, and when I resize the Window, the content is replaced with ts. However, I cannot call this PROGRAMMATICALLY.


Waiting for advices.
« Last Edit: November 03, 2025, 03:27:43 am by egsuh »

egsuh

  • Hero Member
  • *****
  • Posts: 1694
Re: WebView4Delphi question
« Reply #1 on: November 02, 2025, 08:30:33 am »
I do not fully understand the mechanism but I found a simplesolution. The content should be defined before the creation of WVBrowser. Windows Message must be playing some role, which I do not understand.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.    { TfrBrowser }
  4.  
  5.    TfrBrowser = class(TForm)
  6.       WVBrowser1: TWVBrowser;
  7.       WVWindowParent1: TWVWindowParent;
  8.       procedure Button1Click(Sender: TObject);
  9.       procedure FormActivate(Sender: TObject);
  10.       procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  11.       procedure WVBrowser1AfterCreated(Sender: TObject);
  12.    private
  13.       FTextContent: string;
  14.  
  15.    public
  16.       property HTMLText : string write FTextContent;
  17.       procedure Initialize;
  18.  
  19.    protected
  20.       procedure WMMove(var aMessage: TWMMove);  message WM_MOVE;
  21.       procedure WMMoving(var aMessage: TMessage); message WM_MOVING;
  22.    end;
  23.  
  24. var
  25.    frBrowser: TfrBrowser;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. procedure TfrBrowser.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  32. begin
  33.    CloseAction := caFree;
  34. end;
  35.  
  36. procedure TfrBrowser.WVBrowser1AfterCreated(Sender: TObject);
  37. begin
  38.     WVWindowParent1.UpdateSize;
  39.     Caption := 'View responses';
  40.     WVBrowser1.NavigateToString(Utf8Decode(FTextContent));  // I don't know why but this should be positioned here
  41. end;
  42.  
  43. procedure TfrBrowser.Initialize;
  44. begin
  45.    if GlobalWebView2Loader.InitializationError then
  46.       showmessage(UTF8Encode(GlobalWebView2Loader.ErrorMessage))
  47.    else
  48.       if GlobalWebView2Loader.Initialized then begin
  49.          WVBrowser1.CreateBrowser(WVWindowParent1.Handle);
  50.       end
  51.       else begin
  52.          showmessage('initialization failed');
  53.          exit;
  54.       end;
  55. end;
  56.  
  57. procedure TfrBrowser.Button1Click(Sender: TObject);
  58. begin
  59.    // WVBrowser1.Navigate('http://www.alphaq.biz');
  60.    WVBrowser1.NavigateToString(UTF8Decode('가설라무네'));           // Once initialized, this works well.
  61. end;
  62.  
  63. procedure TfrBrowser.FormActivate(Sender: TObject);
  64. begin
  65.     Initialize;
  66. end;
  67.  
  68. procedure TfrBrowser.WMMove(var aMessage : TWMMove);
  69. begin
  70.   inherited;
  71.  
  72.   if (WVBrowser1 <> nil) then
  73.     WVBrowser1.NotifyParentWindowPositionChanged;
  74. end;
  75.  
  76. procedure TfrBrowser.WMMoving(var aMessage : TMessage);
  77. begin
  78.   inherited;
  79.  
  80.   if (WVBrowser1 <> nil) then
  81.       WVBrowser1.NotifyParentWindowPositionChanged;
  82. end;
  83.  
  84. initialization
  85.    GlobalWebView2Loader                := TWVLoader.Create(nil);
  86.    GlobalWebView2Loader.UserDataFolder := UTF8Decode(ExtractFileDir(Application.ExeName) + '\CustomCache');
  87.    GlobalWebView2Loader.StartWebView2;
  88. end.
  89.  

and calling part:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.    ts: string;
  4. begin
  5.    ts := 'Good morning';
  6.  
  7.    with TfrBrowser.Create(nil) do begin  // this creates Pop-up form
  8.       HTMLText := ts;                            // the HTML content to display is assigned first.
  9.       ShowModal;                                  // then call FormActivate.
  10.    end;
  11. end;
  12.  

It's strange that TWVBrowser is a component dropped from component pallette, but it should be created within the program.

egsuh

  • Hero Member
  • *****
  • Posts: 1694
Re: [CLOSED] WebView4Delphi question
« Reply #2 on: November 03, 2025, 03:33:07 am »
This component displays contents, but takes long sometimes --- several seconds. But still seems this is rather tricky to use --- it does not respond to every event handler. It seems that the operation has something to do with Windows messages.

Anyway I prefer this to CEF4 because this needs only one file, WebView2Loader.dll for distribution.


egsuh

  • Hero Member
  • *****
  • Posts: 1694
Re: [CLOSED] WebView4Delphi question
« Reply #3 on: Today at 06:00:34 am »
One thing I think I found --- the components needs to be focused to operate correctly. In my case, the TFrame that contains TWVBrowser and TWVWindowParent should be focused before any contents are provided.

 

TinyPortal © 2005-2018