-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
180 lines (155 loc) · 5.25 KB
/
MainWindow.xaml.cs
File metadata and controls
180 lines (155 loc) · 5.25 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
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
namespace SkyLineSQL
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
[DllImport("User32.dll")]
private static extern bool RegisterHotKey(
[In] IntPtr hWnd,
[In] int id,
[In] uint fsModifiers,
[In] uint vk);
[DllImport("User32.dll")]
private static extern bool UnregisterHotKey(
[In] IntPtr hWnd,
[In] int id);
private HwndSource _source;
private const int HOTKEY_ID = 1;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var helper = new WindowInteropHelper(this);
_source = HwndSource.FromHwnd(helper.Handle);
_source.AddHook(HwndHook);
RegisterHotKey();
}
protected override void OnClosed(EventArgs e)
{
_source.RemoveHook(HwndHook);
_source = null;
UnregisterHotKey();
base.OnClosed(e);
}
private void RegisterHotKey()
{
var helper = new WindowInteropHelper(this);
const uint VK_PLUS = 0x6B;
const uint VK_BACK_SLASH = 0xDC;
const uint MOD_CTRL_SHIFT = 6;
if (!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL_SHIFT, VK_PLUS))
{
// handle error
}
else if (!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL_SHIFT, VK_BACK_SLASH))
{
// handle error
}
}
private void UnregisterHotKey()
{
var helper = new WindowInteropHelper(this);
UnregisterHotKey(helper.Handle, HOTKEY_ID);
}
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
switch (msg)
{
case WM_HOTKEY:
switch (wParam.ToInt32())
{
case HOTKEY_ID:
OnHotKeyPressed();
handled = true;
break;
}
break;
}
return IntPtr.Zero;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private void OnHotKeyPressed()
{
if (this.IsVisible)
{
HideWindowLogic();
}
else
{
ShowWindowLogic();
}
}
void HideWindowLogic()
{
this.Hide();
}
private double GetDpiFactor()
{
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
float dpiX = g.DpiX;
return dpiX / 96.0; // 96 is the standard DPI for WPF units
}
}
void ShowWindowLogic()
{
//var activeScreen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var activeScreen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var workingArea = activeScreen.WorkingArea;
var dpiFactor = GetDpiFactor();
var wpfWorkingArea = new Rect(
workingArea.X / dpiFactor,
workingArea.Y / dpiFactor,
workingArea.Width / dpiFactor,
workingArea.Height / dpiFactor
);
// Position the window in the center of the active screen's working area
this.Left = wpfWorkingArea.Left + (wpfWorkingArea.Width - this.Width) / 2;
this.Top = wpfWorkingArea.Top + (wpfWorkingArea.Height - this.Height) / 2;
//this.Show();
//this.Activate();
// --- critical repairs ---
this.Show();
this.Activate();
// -------------------------
//SearchBox_txt.Clear();
SearchBox_txt.Focus();
SearchBox_txt.SelectAll();
SearchBox_txt.ForceCursor = true;
}
MainWindowViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new MainWindowViewModel();
this.DataContext = vm;
ShowWindowLogic();
}
private void dg_source_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (vm.SelectedIndex > -1 && vm.DatabaseObjects.Count > 0)
dg_source.ScrollIntoView(vm.DatabaseObjects[vm.SelectedIndex]);
}
private void SearchBox_txt_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
e.Handled = true; // stops focus from moving
vm.TabClickCommand.Execute(null);
}
}
}
}