坐席客户端5.0

FrmMain.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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.Configuration;
  13. using System.Drawing.Drawing2D;
  14. using System.Runtime.InteropServices;
  15. using System.Xml;
  16. using System.Threading;
  17. using System.Diagnostics;
  18. using System.Net;
  19. using CefSharp.WinForms.Internals;
  20. namespace Lecall
  21. {
  22. public partial class FrmMain : Form
  23. {
  24. public string ServerURL = "";
  25. public string PhoneNum = "";
  26. public static string MainURL = "";
  27. public FrmMain()
  28. {
  29. //InitializeComponent();
  30. //this.WindowState = FormWindowState.Maximized;
  31. //InitializeChromium();
  32. // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3
  33. //chromeBrowser.RegisterJsObject("formProcess", new FormProcess(chromeBrowser, this));
  34. //MessageBox.Show("4");
  35. InitializeComponent();
  36. //MessageBox.Show("1");
  37. }
  38. private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
  39. {
  40. this.InvokeOnUiThreadIfRequired(() => Text = args.Title);
  41. }
  42. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  43. {
  44. if (this.IsHandleCreated)
  45. {
  46. this.BeginInvoke(new MethodInvoker(() =>
  47. {
  48. if (this.IsHandleCreated && !this.IsDisposed)
  49. {
  50. if (this.toolTip1.Active)
  51. {
  52. this.toolTip1.Hide(this);
  53. }
  54. }
  55. }));
  56. }
  57. if (e.PropertyName == "TooltipText") //tooltipText改变事件
  58. {
  59. string tooltipText = this.wb.TooltipText;
  60. Point elementPos = new Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);
  61. if (!String.IsNullOrEmpty(tooltipText))
  62. {
  63. this.Invoke(new MethodInvoker(() =>
  64. {
  65. System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  66. timer.Interval = 600;
  67. timer.Tick += (timer_sender, timer_e) =>
  68. {
  69. Point nowPos = new Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);
  70. //if ((nowPos.X <= elementPos.X + 20 && nowPos.X >= elementPos.X - 20) && (nowPos.Y <= elementPos.Y + 20 && nowPos.Y >= elementPos.Y - 20)) //鼠标停留了500ms(范围限定在一定范围)
  71. if (this.wb.TooltipText == tooltipText) //600毫秒后,tooltipText没变
  72. {
  73. this.toolTip1.Show(tooltipText, this, nowPos.X + 15, nowPos.Y + 15);
  74. }
  75. (timer_sender as System.Windows.Forms.Timer).Stop();
  76. (timer_sender as System.Windows.Forms.Timer).Dispose();
  77. };
  78. timer.Start();
  79. }));
  80. }
  81. }
  82. }
  83. ChromiumWebBrowser wb;
  84. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  85. {
  86. Cef.Shutdown();
  87. }
  88. private void Form1_KeyDown(object sender, KeyEventArgs e)
  89. {
  90. /*if (e.Control && e.KeyCode == Keys.F1)
  91. {
  92. //设置服务器地址
  93. string inputcap = InputBox("基础设置", "请输入服务器地址:", ServerURL);
  94. if (inputcap != null && inputcap != "")
  95. {
  96. string path = Environment.CurrentDirectory + @"\Config.ini"; //指定ini文件的路径
  97. IniFile ini = new IniFile(path);
  98. ini.IniWriteValue("基础设置", "服务器地址", inputcap);//string FpOrderNumber = ini.IniReadValue("登录设置", "服务器地址");//将tsslMo.Text的值写到键FpOrderNumber中去
  99. //重新加载页面
  100. this.Controls.Remove(wb);
  101. //设置停靠方式
  102. wb.Dock = DockStyle.Fill;
  103. //加入到当前窗体中
  104. this.Controls.Add(wb);
  105. }
  106. }*/
  107. }
  108. #region InputForm
  109. private string[] InputBox(string Caption, string Hint, string Default, string Hint1, string Default1)
  110. {
  111. Form InputForm = new Form();
  112. InputForm.MinimizeBox = false;
  113. InputForm.MaximizeBox = false;
  114. InputForm.StartPosition = FormStartPosition.CenterScreen;
  115. InputForm.Width = 380;
  116. InputForm.Height = 150;
  117. InputForm.FormBorderStyle = FormBorderStyle.FixedSingle;
  118. InputForm.Font = new Font("微软雅黑", 9, FontStyle.Regular);
  119. //InputForm.Font.Size = 10;
  120. InputForm.ShowIcon = false;
  121. InputForm.Text = Caption;
  122. Label lbl = new Label();
  123. lbl.Text = Hint;
  124. lbl.Left = 10;
  125. lbl.Top = 20;
  126. lbl.Parent = InputForm;
  127. lbl.AutoSize = true;
  128. TextBox tb = new TextBox();
  129. //tb.PasswordChar = '*';
  130. tb.Left = 85;
  131. tb.Top = 18;
  132. tb.Width = 260;
  133. tb.Parent = InputForm;
  134. tb.Text = Default;
  135. tb.SelectAll();
  136. //2017-8-5
  137. Label lbl1 = new Label();
  138. lbl1.Text = Hint1;
  139. lbl1.Left = 35;
  140. lbl1.Top = 50;
  141. lbl1.Parent = InputForm;
  142. lbl1.AutoSize = true;
  143. TextBox tb1 = new TextBox();
  144. //tb.PasswordChar = '*';
  145. tb1.Left = 85;
  146. tb1.Top = 48;
  147. tb1.Width = 260;
  148. tb1.Parent = InputForm;
  149. tb1.Text = Default1;
  150. Button btnok = new Button();
  151. btnok.Left = 100;
  152. btnok.Top = 80;
  153. btnok.Parent = InputForm;
  154. btnok.Text = "确定";
  155. InputForm.AcceptButton = btnok;//回车响应
  156. btnok.DialogResult = DialogResult.OK;
  157. Button btncancal = new Button();
  158. btncancal.Left = 190;
  159. btncancal.Top = 80;
  160. btncancal.Parent = InputForm;
  161. btncancal.Text = "取消";
  162. btncancal.DialogResult = DialogResult.Cancel;
  163. try
  164. {
  165. if (InputForm.ShowDialog() == DialogResult.OK)
  166. {
  167. string[] inputcap=new string[2];
  168. inputcap[0] = tb.Text;
  169. inputcap[1] = tb1.Text;
  170. return inputcap;
  171. }
  172. else
  173. {
  174. return null;
  175. }
  176. }
  177. finally
  178. {
  179. InputForm.Dispose();
  180. }
  181. }
  182. #endregion
  183. #region
  184. /*[DllImport("User32.dll")]
  185. private static extern IntPtr GetWindowDC(IntPtr hwnd);
  186. [DllImport("User32.dll")]
  187. private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
  188. [DllImport("Kernel32.dll")]
  189. private static extern int GetLastError();
  190. Rectangle m_rect = new Rectangle(800, 10, 80, 20);
  191. protected override void WndProc(ref Message m)
  192. {
  193. //拦截双击标题栏、移动窗体的系统消息
  194. //if (m.Msg != 0xA3 && m.Msg != 0x0003 && m.WParam != (IntPtr)0xF012)
  195. //{
  196. // base.WndProc(ref m);
  197. //}
  198. base.WndProc(ref m);
  199. switch (m.Msg)
  200. {
  201. case 0x86://WM_NCACTIVATE
  202. goto case 0x85;
  203. case 0x85://WM_NCPAINT
  204. {
  205. IntPtr hDC = GetWindowDC(m.HWnd);
  206. //把DC转换为.NET的Graphics就可以很方便地使用Framework提供的绘图功能了
  207. Graphics gs = Graphics.FromHdc(hDC);
  208. //Graphics gs = this .CreateGraphics();
  209. gs.FillRectangle(new LinearGradientBrush(m_rect, Color.Transparent, Color.Transparent, LinearGradientMode.BackwardDiagonal), m_rect);
  210. //gs.FillRectangle(new LinearGradientBrush(m_rect, Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal), m_rect);
  211. StringFormat strFmt = new StringFormat();
  212. strFmt.Alignment = StringAlignment.Center;
  213. strFmt.LineAlignment = StringAlignment.Center;
  214. gs.DrawString("基础设置", this.Font, Brushes.DarkSlateGray, m_rect, strFmt);//▲
  215. //gs.DrawString("设置", this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);//▲
  216. gs.Dispose();
  217. //释放GDI资源
  218. ReleaseDC(m.HWnd, hDC);
  219. break;
  220. }
  221. case 0xA1://WM_NCLBUTTONDOWN
  222. {
  223. Point mousePoint = new Point((int)m.LParam);
  224. mousePoint.Offset(-this.Left, -this.Top);
  225. if (m_rect.Contains(mousePoint))
  226. {
  227. wb.ShowDevTools();
  228. //MessageBox.Show("hello");
  229. //设置服务器地址
  230. string inputcap = InputBox("基础设置", "请输入服务器地址:", ServerURL);
  231. if (inputcap != null && inputcap != "")
  232. {
  233. string path = Environment.CurrentDirectory + @"\Config.ini"; //指定ini文件的路径
  234. IniFile ini = new IniFile(path);
  235. ini.IniWriteValue("基础设置", "服务器地址", inputcap);//string FpOrderNumber = ini.IniReadValue("登录设置", "服务器地址");//将tsslMo.Text的值写到键FpOrderNumber中去
  236. //重新加载页面
  237. this.Controls.Remove(wb);
  238. wb = new ChromiumWebBrowser(inputcap);
  239. //设置停靠方式
  240. wb.Dock = DockStyle.Fill;
  241. //加入到当前窗体中
  242. this.Controls.Add(wb);
  243. }
  244. }
  245. break;
  246. }
  247. case 0xA3://WM_NCACTIVATE
  248. m.WParam = System.IntPtr.Zero;
  249. //this.WindowState = FormWindowState.Maximized;
  250. break;
  251. case 0xA9://WM_NCACTIVATE
  252. break;
  253. }
  254. }
  255. private void Form1_SizeChanged(object sender, EventArgs e)
  256. {
  257. m_rect = new Rectangle(this.Size.Width - 190, 10, 80, 20);
  258. }*/
  259. #endregion
  260. private void Form1_Load(object sender, EventArgs e)
  261. {
  262. //Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen; int width = rect.Width; int height = rect.Height;
  263. ////this.MaximumSize = new Size(width, height);
  264. //this.MinimumSize = new Size(width, height);
  265. //this.WindowState = FormWindowState.Maximized;
  266. //IntPtr test = FindWindowEx(0, 0, "Shell_TrayWnd", "");
  267. //Console.WriteLine(test.ToString());
  268. //int test5 = SetWindowPos(test, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW);
  269. //this.TopMost = true;
  270. //this.FormBorderStyle = FormBorderStyle.None;
  271. //this.Width = Screen.PrimaryScreen.Bounds.Width;
  272. //this.Height = Screen.PrimaryScreen.Bounds.Height;
  273. //this.Top = 0;
  274. //this.Left = 0;
  275. #region 2017-10-31
  276. this.WindowState = FormWindowState.Maximized;
  277. //必须进行初始化,否则就出来页面啦。
  278. #region 2017-8-30测试
  279. //var setting = new CefSharp.CefSettings();
  280. //setting.Locale = "zh-CN";
  281. ////缓存路径
  282. //setting.CachePath = "/BrowserCache";
  283. ////浏览器引擎的语言
  284. //setting.AcceptLanguageList = "zh-CN,zh;q=0.8";
  285. //setting.LocalesDirPath = "/localeDir";
  286. ////日志文件
  287. //setting.LogFile = "/LogData";
  288. //setting.PersistSessionCookies = true;
  289. //setting.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
  290. //setting.UserDataPath = "/userData";
  291. //CefSharp.Cef.Initialize(setting);
  292. #endregion
  293. CefSettings settings = new CefSettings();
  294. settings.Locale = "zh-CN";
  295. settings.AcceptLanguageList = "zh-CN";
  296. Cef.Initialize(settings);
  297. //CefSharp.Cef.Initialize();
  298. string path = Environment.CurrentDirectory + @"\Config.ini"; //指定ini文件的路径
  299. //MessageBox.Show("2");
  300. IniFile ini = new IniFile(path);
  301. //MessageBox.Show("3");
  302. ServerURL = ini.IniReadValue("基础设置", "服务器地址");
  303. PhoneNum = ini.IniReadValue("基础设置", "分机号");
  304. MainURL = ini.IniReadValue("软件升级设置", "服务器地址");
  305. //MessageBox.Show(ServerURL);
  306. //实例化控件
  307. wb = new CefSharp.WinForms.ChromiumWebBrowser(ServerURL);
  308. //wb = new ChromiumWebBrowser(ServerURL);
  309. //设置停靠方式
  310. wb.Dock = DockStyle.Fill;
  311. //设置cookie
  312. var mngr = Cef.GetGlobalCookieManager();
  313. CefSharp.Cookie Ac = new CefSharp.Cookie();
  314. Ac.Name = "extno";
  315. Ac.Value = PhoneNum;
  316. Ac.Expires = DateTime.MinValue;
  317. mngr.SetCookieAsync(ServerURL, Ac);
  318. //加入到当前窗体中
  319. this.panel1.Controls.Add(wb);
  320. wb.MenuHandler = new MenuHandler();
  321. wb.DownloadHandler = new DownloadHandler();
  322. //wb.PropertyChanged += OnPropertyChanged;
  323. //2017-10-30
  324. //wb.TitleChanged += OnBrowserTitleChanged;
  325. this.Text = "河南华谊乐呼呼叫中心V" + LocalVersion;
  326. #endregion
  327. }
  328. #region 全屏去任务栏
  329. /*public const int HWND_NOTOPMOST = -2;
  330. public const int SWP_SHOWWINDOW = 0x0040;
  331. [DllImport("user32.dll")]
  332. private static extern IntPtr FindWindowEx(int hwnd2, int hWnd2, string lpsz1, string lpsz2);
  333. [DllImport("user32.dll")]
  334. private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
  335. [DllImport("user32.dll")]
  336. private static extern int SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);*/
  337. #endregion
  338. string devToolsUrl;
  339. private void tbnDebug_Click(object sender, EventArgs e)
  340. {
  341. wb.ShowDevTools();
  342. }
  343. private void tbtbnSet_Click(object sender, EventArgs e)
  344. {
  345. //设置服务器地址
  346. string[] inputcap = InputBox("基础设置", "服务器地址:", ServerURL,"分机号:",PhoneNum);
  347. if (inputcap != null )
  348. {
  349. string path = Environment.CurrentDirectory + @"\Config.ini"; //指定ini文件的路径
  350. IniFile ini = new IniFile(path);
  351. ini.IniWriteValue("基础设置", "服务器地址", inputcap[0]);//string FpOrderNumber = ini.IniReadValue("登录设置", "服务器地址");//将tsslMo.Text的值写到键FpOrderNumber中去
  352. ini.IniWriteValue("基础设置", "分机号", inputcap[1]);
  353. ServerURL = inputcap[0];
  354. PhoneNum = inputcap[1];
  355. var request = WebRequest.Create(ServerURL) as HttpWebRequest;
  356. string x = request.Host;
  357. ini.IniWriteValue("软件升级设置", "服务器地址", "http://" + x);
  358. MainURL = "http://" + x;
  359. //2017-8-5将分机号写cookie
  360. //var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
  361. //await cookieManager.SetCookieAsync(ServerURL,
  362. //new CefSharp.Cookie()
  363. //{
  364. // Domain = ServerURL,
  365. // Name = "extno",
  366. // Value = PhoneNum,
  367. // Expires = DateTime.MinValue
  368. //});
  369. //重新加载页面
  370. this.panel1 .Controls.Remove(wb);
  371. wb = new ChromiumWebBrowser(inputcap[0]);
  372. //设置停靠方式
  373. wb.Dock = DockStyle.Fill;
  374. //分机号设置cookie
  375. var mngr = Cef.GetGlobalCookieManager();
  376. CefSharp.Cookie Ac = new CefSharp.Cookie();
  377. Ac.Name = "extno";
  378. Ac.Value = PhoneNum;
  379. Ac.Expires = DateTime.MinValue;
  380. mngr.SetCookieAsync(ServerURL, Ac);
  381. //加入到当前窗体中
  382. this.panel1.Controls.Add(wb);
  383. }
  384. }
  385. /// <summary>
  386. /// 显示调试窗口
  387. /// </summary>
  388. /*public void ShowDevWin()
  389. {
  390. try
  391. {
  392. if (string.IsNullOrEmpty(devToolsUrl))
  393. {
  394. devToolsUrl = web_view.Browser.GetHost().GetDevToolsUrl(true);
  395. wb.ShowDevTools();
  396. }
  397. var frame = web_view.Browser.GetMainFrame();
  398. //frame.ExecuteJavaScript(string.Format("window.open('{0}');", devToolsUrl), "about:blank", 0);
  399. var p = Process.Start(devToolsUrl);
  400. }
  401. catch
  402. {
  403. MessageBox.Show("请等待页面加载完成之后再打开调试器");
  404. }
  405. }*/
  406. public static string LocalVersion = "";
  407. #region 判断是否更新版本
  408. /// <summary>
  409. /// 判断是否更新
  410. /// </summary>
  411. private static void Update()
  412. {
  413. try
  414. {
  415. //获取当前程序的版本号 比较进行升级
  416. LocalVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  417. string xmlFilePath = MainURL+@"/LecallUpdate/Update.xml?v=" + System.DateTime.Now.ToString("yyyyMMddhhmmss");
  418. //string strHTML = Commoon.HttpWeb.GetHTML(xmlFilePath);
  419. //Commoon.XmlHelper xml = new Commoon.XmlHelper();
  420. //xml.LoadXml(strHTML);
  421. XmlDocument doc = new XmlDocument();
  422. doc.Load(xmlFilePath);
  423. XmlElement root = doc.DocumentElement;
  424. XmlNode versionNode = root.SelectSingleNode("version");
  425. string DistalVersion = versionNode.InnerText;
  426. if (DistalVersion != LocalVersion)
  427. {
  428. frmUpdateMsg frmupdatemsg = new frmUpdateMsg();
  429. frmupdatemsg.Msg = root.SelectSingleNode("description").InnerText;
  430. frmupdatemsg.ShowDialog();
  431. }
  432. }
  433. catch
  434. {
  435. //InfoLog.frmLog frmlog = new InfoLog.frmLog();
  436. //frmlog.AddLog("验证新版本出现问题");
  437. }
  438. }
  439. #endregion
  440. [DllImport("user32.dll")]//设置窗体位活动窗体
  441. private static extern bool SetForegroundWindow(IntPtr hWnd);
  442. [DllImport("user32.dll")]//设置窗体状态
  443. private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  444. private const int SW_RESTORE = 9; //用原来的大小和位置显示一个窗口
  445. [STAThread]
  446. static void Main()
  447. {
  448. bool createForm = false;//是否创建引用
  449. using (Mutex mutex = new Mutex(true, "ATT", out createForm))
  450. {
  451. //MessageBox.Show("5");
  452. if (createForm)
  453. {//如果系统中不存在"AHookProgram"元素 则创建窗体
  454. //Application.EnableVisualStyles();
  455. //Application.SetCompatibleTextRenderingDefault(false);
  456. //MessageBox.Show("6");
  457. #region 版本判断是否更新
  458. string path = Environment.CurrentDirectory + @"\Config.ini"; //指定ini文件的路径
  459. IniFile ini = new IniFile(path);
  460. MainURL = ini.IniReadValue("软件升级设置", "服务器地址");
  461. Update();
  462. #endregion
  463. //MessageBox.Show("7");
  464. Application.EnableVisualStyles();
  465. Application.SetCompatibleTextRenderingDefault(false);
  466. Application.Run(new FrmMain());
  467. }
  468. else
  469. {//如果系统中存在"AHookProgram"元素 则表明程序已经运行
  470. //MessageBox.Show("程序已经运行在进程中!", "提示");
  471. //遍历进程根据进程名找寻窗体
  472. //MessageBox.Show("8");
  473. Process current = Process.GetCurrentProcess();
  474. foreach (Process p in Process.GetProcessesByName(current.ProcessName))
  475. {
  476. //MessageBox.Show("9");
  477. if (p.Id != current.Id)
  478. {//会查上来两个 将不是自己的那个显示出来
  479. //MessageBox.Show("10");
  480. SetForegroundWindow(p.MainWindowHandle);//激活窗体
  481. ShowWindowAsync(p.MainWindowHandle, SW_RESTORE);//设置窗体状态
  482. }
  483. }
  484. }
  485. }
  486. }
  487. private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
  488. {
  489. System.Environment.Exit(0);
  490. }
  491. }
  492. }