GeckoFX 在winform中的简单应用

1、创建winfrom工程-过程省略
2、在NuGet程序包管理器里添加GeckoFX相关引用DLL

3、在默认窗口中添加以下代码(默认窗口放了一个进度条两个操作跳转网页按钮和一个pannel)

  1. GeckoWebBrowser browser;
  2. public Form1()
  3. {
  4. InitializeComponent();
  5. StartPosition = FormStartPosition.CenterScreen;
  6. Xpcom.Initialize("Firefox");
  7. browser = new GeckoWebBrowser();
  8. this.browser.Name = "browser";
  9. GeckoPreferences.User["gfx.font_rendering.graphite.enabled"] = true;//设置偏好:字体
  10. GeckoPreferences.User["privacy.donottrackheader.enabled"] = true;//设置浏览器不被追踪
  11. GeckoPreferences.User["intl.accept_languages"] = "zh-CN,zh;q=0.9,en;q=0.8";//不设置的话默认是英文区
  12. //GeckoPreferences.User["permissions.default.image"] = 2; // block image 禁止加载图片
  13. //GeckoPreferences.User["plugin.state.flash"] = 0; // bloack flash禁止加载flash
  14. browser.CreateControl();
  15. browser.NoDefaultContextMenu = true; //禁用右键菜单
  16. browser.Dock = DockStyle.Fill;
  17. panel1.Controls.Add(browser);
  18. browser.ProgressChanged += Gecko_ProgressChanged;
  19. browser.CreateWindow += Gecko_CreateWindow;//打开新窗口事件,全部设为在同一窗口打开
  20. browser.DocumentCompleted += Gecko_DocumentCompleted;
  21. //browser.UseHttpActivityObserver = true;//开启拦截请求
  22. //browser.ObserveHttpModifyRequest += Browser_ObserveHttpModifyRequest;//拦截请求(在创建窗口之前就拦截。)
  23. 同时取消创建创建,在主窗口打开
  24. browser.Navigate("http://www.baidu.com");
  25. //browser.Navigate("http://127.0.0.1:8848/Html/login/login.html");
  26. //browser.Navigate("https://www.bilibili.com/");
  27. }
  28. private void Gecko_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
  29. {
  30. var executor = new Gecko.JQuery.JQueryExecutor(browser.Window); //先获取到jquery对象
  31. executor.ExecuteJQuery("$('#a')"); //然后执行jquery的代码
  32. using (AutoJSContext context = new AutoJSContext(browser.Window))
  33. {
  34. string result;
  35. context.EvaluateScript("3 + 2;", out result);
  36. context.EvaluateScript("'hello' + ' ' + 'world';", out result);
  37. }
  38. progressBar1.Value = 0;
  39. }
  40. //当前窗口跳转网址
  41. private void Gecko_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
  42. {
  43. try
  44. {
  45. browser.Navigate(e.Uri);
  46. e.Cancel = true;
  47. }
  48. catch(Exception ex)
  49. {
  50. MessageBox.Show(ex.Message);
  51. }
  52. //e.InitialHeight = 500;
  53. //e.InitialWidth = 500;
  54. }
  55. private void Gecko_ProgressChanged(object sender, GeckoProgressEventArgs e)
  56. {
  57. if (e.MaximumProgress == 0)
  58. return;
  59.  
  60. var value = (int)Math.Min(100, (e.CurrentProgress * 100) / e.MaximumProgress);
  61. if (value == 100)
  62. return;
  63. progressBar1.Value = value;
  64. }
  65.  
  66. private void btn_GoBack_Click(object sender, EventArgs e)
  67. {
  68. browser.GoBack();
  69. }
  70.  
  71. private void btn_GoPreview_Click(object sender, EventArgs e)
  72. {
  73. browser.GoForward();
  74. }

4、效果

winform中带有默认布局器Dock可以对控件进行简单的布局,但是,在布局过程中,控件的布局效果与控件的添加顺序有着密切的关系。
先添加button1(left),再添加button2(fill)

先添加button1(fill),再添加button2(left)

可见布局效果存在巨大差异,这就使得Dock布局器只适合进行简单的布局,在这里介绍一种布局器,可以消除布局顺序对布局效果的影响,同时可以自己进行四角的设定。


代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Design;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.Design;
  11.  
  12. namespace OnShowTime
  13. {
  14. public class DockLayout : Panel
  15. {
  16. // 左、上、右、下
  17. private int[] dockFlags = new int[8];
  18.  
  19. protected override void OnLayout(LayoutEventArgs levent)
  20. {
  21. base.OnLayout(levent);
  22. int w = this.Width;
  23. int h = this.Height;
  24.  
  25. // 考虑容器本身的 Padding
  26. Padding pad = this.Padding;
  27. w -= (pad.Left + pad.Right);
  28. h -= (pad.Top + pad.Bottom);
  29.  
  30. DockControl dcTop = new DockControl();
  31. DockControl dcRight = new DockControl();
  32. DockControl dcBottom = new DockControl();
  33. DockControl dcLeft = new DockControl();
  34. Control center = null; // 
  35. // 判断4个边的宽度
  36. foreach (Control c in this.Controls)
  37. {
  38. Padding m = c.Margin; // 须考虑Margin设置
  39. // 左
  40. if (c.Dock == DockStyle.Left && c.Visible)
  41. {
  42. DockControl dc = dcLeft; ;
  43. dc.c = c;
  44. dc.size = c.Width;
  45. dc.size += (m.Left + m.Right);
  46. dc.flag1 = dockFlags[0];
  47. dc.flag2 = dockFlags[1];
  48. }
  49. if (c.Dock == DockStyle.Top && c.Visible)
  50. {
  51. DockControl dc = dcTop; ;
  52. dc.c = c;
  53. dc.size = c.Height;
  54. dc.size += (m.Top + m.Bottom);
  55. dc.flag1 = dockFlags[2];
  56. dc.flag2 = dockFlags[3];
  57. }
  58. if (c.Dock == DockStyle.Right && c.Visible)
  59. {
  60. DockControl dc = dcRight; ;
  61. dc.c = c;
  62. dc.size = c.Width;
  63. dc.size += (m.Left + m.Right);
  64. dc.flag1 = dockFlags[4];
  65. dc.flag2 = dockFlags[5];
  66. }
  67. if (c.Dock == DockStyle.Bottom && c.Visible)
  68. {
  69. DockControl dc = dcBottom; ;
  70. dc.c = c;
  71. dc.size = c.Height;
  72. dc.size += (m.Top + m.Bottom);
  73. dc.flag1 = dockFlags[6];
  74. dc.flag2 = dockFlags[7];
  75. }
  76. if (c.Dock == DockStyle.Fill && c.Visible)
  77. {
  78. center = c;
  79. }
  80. }
  81.  
  82. // 依次布局
  83. if (dcLeft.c != null)
  84. {
  85.  
  86. DockControl dc = dcLeft;
  87. int x1 = 0, y1 = 0;
  88. int x2 = dc.size, y2 = h;
  89. if (dc.flag1 == 0)
  90. y1 += dcTop.size;
  91. if (dc.flag2 == 0)
  92. y2 -= dcBottom.size;
  93. SetSizeLocation(dc.c, x1, y1, x2, y2);
  94. }
  95. if (dcTop.c != null)
  96. {
  97. DockControl dc = dcTop;
  98. int x1 = 0, y1 = 0;
  99. int x2 = w, y2 = dc.size;
  100. if (dc.flag1 == 0)
  101. x1 += dcLeft.size;
  102. if (dc.flag2 == 0)
  103. x2 -= dcRight.size;
  104. SetSizeLocation(dc.c, x1, y1, x2, y2);
  105. }
  106. if (dcRight.c != null)
  107. {
  108. DockControl dc = dcRight;
  109. int x1 = w - dc.size, y1 = 0;
  110. int x2 = w, y2 = h;
  111. if (dc.flag1 == 0)
  112. y1 += dcTop.size;
  113. if (dc.flag2 == 0)
  114. y2 -= dcBottom.size;
  115. SetSizeLocation(dc.c, x1, y1, x2, y2);
  116. }
  117. if (dcBottom.c != null)
  118. {
  119. DockControl dc = dcBottom;
  120. int x1 = 0, y1 = h - dc.size;
  121. int x2 = w, y2 = h;
  122. if (dc.flag1 == 0)
  123. x1 += dcLeft.size;
  124. if (dc.flag2 == 0)
  125. x2 -= dcRight.size;
  126. SetSizeLocation(dc.c, x1, y1, x2, y2);
  127. }
  128. if (center != null)
  129. {
  130. int x1 = dcLeft.size, y1 = dcTop.size;
  131. int x2 = w - dcRight.size, y2 = h - dcBottom.size;
  132. SetSizeLocation(center, x1, y1, x2, y2);
  133. //Console.WriteLine("center{0},{1},{2},{3}", x1, y1, x2, y2);
  134. }
  135. }
  136.  
  137. public void SetSizeLocation(Control c, int x1, int y1, int x2, int y2)
  138. {
  139. // 控件的布局尺寸 (包含了控件的Margin)
  140. int width = x2 - x1, height = y2 - y1;
  141.  
  142. // 容器本身的 Padding
  143. Padding pad = this.Padding;
  144. x1 += pad.Left;
  145. y1 += pad.Top;
  146.  
  147. // 考虑控件本身的 Margin
  148. Padding margin = c.Margin;
  149. x1 += margin.Left;
  150. y1 += margin.Top;
  151. width -= (margin.Left + margin.Right);
  152. height -= (margin.Top + margin.Bottom);
  153.  
  154. c.Location = new Point(x1, y1);
  155. c.Size = new Size(width, height);
  156. }
  157.  
  158. public class DockControl
  159. {
  160. public Control c;
  161. public int size;
  162. public int flag1 = 0; // 1占据 0 退让
  163. public int flag2 = 0;
  164. }
  165.  
  166.  
  167. [Browsable(true)]
  168. [Editor(typeof(DockFlagEditorType), typeof(UITypeEditor))]
  169. public int[] DockFlags
  170. {
  171. get
  172. {
  173. return dockFlags;
  174. }
  175. set
  176. {
  177. this.dockFlags = value;
  178. PerformLayout();
  179. }
  180. }
  181. }
  182.  
  183. ///
  184.  
  185. /// 按照Winform框架的要求,定义一个属性编辑器器
  186. ///
  187. 
    pythonpythoneyJ1cGRhdGUiOiB0cnVlLCAidmVyc2lvbiI6IDYuMywgInVwY29udGVudCI6ICJcdTRmZWVcdTU5MGRcdTUyMDZcdTRlYWJcdTYyNGJcdTY3M2FBUFBcdTc2ODRcdTk0ZmVcdTYzYTVcdThmYzdcdTY3MWZcdTk1ZWVcdTk4OThcdWZmMGNcdTUzNDdcdTdlYTdcdTY3MDlcdTk1ZWVcdTk4OThcdThiZjdcdTUyYTBcdTVmYWVcdTRmZTFcdWZmMWFzeHN1Y2Nlc3MiLCAidXB1cmwiOiAiaHR0cHM6Ly93d3gubGFuem91aS5jb20vaUtJSTl3OHpjcmUiLCAiYXBwdXJsIjogImh0dHBzOi8vd3d4LmxhbnpvdWkuY29tL2lwZFF3dzhiamxnIiwgInNob3dtZXNzYWdlIjogZmFsc2UsICJtZXNzYWdlIjogIjExXHU2NzA4XHU1MjA2XHU0ZWFiXHU0ZTI0XHU0ZTJhXHU5MDgwXHU4YmY3XHU3ODAxXHVmZjFhXHUzMDEwMmY2YjY1OTY0NmYqY2QwNlx1MzAxMVx1MzAxMDFjZDBjYWFhOSpjYmIzNDhcdTMwMTFcdTk2OTBcdTg1Y2ZcdTUxNzZcdTRlMmRcdTRlMDBcdTRmNGRcdWZmMGNcdTRlNWZcdTUzZWZcdTRlZTVcdTc2ZjRcdTYzYTVcdThkMmRcdTRlNzBcdTU0ZTYiLCAiaGVhZGVycyI6ICIvaW5kZXgucGhwP3U9NTg1MDk4JmV4dD1iYTJkMzsvaW5kZXgucGhwP3U9NTg5NTY5JmV4dD1iZjdmNjsiLCAiYWJvdXQiOiAiMTAyNFx1ODAwMVx1NTNmOFx1NjczYVx1NWUyNlx1NGY2MFx1NTZkZVx1NWJiNlx1NTU0YVx1ZmYwY1x1NGUwYVx1OGY2Nlx1OGJmN1x1NmVmNFx1NmVmNFx1NjIxMVx1ZmYxYTEwMjR4aWFvc2hlbkBnbWFpbC5jb20iLCAid2VpeGlueGluIjogInN4c3VjY2VzcyIsICJ3ZWlwaG90byI6ICJwaG90byIsICJtYXppbm90ZSI6ICJcdTk3MDBcdTg5ODFcdTkwODBcdThiZjdcdTc4MDFcdTYyNGRcdTUzZWZcdTRlZTVcdTZjZThcdTUxOGNcdTU0ZTYhIn0=pythonpython
    

class DockFlagEditorType : UITypeEditor { public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService editorService = null; if (context != null && context.Instance != null && provider != null) { editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (editorService != null) { // 自定义控件 DockLayout owner = (DockLayout)context.Instance; DockFlagEditor editorUi = new DockFlagEditor(); editorUi.SetValue(owner.DockFlags); //editorUi.Size = editorUi.grid.PreferredSize; // 显示 (阻塞方式,直到界面关闭) editorService.DropDownControl(editorUi); // 新的值 int[] newValue = editorUi.GetValue(); //owner.Partition = newPartition; return newValue; } } return value; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } } ///

  1. /// 定义一个编辑器,将出现在属性面板里
  2. ///

[ToolboxItem(false)] class DockFlagEditor : UserControl { private DataGridViewTextBoxColumn Column1; private DataGridViewTextBoxColumn Column2; private DataGridViewTextBoxColumn Column3; public DataGridView grid; private static string T = “✔”; private static string F = “☐”; public DockFlagEditor() { InitializeComponent(); grid.Rows.Add(new object[] { “左 Left”, F, F }); grid.Rows.Add(new object[] { “上 Top”, F, F }); grid.Rows.Add(new object[] { “右 Right”, F, F }); grid.Rows.Add(new object[] { “下 Bottom”, F, F }); } private void InitializeComponent() { System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); this.grid = new System.Windows.Forms.DataGridView(); this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit(); this.SuspendLayout(); // // grid // this.grid.AllowUserToAddRows = false; this.grid.AllowUserToDeleteRows = false; this.grid.AllowUserToResizeColumns = false; this.grid.AllowUserToResizeRows = false; this.grid.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells; this.grid.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(252)))), ((int)(((byte)(252)))), ((int)(((byte)(252))))); this.grid.BorderStyle = System.Windows.Forms.BorderStyle.None; this.grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.grid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2, this.Column3}); this.grid.Dock = System.Windows.Forms.DockStyle.Fill; this.grid.Location = new System.Drawing.Point(3, 3); this.grid.Name = “grid”; this.grid.ReadOnly = true; this.grid.RowHeadersVisible = false; this.grid.RowTemplate.Height = 23; this.grid.Size = new System.Drawing.Size(225, 120); this.grid.TabIndex = 0; this.grid.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.grid_CellClick); // // Column1 // dataGridViewCellStyle4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(252)))), ((int)(((byte)(252)))), ((int)(((byte)(252))))); this.Column1.DefaultCellStyle = dataGridViewCellStyle4; this.Column1.HeaderText = “方位”; this.Column1.Name = “Column1”; this.Column1.ReadOnly = true; this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; this.Column1.Width = 80; // // Column2 // this.Column2.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.Column2.DefaultCellStyle = dataGridViewCellStyle5; this.Column2.FillWeight = 50F; this.Column2.HeaderText = “Flag1”; this.Column2.Name = “Column2”; this.Column2.ReadOnly = true; this.Column2.Resizable = System.Windows.Forms.DataGridViewTriState.True; this.Column2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column3 // this.Column3.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.Column3.DefaultCellStyle = dataGridViewCellStyle6; this.Column3.FillWeight = 50F; this.Column3.HeaderText = “Flag2”; this.Column3.Name = “Column3”; this.Column3.ReadOnly = true; this.Column3.Resizable = System.Windows.Forms.DataGridViewTriState.True; this.Column3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // AfDockFlagEditor // this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(252)))), ((int)(((byte)(252)))), ((int)(((byte)(252))))); this.Controls.Add(this.grid); this.Name = “AfDockFlagEditor”; this.Padding = new System.Windows.Forms.Padding(3); this.Size = new System.Drawing.Size(231, 126); ((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit(); this.ResumeLayout(false); } private bool IsTrue(string value) { return value.Equals(T); } private bool Cell(int col, int row) { return IsTrue((string)grid[col, row].Value); } public void Cell(int col, int row, bool value) { grid[col, row].Value = value ? T : F; } public void SetValue(int[] flags) { Cell(1, 0, flags[0] > 0); Cell(2, 0, flags[1] > 0); Cell(1, 1, flags[2] > 0); Cell(2, 1, flags[3] > 0); Cell(1, 2, flags[4] > 0); Cell(2, 2, flags[5] > 0); Cell(1, 3, flags[6] > 0); Cell(2, 3, flags[7] > 0); } public int[] GetValue() { int[] flags = new int[8]; flags[0] = Cell(1, 0) ? 1 : 0; flags[1] = Cell(2, 0) ? 1 : 0; flags[2] = Cell(1, 1) ? 1 : 0; flags[3] = Cell(2, 1) ? 1 : 0; flags[4] = Cell(1, 2) ? 1 : 0; flags[5] = Cell(2, 2) ? 1 : 0; flags[6] = Cell(1, 3) ? 1 : 0; flags[7] = Cell(2, 3) ? 1 : 0; return flags; } private void grid_CellClick(object sender, DataGridViewCellEventArgs e) { int row = e.RowIndex, col = e.ColumnIndex; if (col == 0) return; bool value = Cell(col, row); Console.WriteLine(“click at: {0},{1} value={2}”, row, col, value); grid[col, row].Value = value ? F : T; if (!value) { CheckOnEdit(row, col, value); } } // 左上 grid[1,0] 左下 grid[2,0] // 上左 grid[1,1] 上右 grid[2,1] // 右上 grid[1,2] 右下 grid[2,2] // 下左 grid[1,3] 下右 grid[2,3] private void CheckOnEdit(int row, int col, bool value) { if (row == 0) // 左 { if (col == 1) Cell(1, 1, false); if (col == 2) Cell(1, 3, false); } if (row == 1) // 上 { if (col == 1) Cell(1, 0, false); if (col == 2) Cell(1, 2, false); } if (row == 2) // 右 { if (col == 1) Cell(2, 1, false); if (col == 2) Cell(2, 3, false); } if (row == 3) // 下 { if (col == 1) Cell(2, 0, false); if (col == 2) Cell(2, 2, false); } } } }

posted @ 2021-09-14 11:32  愚哦  阅读(397)  评论(0编辑  收藏  举报