Files
WebUITest-RealProjecT/Handler/Project/Dialog/fWebView.cs
2025-11-25 20:14:41 +09:00

208 lines
6.6 KiB
C#

using System;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
namespace Project.Dialog
{
public partial class fWebView : Form
{
private WebView2 webView2;
private TextBox txtUrl;
private Button btnGo;
private Button btnBack;
private Button btnForward;
private Button btnRefresh;
public fWebView()
{
InitializeComponent();
InitializeWebView();
}
private void InitializeComponent()
{
this.SuspendLayout();
// Form
this.ClientSize = new System.Drawing.Size(1200, 800);
this.Text = "WebView2 Browser";
this.Name = "fWebView";
// URL TextBox
this.txtUrl = new TextBox();
this.txtUrl.Location = new System.Drawing.Point(100, 10);
this.txtUrl.Size = new System.Drawing.Size(900, 25);
this.txtUrl.KeyDown += TxtUrl_KeyDown;
// Go Button
this.btnGo = new Button();
this.btnGo.Location = new System.Drawing.Point(1010, 10);
this.btnGo.Size = new System.Drawing.Size(70, 25);
this.btnGo.Text = "Go";
this.btnGo.Click += BtnGo_Click;
// Back Button
this.btnBack = new Button();
this.btnBack.Location = new System.Drawing.Point(10, 10);
this.btnBack.Size = new System.Drawing.Size(25, 25);
this.btnBack.Text = "◀";
this.btnBack.Click += BtnBack_Click;
// Forward Button
this.btnForward = new Button();
this.btnForward.Location = new System.Drawing.Point(40, 10);
this.btnForward.Size = new System.Drawing.Size(25, 25);
this.btnForward.Text = "▶";
this.btnForward.Click += BtnForward_Click;
// Refresh Button
this.btnRefresh = new Button();
this.btnRefresh.Location = new System.Drawing.Point(70, 10);
this.btnRefresh.Size = new System.Drawing.Size(25, 25);
this.btnRefresh.Text = "⟳";
this.btnRefresh.Click += BtnRefresh_Click;
// WebView2
this.webView2 = new WebView2();
this.webView2.Location = new System.Drawing.Point(10, 45);
this.webView2.Size = new System.Drawing.Size(1170, 740);
this.Controls.Add(this.txtUrl);
this.Controls.Add(this.btnGo);
this.Controls.Add(this.btnBack);
this.Controls.Add(this.btnForward);
this.Controls.Add(this.btnRefresh);
this.Controls.Add(this.webView2);
this.ResumeLayout(false);
}
private async void InitializeWebView()
{
try
{
await webView2.EnsureCoreWebView2Async(null);
// 네비게이션 이벤트 핸들러
webView2.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
webView2.CoreWebView2.SourceChanged += CoreWebView2_SourceChanged;
// 기본 페이지 로드
webView2.CoreWebView2.Navigate("http://localhost:3000");
txtUrl.Text = "http://localhost:3000";
}
catch (Exception ex)
{
MessageBox.Show($"WebView2 초기화 실패: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CoreWebView2_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
{
txtUrl.Text = webView2.Source.ToString();
}
private void CoreWebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
btnBack.Enabled = webView2.CanGoBack;
btnForward.Enabled = webView2.CanGoForward;
}
private void BtnGo_Click(object sender, EventArgs e)
{
NavigateToUrl();
}
private void TxtUrl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
NavigateToUrl();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
private void NavigateToUrl()
{
string url = txtUrl.Text;
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
{
url = "http://" + url;
}
try
{
webView2.CoreWebView2.Navigate(url);
}
catch (Exception ex)
{
MessageBox.Show($"페이지 로드 실패: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BtnBack_Click(object sender, EventArgs e)
{
if (webView2.CanGoBack)
{
webView2.GoBack();
}
}
private void BtnForward_Click(object sender, EventArgs e)
{
if (webView2.CanGoForward)
{
webView2.GoForward();
}
}
private void BtnRefresh_Click(object sender, EventArgs e)
{
webView2.Reload();
}
// JavaScript 실행 예제 메서드
public async void ExecuteScriptAsync(string script)
{
try
{
string result = await webView2.CoreWebView2.ExecuteScriptAsync(script);
MessageBox.Show($"실행 결과: {result}");
}
catch (Exception ex)
{
MessageBox.Show($"스크립트 실행 실패: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// C#에서 JavaScript로 메시지 전송
public void PostMessageToWeb(string message)
{
try
{
webView2.CoreWebView2.PostWebMessageAsString(message);
}
catch (Exception ex)
{
MessageBox.Show($"메시지 전송 실패: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// JavaScript에서 C#으로 메시지 수신
private void SetupWebMessageReceived()
{
webView2.CoreWebView2.WebMessageReceived += (sender, e) =>
{
string message = e.TryGetWebMessageAsString();
MessageBox.Show($"웹에서 받은 메시지: {message}");
};
}
}
}