[C#]winform使⽤html做界⾯
DirectUI技术开发界⾯的好奇今天想到怎么不让winform也那样了,使⽤html 做 ui ,⼤家都知道使⽤div +css布局是⼀件⾮常容易的事情;光说不练是假打,所以花了⼀个把⼩时尝试把winform的界⾯做成 webui
⼀:
⼤家都知道 winform 中有个控件叫 webBrowser  如果你不知道具体怎么⽤  详细看msdn 都知道了;  这⾥的它就是主⾓;
准备:
现在 我们新建⼀个winform项⽬  在上⾯放⼀个webBrowser 名称默认  停靠⽗窗⼝  在新建⼀个html ⽹页 名称 html.htm
#region Windows 窗体设计器⽣成的代码
/// <summary>
/// 设计器⽀持所需的⽅法 - 不要
/// 使⽤代码编辑器修改此⽅法的内容。
/// </summary>
private void InitializeComponent()
{
thisponents = new System.ComponentModel.Container();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// moveContron1
//
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.IsWebBrowserContextMenuEnabled = false;
this.webBrowser1.Location = new System.Drawing.Point(2, 2);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.ScrollBarsEnabled = false;
用html和css制作百度页面
this.webBrowser1.Size = new System.Drawing.Size(375, 258);
this.webBrowser1.TabIndex = 1;
this.webBrowser1.Url = new System.Uri("file:///E:/visual%20studio/WebUi/WebUi/HTML.htm", System.UriKind.Absolute);            this.webBrowser1.WebBrowserShortcutsEnabled = false;
//
// MianWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(379, 262);
this.Controls.Add(this.webBrowser1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MianWindow";
this.Opacity = 0.9D;
this.Padding = new System.Windows.Forms.Padding(2);
this.Text = "Form_Test";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MianWindow_Paint);
this.ResumeLayout(false);
}
#endregion
private MoveControns.MoveContron moveContron1;
private System.Windows.Forms.WebBrowser webBrowser1;
html的内容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body >
<div>
<span οnmοusedοwn="al.WinApiMoveEvent()" οnmοuseοver="this.style.cursor =  'move';"
οnmοuseοut="this.style.cursor='default'">11111111</span> <span >
<input id="Button2" type="button" value="最⼤化" οnclick="max()" />
<input id="Button3" type="button" value="CloseMe" οnclick="closewin()" /></span>
</div>
<table border="1" cellspacing="0" cellpadding="0" width="100%" height="100%">
<tr>
<td align="right">
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
// 给Winform调⽤的⽅法
function ShowAlert(msg) {
alert(msg);
}
function closewin() {
}
function max() {
var val = ElementById('Button2').value;
if (val = '最⼤化') {
}
else {
};
}
</script>
为了 使⽹页能够 与winform 交互  所以  的把 com 的看见⾏ 设置为真
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)] // 将该类设置为com可访问
public partial class MianWindow : GlassForm
{}
为了 更好看 我们把 winform的 边框 去掉
这⾥ 的代码 是 处理  移动窗⼝ 我们使⽤ winApi
#region win32 api 移动窗体
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
this.MouseDown += new MouseEventHandler(WinApiMoveEvent);
public void WinApiMoveEvent() //这⾥重载了主要解决参数问题
{
ReleaseCapture();
SendMessage(dropForm.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
public void WinApiMoveEvent(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(dropForm.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
#endregion
现在 演⽰ 怎么让js 调⽤ winform的⽅法
/// <summary>
/
// 给WebBrowser中Web的JS调⽤的⽅法关闭⾃⼰
/// </summary>
/// <param name="msg"></param>
public void ShowMsg(string msg)
{
}
void Form1_AeroGlassCompositionChanged(object sender, AeroGlassCompositionChangedEventArgs e)        {
// When the desktop composition mode changes the window exclusion must be changed appropriately.            if (e.GlassAvailable)
{
Invalidate();
}
else
{
this.BackColor = Color.Teal;
}
}
public void CloseMe()
{
if (MessageBox.Show("你确定要关闭吗?", "提⽰", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Close();
}
}
///最⼤化和最⼩化的处理
public void MaxWin()
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);                this.WindowState = FormWindowState.Maximized;
}
}
如果是winform 要调⽤ web中的js 看这⾥
webBrowser1⾥有 document. InvokeScript("这⾥是js 的函数名", 这⾥是参数);
现在 基本完成  ,
本⼈ 不善表达  如有不明⽩ 这⾥下源码看

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。