Added SafeHandles, see here: http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396335.aspx to some of the Greenshot code. Not all possible handles have been converted, this should be done to prevent more possible memory/resource leaks.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2427 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-01-13 15:53:35 +00:00
commit 502abed36a
5 changed files with 87 additions and 42 deletions

View file

@ -498,33 +498,32 @@ namespace GreenshotPlugin.Core {
capture = new Capture();
}
int x,y;
IntPtr hicon;
CursorInfo cursorInfo = new CursorInfo();
IconInfo iconInfo;
cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
if (User32.GetCursorInfo(out cursorInfo)) {
if (cursorInfo.flags == User32.CURSOR_SHOWING) {
hicon = User32.CopyIcon(cursorInfo.hCursor);
if (User32.GetIconInfo(hicon, out iconInfo)) {
Point cursorLocation = GetCursorLocation();
// Allign cursor location to Bitmap coordinates (instead of Screen coordinates)
x = cursorLocation.X - iconInfo.xHotspot - capture.ScreenBounds.X;
y = cursorLocation.Y - iconInfo.yHotspot - capture.ScreenBounds.Y;
// Set the location
capture.CursorLocation = new Point(x, y);
using (Icon icon = Icon.FromHandle(hicon)) {
capture.Cursor = icon;
}
if (iconInfo.hbmMask != IntPtr.Zero) {
GDI32.DeleteObject(iconInfo.hbmMask);
}
if (iconInfo.hbmColor != IntPtr.Zero) {
GDI32.DeleteObject(iconInfo.hbmColor);
using (SafeIconHandle safeIcon = User32.CopyIcon(cursorInfo.hCursor)) {
if (User32.GetIconInfo(safeIcon, out iconInfo)) {
Point cursorLocation = GetCursorLocation();
// Allign cursor location to Bitmap coordinates (instead of Screen coordinates)
x = cursorLocation.X - iconInfo.xHotspot - capture.ScreenBounds.X;
y = cursorLocation.Y - iconInfo.yHotspot - capture.ScreenBounds.Y;
// Set the location
capture.CursorLocation = new Point(x, y);
using (Icon icon = Icon.FromHandle(safeIcon.DangerousGetHandle())) {
capture.Cursor = icon;
}
if (iconInfo.hbmMask != IntPtr.Zero) {
GDI32.DeleteObject(iconInfo.hbmMask);
}
if (iconInfo.hbmColor != IntPtr.Zero) {
GDI32.DeleteObject(iconInfo.hbmColor);
}
}
}
User32.DestroyIcon(hicon);
}
}
return capture;

View file

@ -627,7 +627,11 @@ namespace GreenshotPlugin.Core {
return User32.IsIconic(this.hWnd) || Location.X <= -32000;
}
set {
User32.SendMessage(this.hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero);
if (value) {
User32.SendMessage(this.hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero);
} else {
User32.SendMessage(this.hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero);
}
}
}
@ -660,7 +664,7 @@ namespace GreenshotPlugin.Core {
if (monitor != IntPtr.Zero) {
if (appVisibility != null) {
MONITOR_APP_VISIBILITY monitorAppVisibility = appVisibility.GetAppVisibilityOnMonitor(monitor);
LOG.DebugFormat("App visible: {0}", monitorAppVisibility);
LOG.DebugFormat("App {0} visible: {1} on {2}", Text, monitorAppVisibility, screen.Bounds);
if (monitorAppVisibility == MONITOR_APP_VISIBILITY.MAV_APP_VISIBLE) {
return true;
}
@ -1097,6 +1101,7 @@ namespace GreenshotPlugin.Core {
/// <returns>Bitmap with transparency</returns>
private Bitmap ApplyTransparency(Bitmap blackBitmap, Bitmap whiteBitmap) {
Bitmap returnBitmap = new Bitmap(blackBitmap.Width, blackBitmap.Height, PixelFormat.Format32bppArgb);
returnBitmap.SetResolution(blackBitmap.HorizontalResolution, blackBitmap.VerticalResolution);
using (BitmapBuffer blackBuffer = new BitmapBuffer(blackBitmap, false)) {
blackBuffer.Lock();
using (BitmapBuffer whiteBuffer = new BitmapBuffer(whiteBitmap, false)) {