PowerBuilder Using WebView2
Powerbuilder Using Microsoft Edge WebView2.
You must first install Microsoft Edge WebView2 for your computer. download from https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section.
WebView2 property https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.winforms.webview2?view=webview2-dotnet-1.0.1661.34
Code Example
unv_webview2_helper from nonvisualobject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
forward global type unv_webview2_helper from nonvisualobject end type end forward global type unv_webview2_helper from nonvisualobject autoinstantiate end type forward prototypes public function integer of_createwebview2 (ux_webview2 auo_webview2) end prototypes public function integer of_createwebview2 (ux_webview2 auo_webview2);// TEST Int i_test = 1 Choose Case i_test Case 1 auo_webview2.CreateWebView('{& "userDataFolder": "" ,~r~n& "browserExecutableFolder" : "" , ~r~n & "environmentOptions" : "" ~r~n& }') Case 2 // TEST auo_webview2.CreateWebView('{& "userDataFolder": Null ,~r~n& "XXbrowserExecutableFolder" : "D:\\asc\\PBNI\\Microsoft.WebView2.FixedVersionRuntime.87.0.664.8.x86" , ~r~n & "environmentOptions" : "" ~r~n& }') End Choose Return 1 end function on unv_webview2_helper.create call super::create TriggerEvent( this, "constructor" ) end on on unv_webview2_helper.destroy TriggerEvent( this, "destructor" ) call super::destroy end on |
ux_webview2 from userobject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
forward global type ux_webview2 from userobject end type end forward global type ux_webview2 from userobject native "pbwebview2.pbx" public subroutine setdefaultuUrl(string defaultURL) public function string getDefaultUrl() event type int onclick() event type int ondoubleclick() public function string getBrowserVersionString() public function boolean CanGoBack() public function boolean CanGoForward() public function int CreateWebView(string config_JSON) public function string DocumentTitle() public function int Navigate(string uri) public function int NavigateEx(string uri, string method, string headers, string postdata) public function int NavigateToString(string htmlContent) public function int Reload() public function int Stop() public function int GoBack() public function int GoForward() public function int ExecuteScript(string executeScript) public function int ExecuteScript2(longlong callerID, string executeScript) public function int ExecuteScriptSync(longlong callerID, string executeScript, ref string result) event type int DocumentTitleChanged(string titleapp) event type int ExecuteScriptResult(string scriptresult) event type int ExecuteScript2Result(longlong callerID, string scriptResult) event type int NavigationStarting() event type int NavigationCompleted(boolean success, int webErrorStatus) event type int SourceChanged(string uri) event type int WebMessageReceived(string msg, string data) integer width = 400 integer height = 200 end type global ux_webview2 ux_webview2 on ux_webview2.create call super::create TriggerEvent( this, "constructor" ) end on on ux_webview2.destroy TriggerEvent( this, "destructor" ) call super::destroy end on |
w_webview2 from window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
forward global type w_webview2 from window end type type cb_2 from commandbutton within w_webview2 end type type cb_executescriptsync from commandbutton within w_webview2 end type type cb_new from commandbutton within w_webview2 end type type pb_1 from picturebutton within w_webview2 end type type sle_documenttitle from singlelineedit within w_webview2 end type type cb_goback from commandbutton within w_webview2 end type type cb_goforward from commandbutton within w_webview2 end type type cb_exec1 from commandbutton within w_webview2 end type type mle_statustext from multilineedit within w_webview2 end type type sle_url from singlelineedit within w_webview2 end type type cb_navigate from commandbutton within w_webview2 end type type uo_webview2 from ux_webview2 within w_webview2 end type end forward global type w_webview2 from window integer width = 4160 integer height = 1932 boolean titlebar = true string title = "PowerBuilder WebView2 " boolean controlmenu = true boolean minbox = true boolean maxbox = true boolean resizable = true long backcolor = 67108864 string icon = "AppIcon!" boolean center = true cb_2 cb_2 cb_executescriptsync cb_executescriptsync cb_new cb_new pb_1 pb_1 sle_documenttitle sle_documenttitle cb_goback cb_goback cb_goforward cb_goforward cb_exec1 cb_exec1 mle_statustext mle_statustext sle_url sle_url cb_navigate cb_navigate uo_webview2 uo_webview2 end type global w_webview2 w_webview2 type variables longlong ill_callerID = 0 end variables forward prototypes public subroutine of_msg (string as_msg) public subroutine of_yield () end prototypes public subroutine of_msg (string as_msg);mle_statustext.Event ue_add ( as_msg ) end subroutine public subroutine of_yield ();//unv_yield lnv_yield //lnv_yield = CREATE unv_yield //lnv_yield.of_yield(Handle(uo_webview2)) //DESTROY( lnv_yield) Return end subroutine on w_webview2.create this.cb_2=create cb_2 this.cb_executescriptsync=create cb_executescriptsync this.cb_new=create cb_new this.pb_1=create pb_1 this.sle_documenttitle=create sle_documenttitle this.cb_goback=create cb_goback this.cb_goforward=create cb_goforward this.cb_exec1=create cb_exec1 this.mle_statustext=create mle_statustext this.sle_url=create sle_url this.cb_navigate=create cb_navigate this.uo_webview2=create uo_webview2 this.Control[]={this.cb_2,& this.cb_executescriptsync,& this.cb_new,& this.pb_1,& this.sle_documenttitle,& this.cb_goback,& this.cb_goforward,& this.cb_exec1,& this.mle_statustext,& this.sle_url,& this.cb_navigate,& this.uo_webview2} end on on w_webview2.destroy destroy(this.cb_2) destroy(this.cb_executescriptsync) destroy(this.cb_new) destroy(this.pb_1) destroy(this.sle_documenttitle) destroy(this.cb_goback) destroy(this.cb_goforward) destroy(this.cb_exec1) destroy(this.mle_statustext) destroy(this.sle_url) destroy(this.cb_navigate) destroy(this.uo_webview2) end on event resize;uo_webview2.Resize(This.WorkSpaceWidth() - ( uo_webview2.X + 30 ),This.WorkSpaceHeight()-( uo_webview2.Y + 50 ) ) sle_documenttitle.Width = This.WorkSpaceWidth() - ( sle_documenttitle.X + 30 ) cb_new.X = This.WorkSpaceWidth() - ( cb_new.Width + 30 ) mle_statustext.Height = This.WorkSpaceHeight()-( mle_statustext.Y + 50 ) end event type cb_2 from commandbutton within w_webview2 integer x = 37 integer y = 444 integer width = 951 integer height = 112 integer taborder = 40 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "getVersion" end type event clicked;String ls_result ls_result = uo_webview2.getBrowserVersionString() of_msg ( "Version:" + ls_result) end event type cb_executescriptsync from commandbutton within w_webview2 integer x = 37 integer y = 320 integer width = 951 integer height = 96 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Test ExecuteScriptSync" end type event clicked;Int li_ret String ls_result ill_callerID ++ li_ret = uo_webview2.ExecuteScriptSync (ill_callerID , "String( 4 + 1)" , ls_result) of_msg ( String(ill_callerID) + ":" + String (li_ret) + ":" + ls_result) ill_callerID ++ li_ret = uo_webview2.ExecuteScriptSync (ill_callerID , "String( 4 * 13)", ls_result) of_msg ( String(ill_callerID) + ":" + String (li_ret) + ":" + ls_result) ill_callerID ++ li_ret = uo_webview2.ExecuteScriptSync (ill_callerID , "(new Date()).getTime();", ls_result) of_msg ( String(ill_callerID) + ":" + String (li_ret) + ":" + ls_result) ill_callerID ++ li_ret = uo_webview2.ExecuteScriptSync (ill_callerID , "new Array(123, new Date(), 'Appeon', 2 > 1)", ls_result) of_msg ( String(ill_callerID) + ":" + String (li_ret) + ":" + ls_result) ill_callerID ++ li_ret = uo_webview2.ExecuteScriptSync (ill_callerID , 'window.chrome.webview.postMessage("GetWindowBounds");', ls_result) of_msg ( String(ill_callerID) + ":" + String (li_ret) + ":" + ls_result) ill_callerID ++ li_ret = uo_webview2.ExecuteScriptSync (ill_callerID , 'windw.chrome.webview.postMessage("GendowBounds");', ls_result) of_msg ( String(ill_callerID) + ":" + String (li_ret) + ":" + ls_result) end event type cb_new from commandbutton within w_webview2 integer x = 3977 integer y = 32 integer width = 119 integer height = 112 integer taborder = 40 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "+" end type event clicked;window lw_a Open (lw_a, "w_webview2") end event type pb_1 from picturebutton within w_webview2 integer x = 2555 integer y = 32 integer width = 137 integer height = 116 integer taborder = 30 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string picturename = "Update5!" alignment htextalign = left! end type event clicked;uo_webview2.reload() end event type sle_documenttitle from singlelineedit within w_webview2 integer x = 1038 integer y = 184 integer width = 3058 integer height = 112 integer taborder = 30 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 long backcolor = 67108864 boolean displayonly = true borderstyle borderstyle = stylelowered! end type type cb_goback from commandbutton within w_webview2 integer x = 2313 integer y = 32 integer width = 101 integer height = 112 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" boolean enabled = false string text = "<" end type event clicked;uo_webview2.goback() end event type cb_goforward from commandbutton within w_webview2 integer x = 2432 integer y = 32 integer width = 101 integer height = 112 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" boolean enabled = false string text = ">" end type event clicked;uo_webview2.goForward() end event type cb_exec1 from commandbutton within w_webview2 integer x = 37 integer y = 184 integer width = 951 integer height = 112 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Test ExecuteScript" end type event clicked;Int li_ret ill_callerID ++ li_ret = uo_webview2.ExecuteScript2 (ill_callerID , "String( 4 + 1)") of_msg ( String(ill_callerID) + ":" + String (li_ret)) ill_callerID ++ li_ret = uo_webview2.ExecuteScript2 (ill_callerID , "String( 4 * 13)") of_msg ( String(ill_callerID) + ":" + String (li_ret)) ill_callerID ++ li_ret = uo_webview2.ExecuteScript2 (ill_callerID , "(new Date()).getTime();") of_msg ( String(ill_callerID) + ":" + String (li_ret)) ill_callerID ++ li_ret = uo_webview2.ExecuteScript2 (ill_callerID , "new Array(123, new Date(), 'Appeon', 2 > 1)") of_msg ( String(ill_callerID) + ":" + String (li_ret)) ill_callerID ++ li_ret = uo_webview2.ExecuteScript2 (ill_callerID , "ne Aaray(123, new Date(), 'Appeon', 2 > X)") of_msg ( String(ill_callerID) + ":" + String (li_ret)) end event type mle_statustext from multilineedit within w_webview2 event ue_add ( string as_msg ) integer x = 37 integer y = 576 integer width = 951 integer height = 1216 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 borderstyle borderstyle = stylelowered! end type event ue_add(string as_msg);This.Text = as_msg + "~r~n" + This.Text end event type sle_url from singlelineedit within w_webview2 integer x = 37 integer y = 32 integer width = 1847 integer height = 100 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 string text = "https://pblib.com" borderstyle borderstyle = stylelowered! end type event constructor;//this.text = 'file://D:\asc\PBNI\html\Example1.html' end event type cb_navigate from commandbutton within w_webview2 integer x = 1915 integer y = 32 integer width = 329 integer height = 104 integer taborder = 10 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Navigate" end type event clicked;Integer li_rc of_msg ( "Fct: Call Navigate()") li_rc = uo_webview2.Navigate( sle_url.Text) of_msg ( "Fct: Navigate() returned:" + String (li_rc)) end event type uo_webview2 from ux_webview2 within w_webview2 integer x = 1038 integer y = 320 integer width = 3058 integer height = 1464 integer taborder = 10 boolean border = true borderstyle borderstyle = stylelowered! end type on uo_webview2.destroy call ux_webview2::destroy end on event ondoubleclick;call super::ondoubleclick;MessageBox("DoubleClicked!","") Return 1 end event event webmessagereceived;call super::webmessagereceived;of_msg(msg) of_msg(Data) Return 1 end event event executescriptresult;call super::executescriptresult;of_msg(scriptresult) Return 1 end event event documenttitlechanged;call super::documenttitlechanged;sle_documenttitle.Text = Title Return 1 end event event sourcechanged;call super::sourcechanged;of_msg ("Evt: SourceChanged") sle_url.Text = uri cb_goback.Enabled = This.CanGoBack() cb_goforward.Enabled = This.CanGoForward() Return 0 end event event navigationstarting;call super::navigationstarting;Post of_msg("Evt: NavigationStarting") Return 1 end event event navigationcompleted;call super::navigationcompleted; of_msg("Evt: NavigationCompleted " + String (success) + " - " + String (weberrorstatus)) sle_documenttitle.Text = This.DocumentTitle() Return 1 end event event constructor;call super::constructor; Integer li_rc of_msg ( "Fct: Call createwebview2()") li_rc = CreateWebView('{& "userDataFolder": "" ,~r~n& "browserExecutableFolder" : "" , ~r~n & "environmentOptions" : "" ~r~n& }') of_msg ( "Fct: createwebview2 () returned:" + String (li_rc)) end event event executescript2result;call super::executescript2result;of_msg(String(callerid) + ":" + scriptresult) Return 1 end event event onclick;call super::onclick;//MessageBox("TEST","TEST") Return 1 end event |
Source get from internet
Here is the source code PBNI105. Can upgrade and build for other PBNI versions. WebView2 Extension for PowerBuilder click here
Find Projects On Github click here
Good Luck!
Thanks for grabbing my code 😉
I have a friend who shared this code with me. I found it very useful so I shared it again. If any problem please contact me. I will remove this post if you find it not good.
Hi and thanks. It was really helpful!!