forked from yangzhongke/NetAutoGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinFormDialogController.cs
More file actions
140 lines (127 loc) · 4.37 KB
/
WinFormDialogController.cs
File metadata and controls
140 lines (127 loc) · 4.37 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
using System;
using System.Diagnostics;
using System.Runtime.Versioning;
using System.Windows.Forms;
using Vanara.PInvoke;
namespace NetAutoGUI.Windows
{
[SupportedOSPlatform("windows")]
internal class WinFormDialogController : IDialogController
{
public long? Parent { get; set; }
private IntPtr GetParentWindowHandle()
{
if (Parent!=null)
{
return new IntPtr(Parent.Value);
}
else
{
HWND foregroundWindow = User32.GetForegroundWindow();
if (foregroundWindow != HWND.NULL)
{
User32.GetWindowThreadProcessId(foregroundWindow, out uint processId);
uint currentProcessId = (uint)Process.GetCurrentProcess().Id;
if (processId == currentProcessId)
{
return new IntPtr(foregroundWindow.ToInt64());
}
else
{
return Process.GetCurrentProcess().MainWindowHandle;
}
}
else
{
return Process.GetCurrentProcess().MainWindowHandle;
}
}
}
private IWin32Window GetParentWindow()
{
return NativeWindow.FromHandle(GetParentWindowHandle());
}
public void Alert(string text, string title = " ")
{
User32.MessageBox(GetParentWindowHandle(), text, title, User32.MB_FLAGS.MB_ICONINFORMATION | User32.MB_FLAGS.MB_OK);
}
public bool Confirm(string text, string title = " ")
{
var result = User32.MessageBox(GetParentWindowHandle(), text, title, User32.MB_FLAGS.MB_ICONQUESTION | User32.MB_FLAGS.MB_OKCANCEL);
return result == User32.MB_RESULT.IDOK;
}
public string? Password(string title = "", string? okText = null, string? cancelText = null)
{
return ShowInputForm(title, okText, cancelText, FormInputType.Password);
}
public string? Prompt(string title = "", string? okText = null, string? cancelText = null)
{
return ShowInputForm(title, okText, cancelText, FormInputType.Plain);
}
private string? ShowInputForm(string title, string? okText, string? cancelText, FormInputType inputType)
{
using FormInput form = new FormInput();
form.InputType = inputType;
form.Text = title;
if (!string.IsNullOrEmpty(okText))
{
form.OKText = okText;
}
if (!string.IsNullOrEmpty(cancelText))
{
form.CancelText = cancelText;
}
if (form.ShowDialog(GetParentWindow()) == DialogResult.OK)
{
return form.Value;
}
else
{
return null;
}
}
public bool YesNoBox(string text, string title = " ")
{
var result = User32.MessageBox(GetParentWindowHandle(), text, title, User32.MB_FLAGS.MB_ICONQUESTION | User32.MB_FLAGS.MB_YESNO);
return result == User32.MB_RESULT.IDYES;
}
public string? SelectFolder()
{
using FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog(GetParentWindow()) == DialogResult.OK)
{
return dialog.SelectedPath;
}
else
{
return null;
}
}
public string? SelectFileForSave(string filters = "")
{
using SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = filters;
if (dialog.ShowDialog(GetParentWindow()) == DialogResult.OK)
{
return dialog.FileName;
}
else
{
return null;
}
}
public string? SelectFileForLoad(string filters = "")
{
using OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = filters;
if (dialog.ShowDialog(GetParentWindow()) == DialogResult.OK)
{
return dialog.FileName;
}
else
{
return null;
}
}
}
}