MyFrmReg.cs 11.4 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MyClassLib;

namespace MyControlLib
{
    /// <summary>
    /// 窗口基类 扩展了功能 :
    /// 1、窗口Logo图标 
    /// 2、设置窗口的标题
    /// 3、非创建线程可以刷新控件
    /// 4、可以将文本输入光标处
    /// 5、消息窗口类
    /// 6、定义委托可以通过其它窗体刷新本窗体
    /// </summary>
    public partial class MyFrmReg : Form
    {
        
        /// <summary>
        /// 初始化
        /// </summary>
        public MyFrmReg()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化带标题的
        /// </summary>
        /// <param name="str"></param>
        public MyFrmReg( string str)
        {
            InitializeComponent();
            this.Text = str;
        }

        /// <summary>
        /// 向数据库注册,并更新其运行的状态和时间
        /// </summary>
        /// <param name="AppName">应用程序名称</param>
        /// <param name="AppState">应用程序状态</param>
        /// <param name="AppFunction">应用程序功能</param>
        /// <returns></returns>
        public bool RegisterExeState(string AppName,string AppFunction,int AppState)
        {
            bool flag = false;
            //step1: 判断有无注册 
            string str1 = string.Format("select count(1) from T_AppRunState where AppName='{0}'", AppName);
            int t1=(int) CSqlHelper.ExecuteScalar(CSqlHelper.ConnectString, CommandType.Text, str1);
            if (t1 <= 0)
            {
                string strInsert = string.Format("Insert into T_AppRunState(AppName,AppFunction,AppState,InputTime)values('{0}','{1}','{2}',getdate())", AppName, AppFunction, AppState);
                int t2 = CSqlHelper.ExecuteNonQuery(strInsert);
                flag = t2 == 1 ? true : false;
            }
            else
            {
                string strUpdate = string.Format("Update T_AppRunState set AppState='{0}',AppFunction='{1}'  where AppName='{2}'", AppState, AppFunction, AppName);
                int t2 = CSqlHelper.ExecuteNonQuery(strUpdate);
                flag = t2 == 1 ? true : false;
            }
            return flag;
        }

        /// <summary>
        /// 核对软件的时效性
        /// </summary>
        /// <returns></returns>
        public int CheckAppRunCertification()
        {
            int results = 0;

            return results;
        }
        #region UI线程操作UI线程使用
        /// <summary>
        /// 委托 实例是用于不是创建线程操作UI线程时候
        /// </summary>
        /// <param name="ct">这个控件类 只能是TextBox,ucErrorDisplay,listBox(add方法) </param>
        /// <param name="text"></param>
        public delegate void DelegateUpdateUI(Control ct, string text);
        /// <summary>
        /// 实例是用于不是创建线程操作UI线程时候
        /// </summary>
        /// <param name="ct">这个控件类 只能是TextBox,ucErrorDisplay,listBox(add方法) </param>
        /// <param name="text"></param>AB B
        public void UpdateUI(Control ct, string text)
        {
            if (this.InvokeRequired == true)
            {
                this.Invoke(new DelegateUpdateUI(UpdateUI), ct, text);
            }
            else
            {
                if (ct is TextBox)
                {
                    TextBox tb = (TextBox)ct;
                    tb.Text = text;
                }
                else if( ct is ucErrorDisplay)
                {
                    ucErrorDisplay uc = (ucErrorDisplay)ct;
                    uc.ErrText = text;
                }
                else if (ct is ListBox)
                {
                    ListBox lb = (ListBox)ct;
                    lb.Items.Add(text);
                }
                else if (ct is CheckBox)
                {
                    CheckBox cb = (CheckBox)ct;
                    if (text == "true")
                    {
                        cb.CheckState = CheckState.Checked;
                    }
                }
               
                else
                {
                    throw new ArgumentException(ct.GetType().ToString());
                }
            }
        }
        #endregion 
        
        #region //将文本输入到光标处
        /// <summary>
        /// 根据获取鼠标光标所在位置返回窗体句柄值 
        /// </summary>
        /// <param name="xPoint">X 坐标</param>
        /// <param name="yPoint">Y 坐标</param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
        public static extern int WindowFromPoint(int xPoint, int yPoint);
        /// <summary>
        /// 发送或接收消息
        /// </summary>
        /// <param name="hwnd">窗体句柄</param>
        /// <param name="wMsg">消息的类型 WM_GETTEXT/WM_SETTEXT</param>
        /// <param name="wParam">参数的长度</param>
        /// <param name="lParam">参数对象</param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hwnd, int wMsg, int wParam, StringBuilder lParam);
        /// <summary>
        /// 获取当前最前面的窗体 
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
        /// <summary>
        /// 获取当前窗体的进程的标示
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="ProcessId"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
        /// <summary>
        /// 获取交互线程的信息
        /// </summary>
        /// <param name="idThread"></param>
        /// <param name="lpgui"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui); 

         [DllImport("user32.dll", CharSet = CharSet.Auto)]      
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);         
        public const int WM_GETTEXT = 13;
        public const int WM_SETTEXT = 12;

        [StructLayout(LayoutKind.Sequential)]     
        public struct GUITHREADINFO        
        {             
            public int cbSize; 
            public int flags;   
            public IntPtr hwndActive;   
            public IntPtr hwndFocus;   
            public IntPtr hwndCapture;
            public IntPtr hwndMenuOwner; 
            public IntPtr hwndMoveSize; 
            public IntPtr hwndCaret;    
            //public Rect rectCaret;
        }   
  
        public static GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)         
        {            
            if (hwnd != IntPtr.Zero)             
            {                
                //Mbox.Info(GetTitle(hwnd), "O");    
                uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
                GUITHREADINFO guiThreadInfo = new GUITHREADINFO();  
                guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo); 
                if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false) 
                    return null;       
                return guiThreadInfo;          
            }            
            
            return null;  
        } 
        /// <summary>
        /// 设置文本到光标处
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public bool SetFocusText(string text)
        {
            StringBuilder sb = new StringBuilder(5000);
            bool result = false;
            if (sb == null) { throw new ArgumentNullException(); }
            try
            {
                int hwnd = WindowFromPoint(MousePosition.X, MousePosition.Y);
                if (hwnd != 0)
                {
                    SendMessage(hwnd, WM_GETTEXT, sb.Length, sb);
                    sb.AppendLine(text);
                    SendMessage(hwnd, WM_SETTEXT, 0, sb);
                    result = true;
                }
            }
            catch { result = false; }
            return result;
        }

        public void SendCaretText(string text)
        { 
            //获取当前活动窗体的句柄
            IntPtr hwnd = GetForegroundWindow();
            if (string.IsNullOrEmpty(text))
            { return; }
            GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
            if (guiInfo != null)
            {
                IntPtr ptr = (IntPtr)guiInfo.Value.hwndCaret;
                if (ptr != IntPtr.Zero)
                {
                    for (int i = 0; i < text.Length; i++) { SendMessage(ptr, WM_SETTEXT, (IntPtr)(int)text[i], IntPtr.Zero); } 
                }
            }
        }
        #endregion

        /// <summary>
        /// 自动关闭弹出的Messagebox
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //用于找到MessageBox的句柄
        [DllImport("user32.dll")]
        public static extern bool DestroyWindow(IntPtr hWnd);  //根据句柄关于MessageBox

        /// <summary>
        /// 定义一个无参数的委托,来调用某个窗体的方法
        /// </summary>
        public delegate void RefreshForm();

        #region 消息窗口函数
        /// <summary>
        /// 消息窗口函数
        /// </summary>
        public class MessageUtil
        {
            public static DialogResult ShowError(string message)
            {
                return MessageBox.Show(message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }

            public static DialogResult ShowTips(string message)
            {
                return MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }

            public static DialogResult ShowWarning(string message)
            {
                return MessageBox.Show(message, "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            public static DialogResult ShowYesNoAndError(string message)
            {
                return MessageBox.Show(message, "错误信息", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
            }

            public static DialogResult ShowYesNoAndTips(string message)
            {
                return MessageBox.Show(message, "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            }

            public static DialogResult ShowYesNoAndWarning(string message)
            {
                return MessageBox.Show(message, "警告信息", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            }

            public static DialogResult ShowYesNoCancelAndTips(string message)
            {
                return MessageBox.Show(message, "提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
            }
        }
        #endregion
    }
}