MyFrmReg.cs
11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
}
}