04-12-2025, 03:35 PM
Call JS from C#.
I haven't used raw C# strings for this code, I've just modified the HTML from the file in the Demos repository. Introducing SetSize() and SetPosition()
console_call_js_from_csharp:
console_call_js_from_csharp_events:
I haven't used raw C# strings for this code, I've just modified the HTML from the file in the Demos repository. Introducing SetSize() and SetPosition()
console_call_js_from_csharp:
// script "console_call_js_from_csharp.cs"
/*/ nuget wui\WebUI4CSharp; nuget wui\WebUI4CSharp.Natives; c console_call_js_from_csharp_events.cs; /*/
using WebUI4CSharp;
using System.Windows.Forms;
using System.Threading;
script.setup(trayIcon: true, sleepExit: true);
string my_html = "<!DOCTYPE html>" +
"<html>" +
" <head>" +
" <meta charset=\"UTF-8\">" +
" <script src=\"webui.js\"></script>" +
" <title>Call JavaScript from C# Example</title>" +
" <style>" +
" body {" +
" font-family: 'Arial', sans-serif;" +
" color: white;" +
" background: linear-gradient(to right, #507d91, #1c596f, #022737);" +
" text-align: center;" +
" font-size: 18px;" +
" }" +
" button, input {" +
" padding: 10px;" +
" margin: 10px;" +
" border-radius: 3px;" +
" border: 1px solid #ccc;" +
" box-shadow: 0 3px 5px rgba(0,0,0,0.1);" +
" transition: 0.2s;" +
" }" +
" button {" +
" background: #3498db;" +
" color: #fff; " +
" cursor: pointer;" +
" font-size: 16px;" +
" }" +
" h1 { text-shadow: -7px 10px 7px rgb(67 57 57 / 76%); }" +
" button:hover { background: #c9913d; }" +
" input:focus { outline: none; border-color: #3498db; }" +
" </style>" +
" </head>" +
" <body>" +
" <h1>WebUI - Call JavaScript from C#</h1>" +
" <br>" +
" <h1 id=\"count\">0</h1>" +
" <br>" +
" <button OnClick=\"my_function_count();\">Manual Count</button>" +
" <br>" +
" <button id=\"MyTest\" OnClick=\"AutoTest();\">Auto Count (Every 500ms)</button>" +
" <br>" +
" <button OnClick=\"my_function_exit();\">Exit</button>" +
" <script>" +
" let count = 0;" +
" function GetCount() {" +
" return count;" +
" }" +
" function SetCount(number) {" +
" document.getElementById('count').innerHTML = number;" +
" count = number;" +
" }" +
" function AutoTest(number) {" +
" setInterval(function(){ my_function_count(); }, 500);" +
" }" +
" </script>" +
" </body>" +
"</html>";
WebUIWindow window = new WebUIWindow();
window.Bind("my_function_count", WebUI_Events.my_function_count);
window.Bind("my_function_exit", WebUI_Events.my_function_exit);
window.SetSize(350, 550);
window.Show(my_html);
window.SetPosition(100, 100);
wait.ms(2000);
window.SetPosition(900, 100);
wait.ms(2000);
window.SetPosition(100, 100);
WebUI.Wait();
WebUI.Clean();
console_call_js_from_csharp_events:
// class "console_call_js_from_csharp_events.cs"
/*/ nuget wui\WebUI4CSharp; nuget wui\WebUI4CSharp.Natives; /*/
using WebUI4CSharp;
/// <summary>
/// UI Events
/// </summary>
public static class WebUI_Events
{
/// <summary>
/// Exit function
/// </summary>
/// <param name="e"></param>
public static void my_function_exit(ref webui_event_t e)
{
// Close all opened windows
WebUI.Exit();
}
/// <summary>
/// count function
/// </summary>
/// <param name="e"></param>
public static void my_function_count(ref webui_event_t e)
{
// This function gets called every time the user clicks on "my_function_count"
WebUIEvent lEvent = new WebUIEvent(e);
#nullable enable
WebUIWindow? window = lEvent.Window;
#nullable disable
if (window != null)
{
string response;
if (!window.Script("return GetCount();", 0, out response, 64))
{
if (!window.IsShown())
{
Console.WriteLine("Window closed.");
}
else
{
Console.WriteLine("JavaScript Error: %s", response);
}
return;
}
// Get the count
int count = Convert.ToInt32(response);
// Increment
count++;
// Generate a JavaScript
string js = $"SetCount({count});";
// Run JavaScript
window.Run(js);
}
}
}