Skip to content

Commit

Permalink
Added a menu item to save the window screenshot.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPro committed Oct 17, 2018
1 parent ced5818 commit 3f87e01
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 41 deletions.
4 changes: 4 additions & 0 deletions SmartSystemMenu/Code/Common/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ static class NativeMethods
[return: MarshalAs(UnmanagedType.Bool)]
public static extern Boolean GetWindowRect(IntPtr handle, out Rect lpRect);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern Boolean PrintWindow(IntPtr handle, IntPtr hdc, Int32 nFlags);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern Boolean GetClientRect(IntPtr handle, out Rect lpRect);
Expand Down
12 changes: 7 additions & 5 deletions SmartSystemMenu/Code/Common/SystemMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public Boolean Exists
public const Int32 SC_ALIGN_DEFAULT = 0x4799;
public const Int32 SC_ALIGN_CUSTOM = 0x4800;
public const Int32 SC_ALIGN_MONITOR = 0x4801;
public const Int32 SC_SAVE_SCREEN_SHOT = 0x4802;

#endregion

Expand All @@ -108,6 +109,7 @@ public void Create()
NativeMethods.InsertMenu(windowMenuHandle, index, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, IntPtr.Zero, "");
NativeMethods.InsertMenu(windowMenuHandle, index + 1, NativeConstants.MF_BYPOSITION, SC_INFORMATION, "Information");
NativeMethods.InsertMenu(windowMenuHandle, index + 2, NativeConstants.MF_BYPOSITION, SC_TOPMOST, "Always On Top");
NativeMethods.InsertMenu(windowMenuHandle, index + 3, NativeConstants.MF_BYPOSITION, SC_SAVE_SCREEN_SHOT, "Save Window Screenshot");

_sizeMenuHandle = NativeMethods.CreateMenu();
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_SIZE_640_480, "640x480");
Expand All @@ -127,7 +129,7 @@ public void Create()
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_SIZE_DEFAULT, "Default");
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_SIZE_CUSTOM, "Custom...");
NativeMethods.InsertMenu(windowMenuHandle, index + 3, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _sizeMenuHandle, "Resize");
NativeMethods.InsertMenu(windowMenuHandle, index + 4, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _sizeMenuHandle, "Resize");

_alignmentMenuHandle = NativeMethods.CreateMenu();
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_ALIGN_MONITOR, "Monitor");
Expand All @@ -145,7 +147,7 @@ public void Create()
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_ALIGN_DEFAULT, "Default");
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_ALIGN_CUSTOM, "Custom...");
NativeMethods.InsertMenu(windowMenuHandle, index + 4, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _alignmentMenuHandle, "Alignment");
NativeMethods.InsertMenu(windowMenuHandle, index + 5, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _alignmentMenuHandle, "Alignment");

_transparencyMenuHandle = NativeMethods.CreateMenu();
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_TRANS_00, "0% (opaque)");
Expand All @@ -163,7 +165,7 @@ public void Create()
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_TRANS_DEFAULT, "Default");
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_TRANS_CUSTOM, "Custom...");
NativeMethods.InsertMenu(windowMenuHandle, index + 5, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _transparencyMenuHandle, "Transparency");
NativeMethods.InsertMenu(windowMenuHandle, index + 6, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _transparencyMenuHandle, "Transparency");

_priorityMenuHandle = NativeMethods.CreateMenu();
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_REAL_TIME, "Real Time: 24");
Expand All @@ -172,12 +174,12 @@ public void Create()
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_NORMAL, "Normal: 8");
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_BELOW_NORMAL, "Below Normal: 6");
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_IDLE, "Idle: 4");
NativeMethods.InsertMenu(windowMenuHandle, index + 6, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _priorityMenuHandle, "Priority");
NativeMethods.InsertMenu(windowMenuHandle, index + 7, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _priorityMenuHandle, "Priority");

_systemTrayMenuHandle = NativeMethods.CreateMenu();
NativeMethods.InsertMenu(_systemTrayMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_MINIMIZE_TO_SYSTEMTRAY, "&Mimimize To Tray");
NativeMethods.InsertMenu(_systemTrayMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY, "Mimimize To Tray Always");
NativeMethods.InsertMenu(windowMenuHandle, index + 7, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _systemTrayMenuHandle, "System Tray");
NativeMethods.InsertMenu(windowMenuHandle, index + 8, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _systemTrayMenuHandle, "System Tray");
}

public void Destroy()
Expand Down
21 changes: 21 additions & 0 deletions SmartSystemMenu/Code/Common/Win32WindowWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SmartSystemMenu.Code.Common
{
class Win32WindowWrapper : IWin32Window
{
public IntPtr Handle
{
get; private set;
}

public Win32WindowWrapper(IntPtr handle)
{
Handle = handle;
}
}
}
23 changes: 23 additions & 0 deletions SmartSystemMenu/Code/Common/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using SmartSystemMenu.Code.Common.Extensions;

Expand Down Expand Up @@ -191,6 +192,14 @@ public Boolean ExistSystemTrayIcon
}
}

public IWin32Window Win32Window
{
get
{
return new Win32WindowWrapper(Handle);
}
}

#endregion


Expand Down Expand Up @@ -398,6 +407,20 @@ public void SetPriority(Priority priority)
NativeMethods.SetPriorityClass(processHandle, priorityClass);
}

public Bitmap PrintWindow()
{
Rect rect;
NativeMethods.GetWindowRect(Handle, out rect);
var bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
using (var graphics = Graphics.FromImage(bitmap))
{
var hdc = graphics.GetHdc();
NativeMethods.PrintWindow(Handle, hdc, 0);
graphics.ReleaseHdc(hdc);
}
return bitmap;
}

public static void CloseAllWindowsOfProcess(Int32 processId)
{
NativeMethods.EnumWindowDelegate d = delegate(IntPtr hWnd, Int32 lParam)
Expand Down
95 changes: 65 additions & 30 deletions SmartSystemMenu/Code/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Drawing.Imaging;
using SmartSystemMenu.Code.Common;
using SmartSystemMenu.Code.Common.Extensions;
using SmartSystemMenu.Code.Hooks;
Expand Down Expand Up @@ -93,8 +94,8 @@ protected override void OnLoad(EventArgs e)
_cbtHook.MinMax += WindowMinMax;
_cbtHook.Start();

//_keyboardHook = new KeyboardHook(Handle);
//_keyboardHook.KeyboardEvent += WindowKeyboardEvent;
_keyboardHook = new KeyboardHook(Handle);
_keyboardHook.KeyboardEvent += WindowKeyboardEvent;
//_keyboardHook.Start();

Hide();
Expand Down Expand Up @@ -307,32 +308,62 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
case NativeConstants.SC_MAXIMIZE:
{
window.Menu.UncheckSizeMenu();
} break;
}
break;

case SystemMenu.SC_MINIMIZE_TO_SYSTEMTRAY:
{
window.MinimizeToSystemTray();
} break;
}
break;

case SystemMenu.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY:
{
Boolean r = window.Menu.IsMenuItemChecked(SystemMenu.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY);
window.Menu.CheckMenuItem(SystemMenu.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY, !r);
} break;
}
break;

case SystemMenu.SC_INFORMATION:
{
InfoForm infoForm = new InfoForm(window);
infoForm.Show();
Window.ForceForegroundWindow(infoForm.Handle);
} break;
var infoForm = new InfoForm(window);
infoForm.Show(window.Win32Window);
}
break;

case SystemMenu.SC_SAVE_SCREEN_SHOT:
{
var bitmap = window.PrintWindow();
var dialog = new SaveFileDialog
{
OverwritePrompt = true,
ValidateNames = true,
Title = "Save Window Screenshot",
FileName = "WindowScreenshot",
DefaultExt = "bmp",
RestoreDirectory = false,
Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf"
};
if (dialog.ShowDialog(window.Win32Window) == DialogResult.OK)
{
var fileExtension = Path.GetExtension(dialog.FileName).ToLower();
var imageFormat = fileExtension == ".bmp" ? ImageFormat.Bmp :
fileExtension == ".gif" ? ImageFormat.Gif :
fileExtension == ".jpeg" ? ImageFormat.Jpeg :
fileExtension == ".png" ? ImageFormat.Png :
fileExtension == ".tiff" ? ImageFormat.Tiff : ImageFormat.Wmf;
bitmap.Save(dialog.FileName, imageFormat);
}
}
break;

case SystemMenu.SC_TOPMOST:
{
Boolean r = window.Menu.IsMenuItemChecked(SystemMenu.SC_TOPMOST);
window.Menu.CheckMenuItem(SystemMenu.SC_TOPMOST, !r);
window.MakeTopMost(!r);
} break;
}
break;

case SystemMenu.SC_ROLLUP:
{
Expand All @@ -346,7 +377,8 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
{
window.UnRollUp();
}
} break;
}
break;


case SystemMenu.SC_SIZE_DEFAULT:
Expand All @@ -355,49 +387,52 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
window.Menu.CheckMenuItem(SystemMenu.SC_SIZE_DEFAULT, true);
window.ShowNormal();
window.RestoreSize();
} break;
}
break;

case SystemMenu.SC_SIZE_CUSTOM:
{
SizeForm sizeForm = new SizeForm(window);
sizeForm.Show();
Window.ForceForegroundWindow(sizeForm.Handle);
} break;
var sizeForm = new SizeForm(window);
sizeForm.Show(window.Win32Window);
}
break;

case SystemMenu.SC_TRANS_DEFAULT:
{
window.Menu.UncheckTransparencyMenu();
window.Menu.CheckMenuItem(SystemMenu.SC_TRANS_DEFAULT, true);
window.RestoreTransparency();
} break;
}
break;

case SystemMenu.SC_TRANS_CUSTOM:
{
TransparencyForm opacityForm = new TransparencyForm(window);
opacityForm.Show();
Window.ForceForegroundWindow(opacityForm.Handle);
} break;
var opacityForm = new TransparencyForm(window);
opacityForm.Show(window.Win32Window);
}
break;

case SystemMenu.SC_ALIGN_DEFAULT:
{
window.Menu.UncheckAlignmentMenu();
window.Menu.CheckMenuItem(SystemMenu.SC_ALIGN_DEFAULT, true);
window.RestorePosition();
} break;
}
break;

case SystemMenu.SC_ALIGN_CUSTOM:
{
PositionForm positionForm = new PositionForm(window);
positionForm.Show();
Window.ForceForegroundWindow(positionForm.Handle);
} break;
var positionForm = new PositionForm(window);
positionForm.Show(window.Win32Window);
}
break;

case SystemMenu.SC_ALIGN_MONITOR:
{
ScreenForm screenForm = new ScreenForm(window);
screenForm.Show();
Window.ForceForegroundWindow(screenForm.Handle);
} break;
var screenForm = new ScreenForm(window);
screenForm.Show(window.Win32Window);
}
break;

case SystemMenu.SC_SIZE_640_480: SetSizeMenuItem(window, SystemMenu.SC_SIZE_640_480, 640, 480); break;
case SystemMenu.SC_SIZE_720_480: SetSizeMenuItem(window, SystemMenu.SC_SIZE_720_480, 720, 480); break;
Expand Down
6 changes: 0 additions & 6 deletions SmartSystemMenu/Code/Forms/PositionForm.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
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 SmartSystemMenu.Code.Common;

Expand Down
1 change: 1 addition & 0 deletions SmartSystemMenu/SmartSystemMenu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Code\Common\PlatformUtility.cs" />
<Compile Include="Code\Common\SystemMenu.cs" />
<Compile Include="Code\Common\SystemTrayMenu.cs" />
<Compile Include="Code\Common\Win32WindowWrapper.cs" />
<Compile Include="Code\Common\WindowAlignment.cs" />
<Compile Include="Code\Forms\AboutForm.cs">
<SubType>Form</SubType>
Expand Down

0 comments on commit 3f87e01

Please sign in to comment.