GeckoFX 在winform中的简单应用

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

GeckoFX 在winform中的简单应用


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. var value = (int)Math.Min(100, (e.CurrentProgress * 100) / e.MaximumProgress);
  60. if (value == 100)
  61. return;
  62. progressBar1.Value = value;
  63. }
  64. private void btn_GoBack_Click(object sender, EventArgs e)
  65. {
  66. browser.GoBack();
  67. }
  68. private void btn_GoPreview_Click(object sender, EventArgs e)
  69. {
  70. browser.GoForward();
  71. }

4、效果

GeckoFX 在winform中的简单应用

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

GeckoFX 在winform中的简单应用


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

GeckoFX 在winform中的简单应用


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

GeckoFX 在winform中的简单应用

GeckoFX 在winform中的简单应用


代码如下:

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

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); } } } }

上一篇:连接池已满,超过最大连接数 解决方案


下一篇:网络作业一(搭建域、linux加入windows域、wmic渗透使用、linux渗透的信息收集、域环境信息收集)