Flow.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using CefSharp;
  11. using CefSharp.WinForms;
  12. using System.Runtime.InteropServices; //引用此名称空间,简化后面的代码
  13. using System.Net;
  14. using System.Diagnostics;
  15. namespace ServiceWrapper
  16. {
  17. public partial class Flow : Form, IMessageFilter
  18. {
  19. public ChromiumWebBrowser chromeBrowser;
  20. public bool ChromeNow = false; //标记现在所在窗口是否为chrome
  21. public static string flowChartUrl = "http://183.129.170.180:8041/frontEnd/FlowChart.html?id="; //流程图所在的位置
  22. public string url = flowChartUrl + "-1";
  23. public Flow()
  24. {
  25. InitializeComponent();
  26. }
  27. public Flow(string link)
  28. {
  29. InitializeComponent();
  30. url = link;
  31. }
  32. // P/Invoke declarations
  33. [DllImport("user32.dll")]
  34. public static extern IntPtr GetForegroundWindow();
  35. [DllImport("user32.dll")]
  36. private extern static bool SwapMouseButton(bool fSwap);
  37. [System.Runtime.InteropServices.DllImport("user32.dll")]
  38. public static extern int SetForegroundWindow(IntPtr hwnd);
  39. public const int WM_CLOSE = 0x10;
  40. public bool closedriver = true;
  41. [DllImport("user32.dll", EntryPoint = "SendMessage")]
  42. private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  43. public bool PreFilterMessage(ref System.Windows.Forms.Message SystemMessage)
  44. {
  45. if (SystemMessage.Msg >= 513 && SystemMessage.Msg <= 515)
  46. {//不响应鼠标左键消息
  47. return true;
  48. }
  49. return false;
  50. }
  51. private void Flow_Load(object sender, EventArgs e)
  52. {
  53. InitializeChromium();
  54. PublicVariable.isInitialized = true;
  55. //保证并排平铺
  56. int width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width;
  57. int height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height;
  58. StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
  59. Location = (Point)new Size(0, 0); //窗体的起始位置为(x,y)
  60. Width = width;
  61. Height = Convert.ToInt32(height * 0.8);
  62. FormClosing += Flow_FormClosing;
  63. }
  64. //初始化浏览器并启动
  65. public void InitializeChromium()
  66. {
  67. if (!PublicVariable.isInitialized)//只初始化一次
  68. {
  69. CefSettings settings = new CefSettings();
  70. Cef.Initialize(settings);
  71. }
  72. // Create a browser component
  73. chromeBrowser = new ChromiumWebBrowser(url);
  74. //跨域访问允许
  75. chromeBrowser.BrowserSettings.FileAccessFromFileUrls = CefState.Enabled;
  76. chromeBrowser.BrowserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
  77. //textBox1.Text = url;
  78. // Add it to the form and fill it to the form window.
  79. panel1.Controls.Add(chromeBrowser);
  80. chromeBrowser.Dock = DockStyle.Fill;
  81. chromeBrowser.RenderProcessMessageHandler = new RenderProcessMessageHandler();
  82. }
  83. //窗体关闭时,记得停止浏览器
  84. private void Flow_FormClosing(object sender, FormClosingEventArgs e)
  85. {
  86. if(closedriver)
  87. {
  88. //Cef.Shutdown();//关掉内嵌控件
  89. try
  90. {
  91. //PublicVariable.chrome.Kill();//关掉chrome
  92. //SendMessage(Start.chromeId, WM_CLOSE, 0, 0);//关掉chrome
  93. Start.browser.Quit();//关掉chromedriver
  94. }
  95. catch (Exception)
  96. {
  97. }
  98. }
  99. PublicVariable.start.Show(); //重新显示初始框
  100. }
  101. private void timer1_Tick_1(object sender, EventArgs e)
  102. {
  103. if (Start.chromeId != GetForegroundWindow())
  104. {
  105. SwapMouseButton(false);
  106. }
  107. else
  108. {
  109. SwapMouseButton(true);
  110. }
  111. //textBox1.Text = "当前进程:" + Process.GetCurrentProcess().ProcessName +
  112. // ",当前激活的进程:" + ;
  113. ////打开chrome,跳转初始输入网页界面,传回主程序chrome的进程id号,然后打开链接
  114. }
  115. private void panel1_Paint(object sender, PaintEventArgs e)
  116. {
  117. SetForegroundWindow(Start.chromeId); //打开流程图窗口后将chrome窗口显示到最前方
  118. }
  119. private void timer2_Tick(object sender, EventArgs e)
  120. {
  121. if (Start.chromeId == GetForegroundWindow())
  122. {
  123. SwapMouseButton(true);
  124. timer1.Enabled = true; //打开检测鼠标的程序
  125. }
  126. }
  127. }
  128. }