| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Speech.Synthesis;
- using System.Text;
- using System.Windows.Forms;
- namespace Core.StlMes.Client.LgResMgt.Mcms
- {
- public static class Comm
- {
- public static void Speak(this string msg)
- {
- SpeechSynthesizer voice = new SpeechSynthesizer
- {
- Rate = 1,
- Volume = 100
- };
- voice.SpeakCompleted += (sender, args) => voice.Dispose();
- //设置语速,[-10,10]
- //设置音量,[0,100]
- voice.SpeakAsync("1 " + msg);
- }
- /// <summary>
- /// 安全刷新界面控件 当为子线程时委托刷新界面
- /// </summary>
- /// <param name="control"></param>
- /// <param name="action"></param>
- public static void SafeRefreshControl(this System.Windows.Forms.Control control, Action action)
- {
- if (control.InvokeRequired)
- control.Invoke(action);
- else
- action();
- }
- /// <summary>
- /// 刷新Grid数据并根据数据调整Grid列宽
- /// </summary>
- /// <param name="ultraGrid">需要处理的Grid</param>
- public static void RefreshAndAutoSize(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid, string[] unSizeColumns = null)
- {
- try
- {
- ultraGrid.DataBind();
- ultraGrid.UpdateData();
- ultraGrid.Refresh();
- foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ultraGrid.DisplayLayout.Bands)
- {
- foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn column in band.Columns)
- {
- if (column.Hidden) continue;
- if (unSizeColumns == null || !unSizeColumns.Contains(column.Key))
- column.PerformAutoResize(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand, true);
- }
- }
- ultraGrid.Refresh();
- }
- catch { }
- }
-
- public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
- {
- MessageForm frm = new MessageForm(buttons, buttonTitles);
- frm.Show();
- frm.WatchForActivate = true;
- DialogResult result = MessageBox.Show(frm, text, caption, buttons);
- frm.Close();
-
- return result;
- }
-
- public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
- MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles)
- {
- MessageForm frm = new MessageForm(buttons, buttonTitles);
- frm.Show();
- frm.WatchForActivate = true;
- DialogResult result = MessageBox.Show(frm, text, caption, buttons, icon, defaultButton);
- frm.Close();
-
- return result;
- }
-
- private class MessageForm : Form
- {
- IntPtr _handle;
- MessageBoxButtons _buttons;
- string[] _buttonTitles = null;
-
- bool _watchForActivate = false;
-
- public bool WatchForActivate
- {
- get { return _watchForActivate; }
- set { _watchForActivate = value; }
- }
-
- public MessageForm(MessageBoxButtons buttons, string[] buttonTitles)
- {
- _buttons = buttons;
- _buttonTitles = buttonTitles;
-
- // Hide self form, and don't show self form in task bar.
- this.Text = "";
- this.StartPosition = FormStartPosition.CenterScreen;
- this.Location = new Point(-32000, -32000);
- this.ShowInTaskbar = false;
- }
-
- protected override void OnShown(EventArgs e)
- {
- base.OnShown(e);
- // Hide self form, don't show self form even in task list.
- NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, 659);
- }
-
- protected override void WndProc(ref System.Windows.Forms.Message m)
- {
- if (_watchForActivate && m.Msg == 0x0006)
- {
- _watchForActivate = false;
- _handle = m.LParam;
- CheckMsgbox();
- }
- base.WndProc(ref m);
- }
-
- private void CheckMsgbox()
- {
- if (_buttonTitles == null || _buttonTitles.Length == 0)
- return;
-
- // Button title index
- int buttonTitleIndex = 0;
- // Get the handle of control in current window.
- IntPtr h = NativeWin32API.GetWindow(_handle, GW_CHILD);
-
- // Set those custom titles to the three buttons(Default title are: Yes, No and Cancle).
- while (h != IntPtr.Zero)
- {
- if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
- {
- if (_buttonTitles.Length > buttonTitleIndex)
- {
- // Changes the text of the specified window's title bar (if it has one).
- // If the specified window is a control, the text of the control is changed.
- // However, SetWindowText cannot change the text of a control in another application.
- NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]);
-
- buttonTitleIndex++;
- }
- }
-
- // Get the handle of next control in current window.
- h = NativeWin32API.GetWindow(h, GW_HWNDNEXT);
- }
- }
- }
-
-
- public const int GW_CHILD = 5;
- public const int GW_HWNDNEXT = 2;
-
- public class NativeWin32API
- {
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindow(IntPtr hWnd, Int32 wCmd);
- [DllImport("user32.dll")]
- public static extern bool SetWindowText(IntPtr hWnd, string lpString);
- [DllImport("user32.dll")]
- public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
-
- public static string GetWindowClassName(IntPtr handle)
- {
- StringBuilder sb = new StringBuilder(256);
-
- // Retrieves the name of the class to which the specified window belongs
- GetClassNameW(handle, sb, sb.Capacity);
- return sb.ToString();
- }
- }
-
- }
-
- }
|