📜 ⬆️ ⬇️

The program for working with windows as in Windows 7 - with the help of hotkeys: Win + [Up | Down | Left | Right]

Rarely use what you write yourself. But here is the rare case.
In Windows 7, I really liked the ability to position windows using keyboard shortcuts:
Win + Left - the window is attached to the left edge
Win + Right - the window is attached to the right edge
Win + Up - maximize window
Win + Bottom - window in normal condition.
And the mounting of windows to the right and left, in my opinion, is a very convenient thing, because how many times did you have to have two windows on the screen, with the possibility of comparing or reprinting ...

I do not want to use Windows 7 as the main OS - because beta (or CR, the main thing is not Release), but I want to use the possibility described above. And now - I was not lazy, and I wrote a program on C # that implements this functionality in Vista (most likely it works even in earlier versions - I just didn’t check it). And as it turned out - the task is not so difficult. I had to import a lot of WinApi functions, and the implementation itself was divided into two: a) a functional that intercepts pressing the necessary shortcut keys b) window positioning.

I solved the first task with the help of the article Low-Level Keyboard Hook in C # , all I had to do was find the opportunity to find out when the LWin key was pressed:
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == ( IntPtr )WAKeysHook.WmKeyDown)
{
if (( int )WAKeysHook.GetAsyncKeyState(( IntPtr )Keys.LWin) != 0) // Left Win Key
{
Keys key = (Keys) Marshal.ReadInt32(lParam);
if (key == Keys.Left || key == Keys.Right || key == Keys.Up || key == Keys.Down) // Arrows keys
SetWindowLocation(key);
}
}
return WAKeysHook.CallNextHookEx(WaKeysHook.HookId, nCode, wParam, lParam);
}


* This source code was highlighted with Source Code Highlighter .
The second part of the task is to find the active window and set it to be literate in size (of course you need to take into account that only windows with this capability can be resized (WS_SIZEBOX), as well as some windows have the maximum allowed dimensions of their window, like cmd.exe) :
private static void SetWindowLocation(Keys k)
{
//Active Window
IntPtr window = WAWindows.GetForegroundWindow();
// Check SIZEBOX Style (Window can sizable)
if ((( int )WAWindows.GetWindowLongPtr(window, WAWindows.GwlStyle) & WAWindows.WsSizebox)
== WAWindows.WsSizebox)
{
// Show window in normal state (if always maximized)
WAWindows.ShowWindow(window, ( int )WAWindows.WindowShowStyle.ShowNormal);

//Need Maximazed
if (k != Keys.Down)
{
WAWindows.ShowWindow(window, ( int )WAWindows.WindowShowStyle.ShowMaximized);

// Place Window on left or right
if (k != Keys.Up)
{
WAWindows.Rect rect, rectDesktop;
if (WAWindows.GetWindowRect(window, out rect) && WAWindows.GetWindowRect(WAWindows.GetDesktopWindow(), out rectDesktop))
{
WAWindows.SetWindowPos(window, WAWindows.HwndTop, (k == Keys.Left) ? Math .Min(rect.Left, 0) : rectDesktop.Right / 2, rect.Top, Math .Min(rect.Width, rectDesktop.Right / 2 - Math .Min(rect.Left, 0)),
Math .Min(rect.Height, rectDesktop.Bottom), WAWindows.SwpShowwindow);
}
}
}
}
}


* This source code was highlighted with Source Code Highlighter .
And finally, in my program, I introduced the option of setting whether it is necessary to display the icon in the tray or not, which helps in closing the program, for this you need to set the value in the corresponding app.config section.
< applicationSettings >
< VistaKeysExtender.Properties.Settings >
< setting name ="ShowNotifyIcon" serializeAs ="String" >
< value > False </ value >
</ setting >
</ VistaKeysExtender.Properties.Settings >
</ applicationSettings >


* This source code was highlighted with Source Code Highlighter .
I just do not need the icon itself, the program is always spinning in the background, and the place in the tray is so crowded. I described the function keys at the top of the article. Questions and suggestions are ready to listen and answer them. I hope that this program will be as useful to someone as me.
UPD: The program received a continuation of this address http://outcoldman.habrahabr.ru/blog/57792/

')

Source: https://habr.com/ru/post/57669/


All Articles