Merge pull request #1567 from campbeb/greenshotmerge21May

Merge Greenshot changes through 22 May 2016
This commit is contained in:
Jaex 2016-05-22 18:36:30 +03:00
commit c224117fe2
87 changed files with 1269 additions and 1130 deletions

View file

@ -205,6 +205,9 @@ public class CoreConfiguration : IniSection, INotifyPropertyChanged
[IniProperty("OptimizeForRDP", Description = "Make some optimizations for usage with remote desktop", DefaultValue = "False")]
public bool OptimizeForRDP;
[IniProperty("DisableRDPOptimizing", Description = "Disable all optimizations for usage with remote desktop", DefaultValue = "False")]
public bool DisableRDPOptimizing;
[IniProperty("MinimizeWorkingSetSize", Description = "Optimize memory footprint, but with a performance penalty!", DefaultValue = "False")]
public bool MinimizeWorkingSetSize;

View file

@ -77,7 +77,7 @@ public override void AfterLoad()
/// <param name="requestingType">Type of the class for which to create the field</param>
/// <param name="fieldType">FieldType of the field to construct</param>
/// <param name="scope">FieldType of the field to construct</param>
/// <param name="preferredDefaultValue">FieldType of the field to construct</param>
/// <returns>a new Field of the given fieldType, with the scope of it's value being restricted to the Type scope</returns>
public Field CreateField(Type requestingType, FieldType fieldType, object preferredDefaultValue)
{

View file

@ -32,7 +32,7 @@ namespace GreenshotPlugin.Controls
/// </summary>
internal partial class BackgroundForm : Form
{
private volatile bool shouldClose = false;
private volatile bool _shouldClose;
private void BackgroundShowDialog()
{
@ -43,7 +43,7 @@ public static BackgroundForm ShowAndWait(string title, string text)
{
BackgroundForm backgroundForm = new BackgroundForm(title, text);
// Show form in background thread
Thread backgroundTask = new Thread(new ThreadStart(backgroundForm.BackgroundShowDialog));
Thread backgroundTask = new Thread(backgroundForm.BackgroundShowDialog);
backgroundForm.Name = "Background form";
backgroundTask.IsBackground = true;
backgroundTask.SetApartmentState(ApartmentState.STA);
@ -58,7 +58,7 @@ public BackgroundForm(string title, string text)
//
InitializeComponent();
Icon = GreenshotResources.getGreenshotIcon();
shouldClose = false;
_shouldClose = false;
Text = title;
label_pleasewait.Text = text;
FormClosing += PreventFormClose;
@ -87,7 +87,7 @@ public new void Show()
private void PreventFormClose(object sender, FormClosingEventArgs e)
{
if (!shouldClose)
if (!_shouldClose)
{
e.Cancel = true;
}
@ -95,7 +95,7 @@ private void PreventFormClose(object sender, FormClosingEventArgs e)
private void Timer_checkforcloseTick(object sender, EventArgs e)
{
if (shouldClose)
if (_shouldClose)
{
timer_checkforclose.Stop();
BeginInvoke(new EventHandler(delegate { Close(); }));
@ -104,7 +104,7 @@ private void Timer_checkforcloseTick(object sender, EventArgs e)
public void CloseDialog()
{
shouldClose = true;
_shouldClose = true;
Application.DoEvents();
}

View file

@ -40,7 +40,7 @@ public string LanguageKey
set;
}
public BindableToolStripButton() : base()
public BindableToolStripButton()
{
CheckedChanged += BindableToolStripButton_CheckedChanged;
}

View file

@ -40,7 +40,7 @@ public string LanguageKey
set;
}
public BindableToolStripComboBox() : base()
public BindableToolStripComboBox()
{
SelectedIndexChanged += BindableToolStripComboBox_SelectedIndexChanged;
}

View file

@ -41,10 +41,6 @@ public string LanguageKey
set;
}
public BindableToolStripDropDownButton()
{
}
public object SelectedTag
{
get { if (Tag == null && DropDownItems.Count > 0) Tag = DropDownItems[0].Tag; return Tag; }

View file

@ -51,11 +51,14 @@ public FontFamily FontFamily
public FontFamilyComboBox() : base()
{
ComboBox.DataSource = FontFamily.Families;
ComboBox.DisplayMember = "Name";
SelectedIndexChanged += BindableToolStripComboBox_SelectedIndexChanged;
ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
ComboBox.DrawItem += ComboBox_DrawItem;
if (ComboBox != null)
{
ComboBox.DataSource = FontFamily.Families;
ComboBox.DisplayMember = "Name";
SelectedIndexChanged += BindableToolStripComboBox_SelectedIndexChanged;
ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
ComboBox.DrawItem += ComboBox_DrawItem;
}
}
private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
@ -69,7 +72,7 @@ private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
FontFamily fontFamily = Items[e.Index] as FontFamily;
FontStyle fontStyle = FontStyle.Regular;
if (!fontFamily.IsStyleAvailable(FontStyle.Regular))
if (fontFamily != null && !fontFamily.IsStyleAvailable(FontStyle.Regular))
{
if (fontFamily.IsStyleAvailable(FontStyle.Bold))
{
@ -90,12 +93,18 @@ private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
}
try
{
DrawText(e.Graphics, fontFamily, fontStyle, e.Bounds, fontFamily.Name);
if (fontFamily != null)
{
DrawText(e.Graphics, fontFamily, fontStyle, e.Bounds, fontFamily.Name);
}
}
catch
{
// If the drawing failed, BUG-1770 seems to have a weird case that causes: Font 'Lucida Sans Typewriter' does not support style 'Regular'
DrawText(e.Graphics, FontFamily.GenericSansSerif, FontStyle.Regular, e.Bounds, fontFamily.Name);
if (fontFamily != null)
{
// If the drawing failed, BUG-1770 seems to have a weird case that causes: Font 'Lucida Sans Typewriter' does not support style 'Regular'
DrawText(e.Graphics, FontFamily.GenericSansSerif, FontStyle.Regular, e.Bounds, fontFamily.Name);
}
}
}
// Uncomment this if you actually like the way the focus rectangle looks
@ -112,7 +121,7 @@ private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
/// <param name="text"></param>
private void DrawText(Graphics graphics, FontFamily fontFamily, FontStyle fontStyle, Rectangle bounds, string text)
{
using (Font font = new Font(fontFamily, this.Font.Size + 5, fontStyle, GraphicsUnit.Pixel))
using (Font font = new Font(fontFamily, Font.Size + 5, fontStyle, GraphicsUnit.Pixel))
{
// Make sure the text is visible by centering it in the line
using (StringFormat stringFormat = new StringFormat())

View file

@ -36,19 +36,8 @@ public string LanguageKey
set;
}
private string sectionName = "Core";
[Category("Greenshot"), DefaultValue("Core"), Description("Specifies the Ini-Section to map this control with.")]
public string SectionName
{
get
{
return sectionName;
}
set
{
sectionName = value;
}
}
public string SectionName { get; set; } = "Core";
[Category("Greenshot"), DefaultValue(null), Description("Specifies the property name to map the configuration.")]
public string PropertyName

View file

@ -32,15 +32,15 @@ internal class GreenshotColumnSorter : IComparer
/// <summary>
/// Specifies the column to be sorted
/// </summary>
private int ColumnToSort;
private int _columnToSort;
/// <summary>
/// Specifies the order in which to sort (i.e. 'Ascending').
/// </summary>
private SortOrder OrderOfSort;
private SortOrder _orderOfSort;
/// <summary>
/// Case insensitive comparer object
/// </summary>
private CaseInsensitiveComparer ObjectCompare;
private readonly CaseInsensitiveComparer _objectCompare;
/// <summary>
/// Class constructor. Initializes various elements
@ -48,13 +48,13 @@ internal class GreenshotColumnSorter : IComparer
public GreenshotColumnSorter()
{
// Initialize the column to '0'
ColumnToSort = 0;
_columnToSort = 0;
// Initialize the sort order to 'none'
OrderOfSort = SortOrder.None;
_orderOfSort = SortOrder.None;
// Initialize the CaseInsensitiveComparer object
ObjectCompare = new CaseInsensitiveComparer();
_objectCompare = new CaseInsensitiveComparer();
}
/// <summary>
@ -65,44 +65,39 @@ public GreenshotColumnSorter()
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
if (x == null && y == null)
{
return 0;
}
else if (x == null && y != null)
if (x == null)
{
return -1;
}
else if (x != null && y == null)
if (y == null)
{
return 1;
}
// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
var listviewX = (ListViewItem)x;
var listviewY = (ListViewItem)y;
// Compare the two items
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
var compareResult = _objectCompare.Compare(listviewX.SubItems[_columnToSort].Text, listviewY.SubItems[_columnToSort].Text);
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
if (_orderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
if (_orderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
// Return '0' to indicate they are equal
return 0;
}
/// <summary>
@ -112,11 +107,11 @@ public int SortColumn
{
set
{
ColumnToSort = value;
_columnToSort = value;
}
get
{
return ColumnToSort;
return _columnToSort;
}
}
@ -127,11 +122,11 @@ public SortOrder Order
{
set
{
OrderOfSort = value;
_orderOfSort = value;
}
get
{
return OrderOfSort;
return _orderOfSort;
}
}
}

View file

@ -27,21 +27,11 @@ namespace GreenshotPlugin.Controls
{
internal class GreenshotComboBox : ComboBox, IGreenshotConfigBindable
{
private Type enumType = null;
private Enum selectedEnum = null;
private string sectionName = "Core";
private Type _enumType = null;
private Enum _selectedEnum = null;
[Category("Greenshot"), DefaultValue("Core"), Description("Specifies the Ini-Section to map this control with.")]
public string SectionName
{
get
{
return sectionName;
}
set
{
sectionName = value;
}
}
public string SectionName { get; set; } = "Core";
[Category("Greenshot"), DefaultValue(null), Description("Specifies the property name to map the configuration.")]
public string PropertyName
@ -62,7 +52,7 @@ public void SetValue(Enum currentValue)
{
if (currentValue != null)
{
selectedEnum = currentValue;
_selectedEnum = currentValue;
SelectedItem = currentValue;
}
}
@ -75,11 +65,10 @@ public void SetValue(Enum currentValue)
public void Populate(Type enumType)
{
// Store the enum-type, so we can work with it
this.enumType = enumType;
_enumType = enumType;
var availableValues = Enum.GetValues(enumType);
Items.Clear();
string enumTypeName = enumType.Name;
foreach (var enumValue in availableValues)
{
Items.Add(enumValue);
@ -91,20 +80,20 @@ public void Populate(Type enumType)
/// </summary>
private void StoreSelectedEnum()
{
string enumTypeName = enumType.Name;
string enumTypeName = _enumType.Name;
string selectedValue = SelectedItem as string;
var availableValues = Enum.GetValues(enumType);
var availableValues = Enum.GetValues(_enumType);
object returnValue = null;
try
{
returnValue = Enum.Parse(enumType, selectedValue);
returnValue = Enum.Parse(_enumType, selectedValue);
}
catch (Exception)
{
}
selectedEnum = (Enum)returnValue;
_selectedEnum = (Enum)returnValue;
}
/// <summary>
@ -113,7 +102,7 @@ private void StoreSelectedEnum()
/// <returns>The enum value of the combobox</returns>
public Enum GetSelectedEnum()
{
return selectedEnum;
return _selectedEnum;
}
}
}

View file

@ -36,9 +36,9 @@ namespace GreenshotPlugin.Controls
public class GreenshotForm : Form, IGreenshotLanguageBindable
{
protected static CoreConfiguration coreConfiguration;
private static IDictionary<Type, FieldInfo[]> reflectionCache = new Dictionary<Type, FieldInfo[]>();
private static readonly IDictionary<Type, FieldInfo[]> reflectionCache = new Dictionary<Type, FieldInfo[]>();
private IComponentChangeService m_changeService;
private bool _storeFieldsManually = false;
private bool _storeFieldsManually;
private IDictionary<string, Control> _designTimeControls;
private IDictionary<string, ToolStripItem> _designTimeToolStripItems;
@ -184,7 +184,7 @@ private void ClearChangeNotifications()
// Clear our the component change events to prepare for re-siting.
if (m_changeService != null)
{
m_changeService.ComponentAdded -= new ComponentEventHandler(OnComponentAdded);
m_changeService.ComponentAdded -= OnComponentAdded;
}
}
@ -193,7 +193,7 @@ private void RegisterChangeNotifications()
// Register the event handlers for the IComponentChangeService events
if (m_changeService != null)
{
m_changeService.ComponentAdded += new ComponentEventHandler(OnComponentAdded);
m_changeService.ComponentAdded += OnComponentAdded;
}
}
@ -303,7 +303,6 @@ protected void FillFields()
comboxBox.Populate(iniValue.ValueType);
comboxBox.SetValue((Enum)iniValue.Value);
comboxBox.Enabled = !iniValue.IsFixed;
continue;
}
}
}

View file

@ -26,19 +26,8 @@ namespace GreenshotPlugin.Controls
{
internal class GreenshotTextBox : TextBox, IGreenshotConfigBindable
{
private string sectionName = "Core";
[Category("Greenshot"), DefaultValue("Core"), Description("Specifies the Ini-Section to map this control with.")]
public string SectionName
{
get
{
return sectionName;
}
set
{
sectionName = value;
}
}
public string SectionName { get; set; } = "Core";
[Category("Greenshot"), DefaultValue(null), Description("Specifies the property name to map the configuration.")]
public string PropertyName

View file

@ -36,11 +36,9 @@ private enum NativeConstants : uint
{
MA_ACTIVATE = 1,
MA_ACTIVATEANDEAT = 2,
MA_NOACTIVATE = 3,
MA_NOACTIVATEANDEAT = 4,
}
private bool clickThrough = false;
private bool _clickThrough;
/// <summary>
/// Gets or sets whether the ToolStripEx honors item clicks when its containing form does not have input focus.
/// </summary>
@ -51,19 +49,19 @@ public bool ClickThrough
{
get
{
return clickThrough;
return _clickThrough;
}
set
{
clickThrough = value;
_clickThrough = value;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (clickThrough && m.Msg == WM_MOUSEACTIVATE && m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
if (_clickThrough && m.Msg == WM_MOUSEACTIVATE && m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
{
m.Result = (IntPtr)NativeConstants.MA_ACTIVATE;
}

View file

@ -19,15 +19,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// <summary>
/// See: http://nickstips.wordpress.com/2010/03/03/c-panel-resets-scroll-position-after-focus-is-lost-and-regained/
/// </summary>
using System.Drawing;
using System.Windows.Forms;
namespace GreenshotPlugin.Controls
{
/// <summary>
/// See: http://nickstips.wordpress.com/2010/03/03/c-panel-resets-scroll-position-after-focus-is-lost-and-regained/
/// </summary>
internal class NonJumpingPanel : Panel
{
protected override Point ScrollToControl(Control activeControl)
@ -36,5 +35,23 @@ protected override Point ScrollToControl(Control activeControl)
// scrolling to the active control when the panel loses and regains focus
return DisplayRectangle.Location;
}
/// <summary>
/// Add horizontal scrolling to the panel, when using the wheel and the shift key is pressed
/// </summary>
/// <param name="e">MouseEventArgs</param>
protected override void OnMouseWheel(MouseEventArgs e)
{
if (VScroll && (ModifierKeys & Keys.Shift) == Keys.Shift)
{
VScroll = false;
base.OnMouseWheel(e);
VScroll = true;
}
else
{
base.OnMouseWheel(e);
}
}
}
}

View file

@ -37,7 +37,7 @@ internal class Pipette : Label, IMessageFilter, IDisposable
private MovableShowColorForm movableShowColorForm;
private bool dragging;
private Cursor _cursor;
private Bitmap _image;
private readonly Bitmap _image;
private const int VK_ESC = 27;
public event EventHandler<PipetteUsedArgs> PipetteUsed;
@ -129,7 +129,10 @@ protected override void OnMouseUp(MouseEventArgs e)
{
//Release Capture should consume MouseUp when canceled with the escape key
User32.ReleaseCapture();
PipetteUsed(this, new PipetteUsedArgs(movableShowColorForm.color));
if (PipetteUsed != null)
{
PipetteUsed(this, new PipetteUsedArgs(movableShowColorForm.color));
}
}
base.OnMouseUp(e);
}

View file

@ -31,7 +31,7 @@ namespace GreenshotPlugin.Controls
/// </summary>
internal partial class PleaseWaitForm : Form
{
private Thread waitFor = null;
private Thread waitFor;
private string title;
public PleaseWaitForm()

View file

@ -31,7 +31,7 @@ namespace GreenshotPlugin.Controls
/// </summary>
internal partial class QualityDialog : GreenshotForm
{
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
public SurfaceOutputSettings Settings
{
get;

View file

@ -34,11 +34,11 @@ namespace GreenshotPlugin.Controls
/// </summary>
internal class SaveImageFileDialog : IDisposable
{
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
protected SaveFileDialog saveFileDialog;
private FilterOption[] filterOptions;
private DirectoryInfo eagerlyCreatedDirectory;
private ICaptureDetails captureDetails = null;
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
protected SaveFileDialog SaveFileDialog;
private FilterOption[] _filterOptions;
private DirectoryInfo _eagerlyCreatedDirectory;
private readonly ICaptureDetails _captureDetails = null;
public void Dispose()
{
@ -50,29 +50,29 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (saveFileDialog != null)
if (SaveFileDialog != null)
{
saveFileDialog.Dispose();
saveFileDialog = null;
SaveFileDialog.Dispose();
SaveFileDialog = null;
}
}
}
public SaveImageFileDialog()
{
init();
Init();
}
public SaveImageFileDialog(ICaptureDetails captureDetails)
{
this.captureDetails = captureDetails;
init();
_captureDetails = captureDetails;
Init();
}
private void init()
private void Init()
{
saveFileDialog = new SaveFileDialog();
applyFilterOptions();
SaveFileDialog = new SaveFileDialog();
ApplyFilterOptions();
string initialDirectory = null;
try
{
@ -85,50 +85,52 @@ private void init()
if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
{
saveFileDialog.InitialDirectory = initialDirectory;
SaveFileDialog.InitialDirectory = initialDirectory;
}
else if (Directory.Exists(conf.OutputFilePath))
{
saveFileDialog.InitialDirectory = conf.OutputFilePath;
SaveFileDialog.InitialDirectory = conf.OutputFilePath;
}
// The following property fixes a problem that the directory where we save is locked (bug #2899790)
saveFileDialog.RestoreDirectory = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.CheckPathExists = false;
saveFileDialog.AddExtension = true;
SaveFileDialog.RestoreDirectory = true;
SaveFileDialog.OverwritePrompt = true;
SaveFileDialog.CheckPathExists = false;
SaveFileDialog.AddExtension = true;
ApplySuggestedValues();
}
private void applyFilterOptions()
private void ApplyFilterOptions()
{
prepareFilterOptions();
PrepareFilterOptions();
string fdf = "";
int preselect = 0;
var outputFileFormatAsString = Enum.GetName(typeof(OutputFormat), conf.OutputFileFormat);
for (int i = 0; i < filterOptions.Length; i++)
for (int i = 0; i < _filterOptions.Length; i++)
{
FilterOption fo = filterOptions[i];
FilterOption fo = _filterOptions[i];
fdf += fo.Label + "|*." + fo.Extension + "|";
if (outputFileFormatAsString == fo.Extension)
preselect = i;
}
fdf = fdf.Substring(0, fdf.Length - 1);
saveFileDialog.Filter = fdf;
saveFileDialog.FilterIndex = preselect + 1;
SaveFileDialog.Filter = fdf;
SaveFileDialog.FilterIndex = preselect + 1;
}
private void prepareFilterOptions()
private void PrepareFilterOptions()
{
OutputFormat[] supportedImageFormats = (OutputFormat[])Enum.GetValues(typeof(OutputFormat));
filterOptions = new FilterOption[supportedImageFormats.Length];
for (int i = 0; i < filterOptions.Length; i++)
_filterOptions = new FilterOption[supportedImageFormats.Length];
for (int i = 0; i < _filterOptions.Length; i++)
{
string ifo = supportedImageFormats[i].ToString();
if (ifo.ToLower().Equals("jpeg")) ifo = "Jpg"; // we dont want no jpeg files, so let the dialog check for jpg
FilterOption fo = new FilterOption();
fo.Label = ifo.ToUpper();
fo.Extension = ifo.ToLower();
filterOptions.SetValue(fo, i);
FilterOption fo = new FilterOption
{
Label = ifo.ToUpper(),
Extension = ifo.ToLower()
};
_filterOptions.SetValue(fo, i);
}
}
@ -137,8 +139,8 @@ private void prepareFilterOptions()
/// </summary>
public string FileName
{
get { return saveFileDialog.FileName; }
set { saveFileDialog.FileName = value; }
get { return SaveFileDialog.FileName; }
set { SaveFileDialog.FileName = value; }
}
/// <summary>
@ -146,8 +148,8 @@ public string FileName
/// </summary>
public string InitialDirectory
{
get { return saveFileDialog.InitialDirectory; }
set { saveFileDialog.InitialDirectory = value; }
get { return SaveFileDialog.InitialDirectory; }
set { SaveFileDialog.InitialDirectory = value; }
}
/// <summary>
@ -159,7 +161,7 @@ public string FileNameWithExtension
{
get
{
string fn = saveFileDialog.FileName;
string fn = SaveFileDialog.FileName;
// if the filename contains a valid extension, which is the same like the selected filter item's extension, the filename is okay
if (fn.EndsWith(Extension, StringComparison.CurrentCultureIgnoreCase)) return fn;
// otherwise we just add the selected filter item's extension
@ -179,15 +181,15 @@ public string Extension
{
get
{
return filterOptions[saveFileDialog.FilterIndex - 1].Extension;
return _filterOptions[SaveFileDialog.FilterIndex - 1].Extension;
}
set
{
for (int i = 0; i < filterOptions.Length; i++)
for (int i = 0; i < _filterOptions.Length; i++)
{
if (value.Equals(filterOptions[i].Extension, StringComparison.CurrentCultureIgnoreCase))
if (value.Equals(_filterOptions[i].Extension, StringComparison.CurrentCultureIgnoreCase))
{
saveFileDialog.FilterIndex = i + 1;
SaveFileDialog.FilterIndex = i + 1;
}
}
}
@ -195,7 +197,7 @@ public string Extension
public DialogResult ShowDialog()
{
DialogResult ret = saveFileDialog.ShowDialog();
DialogResult ret = SaveFileDialog.ShowDialog();
CleanUp();
return ret;
}
@ -203,18 +205,10 @@ public DialogResult ShowDialog()
/// <summary>
/// sets InitialDirectory and FileName property of a SaveFileDialog smartly, considering default pattern and last used path
/// </summary>
/// <param name="sfd">a SaveFileDialog instance</param>
private void ApplySuggestedValues()
{
// build the full path and set dialog properties
FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails);
}
private string GetRootDirFromConfig()
{
string rootDir = conf.OutputFilePath;
rootDir = FilenameHelper.FillVariables(rootDir, false);
return rootDir;
FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, _captureDetails);
}
private class FilterOption
@ -228,37 +222,18 @@ private void CleanUp()
// fix for bug #3379053
try
{
if (eagerlyCreatedDirectory != null && eagerlyCreatedDirectory.GetFiles().Length == 0 && eagerlyCreatedDirectory.GetDirectories().Length == 0)
if (_eagerlyCreatedDirectory != null && _eagerlyCreatedDirectory.GetFiles().Length == 0 && _eagerlyCreatedDirectory.GetDirectories().Length == 0)
{
eagerlyCreatedDirectory.Delete();
eagerlyCreatedDirectory = null;
_eagerlyCreatedDirectory.Delete();
_eagerlyCreatedDirectory = null;
}
}
catch (Exception e)
{
LOG.WarnFormat("Couldn't cleanup directory due to: {0}", e.Message);
eagerlyCreatedDirectory = null;
_eagerlyCreatedDirectory = null;
}
}
private string CreateDirectoryIfNotExists(string fullPath)
{
string dirName = null;
try
{
dirName = Path.GetDirectoryName(fullPath);
DirectoryInfo di = new DirectoryInfo(dirName);
if (!di.Exists)
{
di = Directory.CreateDirectory(dirName);
eagerlyCreatedDirectory = di;
}
}
catch (Exception e)
{
LOG.Error("Error in CreateDirectoryIfNotExists", e);
}
return dirName;
}
}
}

View file

@ -36,11 +36,9 @@ private enum NativeConstants : uint
{
MA_ACTIVATE = 1,
MA_ACTIVATEANDEAT = 2,
MA_NOACTIVATE = 3,
MA_NOACTIVATEANDEAT = 4,
}
private bool clickThrough = false;
private bool _clickThrough = false;
/// <summary>
/// Gets or sets whether the ToolStripEx honors item clicks when its containing form does not have input focus.
/// </summary>
@ -52,19 +50,19 @@ public bool ClickThrough
{
get
{
return clickThrough;
return _clickThrough;
}
set
{
clickThrough = value;
_clickThrough = value;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (clickThrough && m.Msg == WM_MOUSEACTIVATE && m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
if (_clickThrough && m.Msg == WM_MOUSEACTIVATE && m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
{
m.Result = (IntPtr)NativeConstants.MA_ACTIVATE;
}

View file

@ -32,10 +32,10 @@ namespace GreenshotPlugin.Core
/// <typeparam name="TV">Type of value</typeparam>
public class Cache<TK, TV>
{
private IDictionary<TK, TV> internalCache = new Dictionary<TK, TV>();
private object lockObject = new object();
private int secondsToExpire = 10;
private CacheObjectExpired expiredCallback = null;
private readonly IDictionary<TK, TV> internalCache = new Dictionary<TK, TV>();
private readonly object lockObject = new object();
private readonly int secondsToExpire = 10;
private readonly CacheObjectExpired expiredCallback = null;
public delegate void CacheObjectExpired(TK key, TV cacheValue);
/// <summary>
@ -82,9 +82,12 @@ public IEnumerable<TV> Elements
{
List<TV> elements = new List<TV>();
foreach (TV element in internalCache.Values)
lock (lockObject)
{
elements.Add(element);
foreach (TV element in internalCache.Values)
{
elements.Add(element);
}
}
foreach (TV element in elements)
{
@ -121,7 +124,10 @@ public IEnumerable<TV> Elements
/// <returns>true if the cache contains the key</returns>
public bool Contains(TK key)
{
return internalCache.ContainsKey(key);
lock (lockObject)
{
return internalCache.ContainsKey(key);
}
}
/// <summary>
@ -198,7 +204,7 @@ public void Remove(TK key)
private class CachedItem
{
public event CacheObjectExpired Expired;
private int secondsToExpire;
private readonly int secondsToExpire;
private readonly Timer _timerEvent;
public CachedItem(TK key, TV item, int secondsToExpire)

View file

@ -396,11 +396,11 @@ private static Image GetImage(IDataObject dataObject)
{
// Outlook ??
LOG.Info("Most likely the current clipboard contents come from Outlook, as this has a problem with PNG and others we place the DIB format to the front...");
retrieveFormats = new string[] { DataFormats.Dib, FORMAT_BITMAP, FORMAT_FILECONTENTS, FORMAT_PNG_OFFICEART, FORMAT_PNG, FORMAT_JFIF_OFFICEART, FORMAT_JPG, FORMAT_JFIF, DataFormats.Tiff, FORMAT_GIF };
retrieveFormats = new[] { DataFormats.Dib, FORMAT_BITMAP, FORMAT_FILECONTENTS, FORMAT_PNG_OFFICEART, FORMAT_PNG, FORMAT_JFIF_OFFICEART, FORMAT_JPG, FORMAT_JFIF, DataFormats.Tiff, FORMAT_GIF };
}
else
{
retrieveFormats = new string[] { FORMAT_PNG_OFFICEART, FORMAT_PNG, FORMAT_17, FORMAT_JFIF_OFFICEART, FORMAT_JPG, FORMAT_JFIF, DataFormats.Tiff, DataFormats.Dib, FORMAT_BITMAP, FORMAT_FILECONTENTS, FORMAT_GIF };
retrieveFormats = new[] { FORMAT_PNG_OFFICEART, FORMAT_PNG, FORMAT_17, FORMAT_JFIF_OFFICEART, FORMAT_JPG, FORMAT_JFIF, DataFormats.Tiff, DataFormats.Dib, FORMAT_BITMAP, FORMAT_FILECONTENTS, FORMAT_GIF };
}
foreach (string currentFormat in retrieveFormats)
{
@ -867,7 +867,7 @@ public static List<string> GetFormats(IDataObject dataObj)
/// <returns>true if one the format is found</returns>
public static bool ContainsFormat(string format)
{
return ContainsFormat(GetDataObject(), new string[] { format });
return ContainsFormat(GetDataObject(), new[] { format });
}
/// <summary>
@ -877,7 +877,7 @@ public static bool ContainsFormat(string format)
/// <returns>true if one the format is found</returns>
public static bool ContainsFormat(IDataObject dataObject, string format)
{
return ContainsFormat(dataObject, new string[] { format });
return ContainsFormat(dataObject, new[] { format });
}
/// <summary>

View file

@ -97,7 +97,7 @@ public virtual Image Apply(Image sourceImage, Matrix matrix)
[TypeConverter(typeof(EffectConverter))]
public class TornEdgeEffect : DropShadowEffect
{
public TornEdgeEffect() : base()
public TornEdgeEffect()
{
Reset();
}
@ -135,7 +135,7 @@ public override void Reset()
ToothHeight = 12;
HorizontalToothRange = 20;
VerticalToothRange = 20;
Edges = new bool[] { true, true, true, true };
Edges = new[] { true, true, true, true };
GenerateShadow = true;
}
@ -174,12 +174,12 @@ public void Reset()
/// </summary>
public class MonochromeEffect : IEffect
{
private byte threshold;
private readonly byte _threshold;
/// <param name="threshold">Threshold for monochrome filter (0 - 255), lower value means less black</param>
public MonochromeEffect(byte threshold)
{
this.threshold = threshold;
_threshold = threshold;
}
public void Reset()
@ -189,7 +189,7 @@ public void Reset()
public Image Apply(Image sourceImage, Matrix matrix)
{
return ImageHelper.CreateMonochrome(sourceImage, threshold);
return ImageHelper.CreateMonochrome(sourceImage, _threshold);
}
}
@ -198,7 +198,7 @@ public Image Apply(Image sourceImage, Matrix matrix)
/// </summary>
public class AdjustEffect : IEffect
{
public AdjustEffect() : base()
public AdjustEffect()
{
Reset();
}
@ -237,7 +237,7 @@ public Image Apply(Image sourceImage, Matrix matrix)
/// </summary>
public class ReduceColorsEffect : IEffect
{
public ReduceColorsEffect() : base()
public ReduceColorsEffect()
{
Reset();
}
@ -460,9 +460,9 @@ public Image Apply(Image sourceImage, Matrix matrix)
public class EffectConverter : TypeConverter
{
// Fix to prevent BUG-1753
private NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
private readonly NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
public EffectConverter() : base()
public EffectConverter()
{
numberFormatInfo.NumberDecimalSeparator = ".";
numberFormatInfo.NumberGroupSeparator = ",";
@ -494,7 +494,7 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
// to string
if (destinationType == typeof(string))
@ -516,7 +516,7 @@ public override object ConvertTo(ITypeDescriptorContext context, System.Globaliz
}
}
// from string
if (value.GetType() == typeof(string))
if (value is string)
{
string settings = value as string;
if (destinationType == typeof(DropShadowEffect))
@ -536,19 +536,16 @@ public override object ConvertTo(ITypeDescriptorContext context, System.Globaliz
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value != null && value.GetType() == typeof(string))
if (value != null && value is string)
{
string settings = value as string;
if (settings.Contains("ToothHeight"))
{
return ConvertTo(context, culture, value, typeof(TornEdgeEffect));
}
else
{
return ConvertTo(context, culture, value, typeof(DropShadowEffect));
}
return ConvertTo(context, culture, value, typeof(DropShadowEffect));
}
return base.ConvertFrom(context, culture, value);
}

View file

@ -25,12 +25,12 @@ namespace GreenshotPlugin.Core
{
public class EventDelay
{
private long lastCheck = 0;
private long waitTime;
private long lastCheck;
private readonly long waitTime;
public EventDelay(long ticks)
{
this.waitTime = ticks;
waitTime = ticks;
}
public bool Check()

View file

@ -343,7 +343,7 @@ public bool InvertClip
protected BitmapData bmData;
protected int stride; /* bytes per pixel row */
protected bool bitsLocked = false;
protected bool bitsLocked;
protected byte* pointer;
public static IFastBitmap Create(Bitmap source)
@ -825,8 +825,8 @@ void IFastBitmapWithOffset.SetColorAt(int x, int y, Color color)
public unsafe class FastChunkyBitmap : FastBitmap
{
// Used for indexed images
private Color[] colorEntries;
private Dictionary<Color, byte> colorCache = new Dictionary<Color, byte>();
private readonly Color[] colorEntries;
private readonly Dictionary<Color, byte> colorCache = new Dictionary<Color, byte>();
public FastChunkyBitmap(Bitmap source, Rectangle area) : base(source, area)
{

View file

@ -39,10 +39,11 @@ public static class FilenameHelper
// The parameter format is a single alpha followed by the value belonging to the parameter, e.g. :
// ${capturetime:d"yyyy-MM-dd HH_mm_ss"}
private static readonly Regex VAR_REGEXP = new Regex(@"\${(?<variable>[^:}]+)[:]?(?<parameters>[^}]*)}", RegexOptions.Compiled);
private static readonly Regex CMD_VAR_REGEXP = new Regex(@"%(?<variable>[^%]+)%", RegexOptions.Compiled);
private static readonly Regex SPLIT_REGEXP = new Regex(";(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", RegexOptions.Compiled);
private const int MAX_TITLE_LENGTH = 80;
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private const string UNSAFE_REPLACEMENT = "_";
/// <summary>
@ -79,7 +80,7 @@ public static string MakeFilenameSafe(string filename)
/// <summary>
/// Remove invalid characters from the path
/// </summary>
/// <param name="fullpath">string with the full path to a file</param>
/// <param name="path">string with the full path to a file</param>
/// <returns>string with the full path to a file, without invalid characters</returns>
public static string MakePathSafe(string path)
{
@ -180,7 +181,7 @@ private static string MatchVarEvaluatorInternal(Match match, ICaptureDetails cap
{
// Padding p<width>[,pad-character]
case "p":
string[] padParams = parameter.Substring(1).Split(new char[] { ',' });
string[] padParams = parameter.Substring(1).Split(new[] { ',' });
try
{
padWidth = int.Parse(padParams[0]);
@ -196,7 +197,7 @@ private static string MatchVarEvaluatorInternal(Match match, ICaptureDetails cap
// replace
// r<old string>,<new string>
case "r":
string[] replaceParameters = parameter.Substring(1).Split(new char[] { ',' });
string[] replaceParameters = parameter.Substring(1).Split(new[] { ',' });
if (replaceParameters != null && replaceParameters.Length == 2)
{
replacements.Add(replaceParameters[0], replaceParameters[1]);
@ -219,7 +220,7 @@ private static string MatchVarEvaluatorInternal(Match match, ICaptureDetails cap
// s<start>[,length]
case "s":
string range = parameter.Substring(1);
string[] rangelist = range.Split(new char[] { ',' });
string[] rangelist = range.Split(new[] { ',' });
if (rangelist.Length > 0)
{
try
@ -469,6 +470,51 @@ private static string MatchVarEvaluatorInternal(Match match, ICaptureDetails cap
return replaceValue;
}
/// <summary>
/// "Simply" fill the pattern with environment variables
/// </summary>
/// <param name="pattern">String with pattern %var%</param>
/// <param name="filenameSafeMode">true to make sure everything is filenamesafe</param>
/// <returns>Filled string</returns>
public static string FillCmdVariables(string pattern, bool filenameSafeMode)
{
IDictionary processVars = null;
IDictionary userVars = null;
IDictionary machineVars = null;
try
{
processVars = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process);
}
catch (Exception e)
{
LOG.Error("Error retrieving EnvironmentVariableTarget.Process", e);
}
try
{
userVars = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
}
catch (Exception e)
{
LOG.Error("Error retrieving EnvironmentVariableTarget.User", e);
}
try
{
machineVars = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);
}
catch (Exception e)
{
LOG.Error("Error retrieving EnvironmentVariableTarget.Machine", e);
}
return CMD_VAR_REGEXP.Replace(pattern,
delegate (Match m) {
return MatchVarEvaluator(m, null, processVars, userVars, machineVars, filenameSafeMode);
}
);
}
/// <summary>
/// "Simply" fill the pattern with environment variables
/// </summary>
@ -508,10 +554,10 @@ public static string FillVariables(string pattern, bool filenameSafeMode)
}
return VAR_REGEXP.Replace(pattern,
new MatchEvaluator(delegate (Match m)
delegate (Match m)
{
return MatchVarEvaluator(m, null, processVars, userVars, machineVars, filenameSafeMode);
})
}
);
}
@ -557,10 +603,10 @@ public static string FillPattern(string pattern, ICaptureDetails captureDetails,
try
{
return VAR_REGEXP.Replace(pattern,
new MatchEvaluator(delegate (Match m)
delegate (Match m)
{
return MatchVarEvaluator(m, captureDetails, processVars, userVars, machineVars, filenameSafeMode);
})
}
);
}
catch (Exception e)

View file

@ -29,7 +29,7 @@ namespace GreenshotPlugin.Core
/// </summary>
public static class GreenshotResources
{
private static ComponentResourceManager greenshotResources = new ComponentResourceManager(typeof(GreenshotResources));
private static readonly ComponentResourceManager greenshotResources = new ComponentResourceManager(typeof(GreenshotResources));
public static Image getImage(string imageName)
{

View file

@ -972,7 +972,7 @@ public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadow
{
shadowSize++;
}
bool useGDIBlur = GDIplus.isBlurPossible(shadowSize);
bool useGDIBlur = GDIplus.IsBlurPossible(shadowSize);
// Create "mask" for the shadow
ColorMatrix maskMatrix = new ColorMatrix();
maskMatrix.Matrix00 = 0;
@ -1195,12 +1195,12 @@ public static ImageAttributes CreateAdjustAttributes(float brightness, float con
{
float adjustedBrightness = brightness - 1.0f;
ColorMatrix applyColorMatrix = new ColorMatrix(
new float[][] {
new float[] {contrast, 0, 0, 0, 0}, // scale red
new float[] {0, contrast, 0, 0, 0}, // scale green
new float[] {0, 0, contrast, 0, 0}, // scale blue
new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}
new [] {
new[] {contrast, 0, 0, 0, 0}, // scale red
new[] {0, contrast, 0, 0, 0}, // scale green
new[] {0, 0, contrast, 0, 0}, // scale blue
new[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
new[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}
});
//create some image attributes
@ -1240,10 +1240,10 @@ public static Image Adjust(Image sourceImage, float brightness, float contrast,
public static Image CreateGrayscale(Image sourceImage)
{
Bitmap clone = (Bitmap)Clone(sourceImage);
ColorMatrix grayscaleMatrix = new ColorMatrix(new float[][] {
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
ColorMatrix grayscaleMatrix = new ColorMatrix(new[] {
new[] {.3f, .3f, .3f, 0, 0},
new[] {.59f, .59f, .59f, 0, 0},
new[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});

View file

@ -44,7 +44,7 @@ public static class ImageOutput
{
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly int PROPERTY_TAG_SOFTWARE_USED = 0x0131;
private static Cache<string, string> tmpFileCache = new Cache<string, string>(10 * 60 * 60, RemoveExpiredTmpFile);
private static readonly Cache<string, string> tmpFileCache = new Cache<string, string>(10 * 60 * 60, RemoveExpiredTmpFile);
/// <summary>
/// Creates a PropertyItem (Metadata) to store with the image.

View file

@ -54,12 +54,17 @@ public static class NetworkHelper
static NetworkHelper()
{
// Disable certificate checking
ServicePointManager.ServerCertificateValidationCallback +=
delegate
try
{
return true;
};
// Disable certificate checking
ServicePointManager.ServerCertificateValidationCallback += delegate {
return true;
};
}
catch (Exception ex)
{
LOG.Warn("An error has occured while allowing self-signed certificates:", ex);
}
}
/// <summary>
@ -157,15 +162,18 @@ public static Image DownloadImage(string url)
{
content = streamReader.ReadLine();
}
Regex imageUrlRegex = new Regex(@"(http|https)://.*(\.png|\.gif|\.jpg|\.tiff|\.jpeg|\.bmp)");
Match match = imageUrlRegex.Match(content);
if (match.Success)
if (!string.IsNullOrEmpty(content))
{
using (MemoryStream memoryStream2 = GetAsMemoryStream(match.Value))
Regex imageUrlRegex = new Regex(@"(http|https)://.*(\.png|\.gif|\.jpg|\.tiff|\.jpeg|\.bmp)");
Match match = imageUrlRegex.Match(content);
if (match.Success)
{
using (Image image = Image.FromStream(memoryStream2))
using (MemoryStream memoryStream2 = GetAsMemoryStream(match.Value))
{
return ImageHelper.Clone(image, PixelFormat.Format32bppArgb);
using (Image image = Image.FromStream(memoryStream2))
{
return ImageHelper.Clone(image, PixelFormat.Format32bppArgb);
}
}
}
}
@ -222,15 +230,7 @@ public static HttpWebRequest CreateWebRequest(Uri uri, HTTPMethod method)
public static HttpWebRequest CreateWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
if (Config.UseProxy)
{
webRequest.Proxy = CreateProxy(uri);
}
else
{
// BUG-1655: Fix that Greenshot always uses the default proxy even if the "use default proxy" checkbox is unset
webRequest.Proxy = null;
}
webRequest.Proxy = Config.UseProxy ? CreateProxy(uri) : null;
// Make sure the default credentials are available
webRequest.Credentials = CredentialCache.DefaultCredentials;
@ -463,10 +463,11 @@ public static void WriteMultipartFormData(Stream formDataStream, string boundary
/// Post the parameters "x-www-form-urlencoded"
/// </summary>
/// <param name="webRequest"></param>
/// <param name="parameters"></param>
public static void UploadFormUrlEncoded(HttpWebRequest webRequest, IDictionary<string, object> parameters)
{
webRequest.ContentType = "application/x-www-form-urlencoded";
string urlEncoded = NetworkHelper.GenerateQueryParameters(parameters);
string urlEncoded = GenerateQueryParameters(parameters);
byte[] data = Encoding.UTF8.GetBytes(urlEncoded);
using (var requestStream = webRequest.GetRequestStream())
@ -533,6 +534,7 @@ private static string GetResponseAsString(HttpWebResponse response)
///
/// </summary>
/// <param name="webRequest"></param>
/// <param name="alsoReturnContentOnError"></param>
/// <returns></returns>
public static string GetResponseAsString(HttpWebRequest webRequest, bool alsoReturnContentOnError)
{
@ -639,10 +641,10 @@ public interface IBinaryContainer
/// </summary>
public class ByteContainer : IBinaryContainer
{
private byte[] file;
private string fileName;
private string contentType;
private int fileSize;
private readonly byte[] _file;
private readonly string _fileName;
private readonly string _contentType;
private readonly int _fileSize;
public ByteContainer(byte[] file) : this(file, null)
{
@ -658,17 +660,10 @@ public ByteContainer(byte[] file, string filename, string contenttype) : this(fi
public ByteContainer(byte[] file, string filename, string contenttype, int filesize)
{
this.file = file;
fileName = filename;
contentType = contenttype;
if (filesize == 0)
{
fileSize = file.Length;
}
else
{
fileSize = filesize;
}
_file = file;
_fileName = filename;
_contentType = contenttype;
_fileSize = filesize == 0 ? file.Length : filesize;
}
/// <summary>
@ -677,7 +672,7 @@ public ByteContainer(byte[] file, string filename, string contenttype, int files
/// <returns>string</returns>
public string ToBase64String(Base64FormattingOptions formattingOptions)
{
return Convert.ToBase64String(file, 0, fileSize, formattingOptions);
return Convert.ToBase64String(_file, 0, _fileSize, formattingOptions);
}
/// <summary>
@ -686,7 +681,7 @@ public string ToBase64String(Base64FormattingOptions formattingOptions)
/// <returns>byte[]</returns>
public byte[] ToByteArray()
{
return file;
return _file;
}
/// <summary>
@ -701,13 +696,13 @@ public void WriteFormDataToStream(string boundary, string name, Stream formDataS
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
name,
fileName ?? name,
contentType ?? "application/octet-stream");
_fileName ?? name,
_contentType ?? "application/octet-stream");
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
// Write the file data directly to the Stream, rather than serializing it to a string.
formDataStream.Write(file, 0, fileSize);
formDataStream.Write(_file, 0, _fileSize);
}
/// <summary>
@ -717,7 +712,7 @@ public void WriteFormDataToStream(string boundary, string name, Stream formDataS
public void WriteToStream(Stream dataStream)
{
// Write the file data directly to the Stream, rather than serializing it to a string.
dataStream.Write(file, 0, fileSize);
dataStream.Write(_file, 0, _fileSize);
}
/// <summary>
@ -726,8 +721,8 @@ public void WriteToStream(Stream dataStream)
/// <param name="webRequest"></param>
public void Upload(HttpWebRequest webRequest)
{
webRequest.ContentType = contentType;
webRequest.ContentLength = fileSize;
webRequest.ContentType = _contentType;
webRequest.ContentLength = _fileSize;
using (var requestStream = webRequest.GetRequestStream())
{
WriteToStream(requestStream);
@ -740,15 +735,15 @@ public void Upload(HttpWebRequest webRequest)
/// </summary>
public class BitmapContainer : IBinaryContainer
{
private Bitmap bitmap;
private SurfaceOutputSettings outputSettings;
private string fileName;
private readonly Bitmap _bitmap;
private readonly SurfaceOutputSettings _outputSettings;
private readonly string _fileName;
public BitmapContainer(Bitmap bitmap, SurfaceOutputSettings outputSettings, string filename)
{
this.bitmap = bitmap;
this.outputSettings = outputSettings;
fileName = filename;
_bitmap = bitmap;
_outputSettings = outputSettings;
_fileName = filename;
}
/// <summary>
@ -760,7 +755,7 @@ public string ToBase64String(Base64FormattingOptions formattingOptions)
{
using (MemoryStream stream = new MemoryStream())
{
ImageOutput.SaveToStream(bitmap, null, stream, outputSettings);
ImageOutput.SaveToStream(_bitmap, null, stream, _outputSettings);
return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions);
}
}
@ -774,7 +769,7 @@ public byte[] ToByteArray()
{
using (MemoryStream stream = new MemoryStream())
{
ImageOutput.SaveToStream(bitmap, null, stream, outputSettings);
ImageOutput.SaveToStream(_bitmap, null, stream, _outputSettings);
return stream.ToArray();
}
}
@ -791,11 +786,11 @@ public void WriteFormDataToStream(string boundary, string name, Stream formDataS
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
name,
fileName ?? name,
"image/" + outputSettings.Format.ToString());
_fileName ?? name,
"image/" + _outputSettings.Format);
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
ImageOutput.SaveToStream(bitmap, null, formDataStream, outputSettings);
ImageOutput.SaveToStream(_bitmap, null, formDataStream, _outputSettings);
}
/// <summary>
@ -805,7 +800,7 @@ public void WriteFormDataToStream(string boundary, string name, Stream formDataS
public void WriteToStream(Stream dataStream)
{
// Write the file data directly to the Stream, rather than serializing it to a string.
ImageOutput.SaveToStream(bitmap, null, dataStream, outputSettings);
ImageOutput.SaveToStream(_bitmap, null, dataStream, _outputSettings);
}
/// <summary>
@ -814,7 +809,7 @@ public void WriteToStream(Stream dataStream)
/// <param name="webRequest"></param>
public void Upload(HttpWebRequest webRequest)
{
webRequest.ContentType = "image/" + outputSettings.Format.ToString();
webRequest.ContentType = "image/" + _outputSettings.Format;
using (var requestStream = webRequest.GetRequestStream())
{
WriteToStream(requestStream);
@ -827,15 +822,15 @@ public void Upload(HttpWebRequest webRequest)
/// </summary>
public class SurfaceContainer : IBinaryContainer
{
private ISurface surface;
private SurfaceOutputSettings outputSettings;
private string fileName;
private readonly ISurface _surface;
private readonly SurfaceOutputSettings _outputSettings;
private readonly string _fileName;
public SurfaceContainer(ISurface surface, SurfaceOutputSettings outputSettings, string filename)
{
this.surface = surface;
this.outputSettings = outputSettings;
fileName = filename;
_surface = surface;
_outputSettings = outputSettings;
_fileName = filename;
}
/// <summary>
@ -847,7 +842,7 @@ public string ToBase64String(Base64FormattingOptions formattingOptions)
{
using (MemoryStream stream = new MemoryStream())
{
ImageOutput.SaveToStream(surface, stream, outputSettings);
ImageOutput.SaveToStream(_surface, stream, _outputSettings);
return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions);
}
}
@ -861,7 +856,7 @@ public byte[] ToByteArray()
{
using (MemoryStream stream = new MemoryStream())
{
ImageOutput.SaveToStream(surface, stream, outputSettings);
ImageOutput.SaveToStream(_surface, stream, _outputSettings);
return stream.ToArray();
}
}
@ -878,11 +873,11 @@ public void WriteFormDataToStream(string boundary, string name, Stream formDataS
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
name,
fileName ?? name,
"image/" + outputSettings.Format);
_fileName ?? name,
"image/" + _outputSettings.Format);
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
ImageOutput.SaveToStream(surface, formDataStream, outputSettings);
ImageOutput.SaveToStream(_surface, formDataStream, _outputSettings);
}
/// <summary>
@ -892,7 +887,7 @@ public void WriteFormDataToStream(string boundary, string name, Stream formDataS
public void WriteToStream(Stream dataStream)
{
// Write the file data directly to the Stream, rather than serializing it to a string.
ImageOutput.SaveToStream(surface, dataStream, outputSettings);
ImageOutput.SaveToStream(_surface, dataStream, _outputSettings);
}
/// <summary>
@ -901,7 +896,7 @@ public void WriteToStream(Stream dataStream)
/// <param name="webRequest"></param>
public void Upload(HttpWebRequest webRequest)
{
webRequest.ContentType = "image/" + outputSettings.Format.ToString();
webRequest.ContentType = "image/" + _outputSettings.Format.ToString();
using (var requestStream = webRequest.GetRequestStream())
{
WriteToStream(requestStream);

View file

@ -0,0 +1,92 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub: https://github.com/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace GreenshotPlugin.Core
{
/// <summary>
/// Extensions to help with querying the Operating System
/// </summary>
public static class OperatingSystemExtensions
{
/// <summary>
/// Test if the current OS is Windows 10
/// </summary>
/// <param name="operatingSystem">OperatingSystem from Environment.OSVersion</param>
/// <returns>true if we are running on Windows 10</returns>
public static bool IsWindows10(this OperatingSystem operatingSystem)
{
return operatingSystem.Version.Major == 10;
}
/// <summary>
/// Test if the current OS is Windows 8(.1)
/// </summary>
/// <param name="operatingSystem">OperatingSystem from Environment.OSVersion</param>
/// <returns>true if we are running on Windows 8(.1)</returns>
public static bool IsWindows8(this OperatingSystem operatingSystem)
{
return operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 2;
}
/// <summary>
/// Test if the current OS is Windows 8 or later
/// </summary>
/// <param name="operatingSystem">OperatingSystem from Environment.OSVersion</param>
/// <returns>true if we are running on Windows 8 or later</returns>
public static bool IsWindows8OrLater(this OperatingSystem operatingSystem)
{
return (operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 2) || operatingSystem.Version.Major >= 6;
}
/// <summary>
/// Test if the current OS is Windows 7 or later
/// </summary>
/// <param name="operatingSystem">OperatingSystem from Environment.OSVersion</param>
/// <returns>true if we are running on Windows 7 or later</returns>
public static bool IsWindows7OrLater(this OperatingSystem operatingSystem)
{
return (operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 1) || operatingSystem.Version.Major >= 6;
}
/// <summary>
/// Test if the current OS is Windows Vista or later
/// </summary>
/// <param name="operatingSystem">OperatingSystem from Environment.OSVersion</param>
/// <returns>true if we are running on Windows Vista or later</returns>
public static bool IsWindowsVistaOrLater(this OperatingSystem operatingSystem)
{
return operatingSystem.Version.Major >= 6;
}
/// <summary>
/// Test if the current OS is Windows XP or later
/// </summary>
/// <param name="operatingSystem">OperatingSystem from Environment.OSVersion</param>
/// <returns>true if we are running on Windows XP or later</returns>
public static bool IsWindowsXpOrLater(this OperatingSystem operatingSystem)
{
// Windows 2000 is Major 5 minor 0
return Environment.OSVersion.Version.Major > 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1);
}
}
}

View file

@ -37,9 +37,9 @@ namespace GreenshotPlugin.Core
/// </summary>
public static class PluginUtils
{
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private const string PATH_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
private static IDictionary<string, Image> exeIconCache = new Dictionary<string, Image>();
private static readonly IDictionary<string, Image> exeIconCache = new Dictionary<string, Image>();
static PluginUtils()
{
@ -126,16 +126,19 @@ public static Image GetCachedExeIcon(string path, int index)
{
string cacheKey = string.Format("{0}:{1}", path, index);
Image returnValue;
if (!exeIconCache.TryGetValue(cacheKey, out returnValue))
lock (exeIconCache)
{
lock (exeIconCache)
if (!exeIconCache.TryGetValue(cacheKey, out returnValue))
{
if (!exeIconCache.TryGetValue(cacheKey, out returnValue))
lock (exeIconCache)
{
returnValue = GetExeIcon(path, index);
if (returnValue != null)
if (!exeIconCache.TryGetValue(cacheKey, out returnValue))
{
exeIconCache.Add(cacheKey, returnValue);
returnValue = GetExeIcon(path, index);
if (returnValue != null)
{
exeIconCache.Add(cacheKey, returnValue);
}
}
}
}

View file

@ -83,23 +83,23 @@ public class WuQuantizer : IDisposable
private const Int32 MAXVOLUME = SIDESIZE * SIDESIZE * SIDESIZE;
// To count the colors
private int colorCount = 0;
private readonly int colorCount;
private Int32[] reds;
private Int32[] greens;
private Int32[] blues;
private Int32[] sums;
private Int64[,,] weights;
private Int64[,,] momentsRed;
private Int64[,,] momentsGreen;
private Int64[,,] momentsBlue;
private Single[,,] moments;
private readonly Int64[,,] weights;
private readonly Int64[,,] momentsRed;
private readonly Int64[,,] momentsGreen;
private readonly Int64[,,] momentsBlue;
private readonly Single[,,] moments;
private byte[] tag;
private WuColorCube[] cubes;
private Bitmap sourceBitmap;
private readonly WuColorCube[] cubes;
private readonly Bitmap sourceBitmap;
private Bitmap resultBitmap;
public void Dispose()

View file

@ -68,7 +68,7 @@ public float DpiY
set;
}
private Dictionary<string, string> metaData = new Dictionary<string, string>();
private readonly Dictionary<string, string> metaData = new Dictionary<string, string>();
public Dictionary<string, string> MetaData
{
get { return metaData; }
@ -503,6 +503,7 @@ public class WindowCapture
/// <param name="hObject"></param>
/// <returns></returns>
[DllImport("gdi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteObject(IntPtr hObject);
private WindowCapture()
@ -766,7 +767,7 @@ public static Bitmap CaptureRectangle(Rectangle captureBounds)
// capture.Image = capturedBitmap;
// capture.Location = captureBounds.Location;
using (SafeWindowDCHandle desktopDCHandle = SafeWindowDCHandle.fromDesktop())
using (SafeWindowDCHandle desktopDCHandle = SafeWindowDCHandle.FromDesktop())
{
if (desktopDCHandle.IsInvalid)
{

View file

@ -33,12 +33,6 @@
using System.Text.RegularExpressions;
using System.Windows.Forms;
/// <summary>
/// Code for handling with "windows"
/// Main code is taken from vbAccelerator, location:
/// http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/Enumerating_Windows/article.asp
/// but a LOT of changes/enhancements were made to adapt it for Greenshot.
/// </summary>
namespace GreenshotPlugin.Core
{
#region EnumWindows
@ -50,7 +44,7 @@ public class WindowsEnumerator
{
#region Member Variables
private List<WindowDetails> items = null;
private List<WindowDetails> items;
#endregion Member Variables
@ -101,7 +95,7 @@ public WindowsEnumerator GetWindows(IntPtr hWndParent, string classname)
{
items = new List<WindowDetails>();
List<WindowDetails> windows = new List<WindowDetails>();
User32.EnumChildWindows(hWndParent, new EnumWindowsProc(WindowEnum), IntPtr.Zero);
User32.EnumChildWindows(hWndParent, WindowEnum, IntPtr.Zero);
bool hasParent = !IntPtr.Zero.Equals(hWndParent);
string parentText = null;
@ -162,7 +156,7 @@ private int WindowEnum(IntPtr hWnd, int lParam)
/// <returns>True to continue enumeration, False to stop</returns>
protected virtual bool OnWindowEnum(IntPtr hWnd)
{
if (!WindowDetails.isIgnoreHandle(hWnd))
if (!WindowDetails.IsIgnoreHandle(hWnd))
{
items.Add(new WindowDetails(hWnd));
}
@ -186,19 +180,24 @@ public WindowsEnumerator()
#region WindowDetails
/// <summary>
/// Code for handling with "windows"
/// Main code is taken from vbAccelerator, location:
/// http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/Enumerating_Windows/article.asp
/// but a LOT of changes/enhancements were made to adapt it for Greenshot.
/// <summary>
/// Provides details about a Window returned by the
/// enumeration
/// </summary>
public class WindowDetails : IEquatable<WindowDetails>
{
private const string METRO_WINDOWS_CLASS = "Windows.UI.Core.CoreWindow";
private const string METRO_WINDOWS_CLASS = "Windows.UI.Core.CoreWindow"; // Windows 10 uses ApplicationFrameWindow
private const string METRO_APPLAUNCHER_CLASS = "ImmersiveLauncher";
private const string METRO_GUTTER_CLASS = "ImmersiveGutter";
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static List<IntPtr> ignoreHandles = new List<IntPtr>();
private static List<string> excludeProcessesFromFreeze = new List<string>();
private static IAppVisibility appVisibility = null;
private static readonly CoreConfiguration Conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly List<IntPtr> IgnoreHandles = new List<IntPtr>();
private static readonly List<string> ExcludeProcessesFromFreeze = new List<string>();
private static readonly IAppVisibility appVisibility;
static WindowDetails()
{
@ -215,23 +214,27 @@ static WindowDetails()
public static void AddProcessToExcludeFromFreeze(string processname)
{
if (!excludeProcessesFromFreeze.Contains(processname))
if (!ExcludeProcessesFromFreeze.Contains(processname))
{
excludeProcessesFromFreeze.Add(processname);
ExcludeProcessesFromFreeze.Add(processname);
}
}
internal static bool isIgnoreHandle(IntPtr handle)
internal static bool IsIgnoreHandle(IntPtr handle)
{
return ignoreHandles.Contains(handle);
return IgnoreHandles.Contains(handle);
}
private List<WindowDetails> childWindows = null;
private IntPtr parentHandle = IntPtr.Zero;
private WindowDetails parent = null;
private bool frozen = false;
private List<WindowDetails> _childWindows;
private IntPtr _parentHandle = IntPtr.Zero;
private WindowDetails _parent;
private bool _frozen;
public bool isApp
/// <summary>
/// This checks if the window is a Windows 8 App
/// For Windows 10 most normal code works, as it's hosted inside "ApplicationFrameWindow"
/// </summary>
public bool IsApp
{
get
{
@ -239,7 +242,7 @@ public bool isApp
}
}
public bool isGutter
public bool IsGutter
{
get
{
@ -247,7 +250,7 @@ public bool isGutter
}
}
public bool isAppLauncher
public bool IsAppLauncher
{
get
{
@ -258,18 +261,18 @@ public bool isAppLauncher
/// <summary>
/// Check if this window is the window of a metro app
/// </summary>
public bool isMetroApp
public bool IsMetroApp
{
get
{
return isAppLauncher || isApp;
return IsAppLauncher || IsApp;
}
}
/// <summary>
/// The window handle.
/// </summary>
private IntPtr hWnd = IntPtr.Zero;
private IntPtr _hWnd = IntPtr.Zero;
/// <summary>
/// To allow items to be compared, the hash code
@ -310,18 +313,18 @@ public bool HasChildren
{
get
{
return (childWindows != null) && (childWindows.Count > 0);
return (_childWindows != null) && (_childWindows.Count > 0);
}
}
public void FreezeDetails()
{
frozen = true;
_frozen = true;
}
public void UnfreezeDetails()
{
frozen = false;
_frozen = false;
}
public string ProcessPath
@ -362,7 +365,7 @@ public Image DisplayIcon
LOG.WarnFormat("Couldn't get icon for window {0} due to: {1}", Text, ex.Message);
LOG.Warn(ex);
}
if (isMetroApp)
if (IsMetroApp)
{
// No method yet to get the metro icon
return null;
@ -391,8 +394,8 @@ private static Icon GetAppIcon(IntPtr hwnd)
IntPtr ICON_BIG = new IntPtr(1);
IntPtr ICON_SMALL2 = new IntPtr(2);
IntPtr iconHandle = User32.SendMessage(hwnd, (int)WindowsMessages.WM_GETICON, ICON_BIG, IntPtr.Zero);
if (conf.UseLargeIcons)
IntPtr iconHandle;
if (Conf.UseLargeIcons)
{
iconHandle = User32.SendMessage(hwnd, (int)WindowsMessages.WM_GETICON, ICON_BIG, IntPtr.Zero);
if (iconHandle == IntPtr.Zero)
@ -437,7 +440,7 @@ private static Icon GetAppIcon(IntPtr hwnd)
/// <param name="ignoreHandle"></param>
public static void RegisterIgnoreHandle(IntPtr ignoreHandle)
{
ignoreHandles.Add(ignoreHandle);
IgnoreHandles.Add(ignoreHandle);
}
/// <summary>
@ -446,24 +449,25 @@ public static void RegisterIgnoreHandle(IntPtr ignoreHandle)
/// <param name="ignoreHandle"></param>
public static void UnregisterIgnoreHandle(IntPtr ignoreHandle)
{
ignoreHandles.Remove(ignoreHandle);
IgnoreHandles.Remove(ignoreHandle);
}
public List<WindowDetails> Children
{
get
{
if (childWindows == null)
if (_childWindows == null)
{
GetChildren();
}
return childWindows;
return _childWindows;
}
}
/// <summary>
/// Retrieve all windows with a certain title or classname
/// </summary>
/// <param name="windows"></param>
/// <param name="titlePattern">The regexp to look for in the title</param>
/// <param name="classnamePattern">The regexp to look for in the classname</param>
/// <returns>List<WindowDetails> with all the found windows</returns>
@ -529,19 +533,19 @@ public IntPtr ParentHandle
{
get
{
if (parentHandle == IntPtr.Zero)
if (_parentHandle == IntPtr.Zero)
{
parentHandle = User32.GetParent(Handle);
parent = null;
_parentHandle = User32.GetParent(Handle);
_parent = null;
}
return parentHandle;
return _parentHandle;
}
set
{
if (parentHandle != value)
if (_parentHandle != value)
{
parentHandle = value;
parent = null;
_parentHandle = value;
_parent = null;
}
}
}
@ -552,18 +556,18 @@ public IntPtr ParentHandle
/// <returns>WindowDetails of the parent, or null if none</returns>
public WindowDetails GetParent()
{
if (parent == null)
if (_parent == null)
{
if (parentHandle == IntPtr.Zero)
if (_parentHandle == IntPtr.Zero)
{
parentHandle = User32.GetParent(Handle);
_parentHandle = User32.GetParent(Handle);
}
if (parentHandle != IntPtr.Zero)
if (_parentHandle != IntPtr.Zero)
{
parent = new WindowDetails(parentHandle);
_parent = new WindowDetails(_parentHandle);
}
}
return parent;
return _parent;
}
/// <summary>
@ -572,11 +576,11 @@ public WindowDetails GetParent()
/// </summary>
public List<WindowDetails> GetChildren()
{
if (childWindows == null)
if (_childWindows == null)
{
return GetChildren(0);
}
return childWindows;
return _childWindows;
}
/// <summary>
@ -585,19 +589,19 @@ public List<WindowDetails> GetChildren()
/// <param name="levelsToGo">Specify how many levels we go in</param>
public List<WindowDetails> GetChildren(int levelsToGo)
{
if (childWindows == null)
if (_childWindows == null)
{
childWindows = new List<WindowDetails>();
foreach (WindowDetails childWindow in new WindowsEnumerator().GetWindows(hWnd, null).Items)
_childWindows = new List<WindowDetails>();
foreach (WindowDetails childWindow in new WindowsEnumerator().GetWindows(_hWnd, null).Items)
{
childWindows.Add(childWindow);
_childWindows.Add(childWindow);
if (levelsToGo > 0)
{
childWindow.GetChildren(levelsToGo - 1);
}
}
}
return childWindows;
return _childWindows;
}
/// <summary>
@ -605,7 +609,7 @@ public List<WindowDetails> GetChildren(int levelsToGo)
/// </summary>
/// <param name="titlePattern">The regexp to look for in the title</param>
/// <param name="classnamePattern">The regexp to look for in the classname</param>
/// <returns>List<WindowDetails> with all the found windows, or an empty list</returns>
/// <returns>List WindowDetails with all the found windows, or an empty list</returns>
public List<WindowDetails> FindChildren(string titlePattern, string classnamePattern)
{
return FindWindow(Children, titlePattern, classnamePattern);
@ -646,7 +650,7 @@ private WindowDetails FindPath(List<string> classnames, int index)
/// This method will find the child window according to a path of classnames.
/// Usually used for finding a certain "content" window like for the IE Browser
/// </summary>
/// <param name="classnames">List<string> with classname "path"</param>
/// <param name="classnames">List string with classname "path"</param>
/// <param name="allowSkip">true allows the search to skip a classname of the path</param>
/// <returns>WindowDetails if found</returns>
public WindowDetails FindPath(List<string> classnames, bool allowSkip)
@ -707,7 +711,7 @@ public WindowDetails GetWindow(GetWindowCommand gwCommand)
return null;
}
WindowDetails windowDetails = new WindowDetails(tmphWnd);
windowDetails.parent = this;
windowDetails._parent = this;
return windowDetails;
}
@ -718,11 +722,11 @@ public IntPtr Handle
{
get
{
return hWnd;
return _hWnd;
}
}
private string text = null;
private string _text;
/// <summary>
/// Gets the window's title (caption)
/// </summary>
@ -730,21 +734,21 @@ public string Text
{
set
{
text = value;
_text = value;
}
get
{
if (text == null)
if (_text == null)
{
StringBuilder title = new StringBuilder(260, 260);
User32.GetWindowText(hWnd, title, title.Capacity);
text = title.ToString();
User32.GetWindowText(_hWnd, title, title.Capacity);
_text = title.ToString();
}
return text;
return _text;
}
}
private string className = null;
private string _className;
/// <summary>
/// Gets the window's class name.
/// </summary>
@ -752,11 +756,11 @@ public string ClassName
{
get
{
if (className == null)
if (_className == null)
{
className = GetClassName(hWnd);
_className = GetClassName(_hWnd);
}
return className;
return _className;
}
}
@ -767,21 +771,21 @@ public bool Iconic
{
get
{
if (isMetroApp)
if (IsMetroApp)
{
return !Visible;
}
return User32.IsIconic(hWnd) || Location.X <= -32000;
return User32.IsIconic(_hWnd) || Location.X <= -32000;
}
set
{
if (value)
{
User32.SendMessage(hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero);
User32.SendMessage(_hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero);
}
else
{
User32.SendMessage(hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero);
User32.SendMessage(_hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero);
}
}
}
@ -793,7 +797,7 @@ public bool Maximised
{
get
{
if (isApp)
if (IsApp)
{
if (Visible)
{
@ -811,17 +815,17 @@ public bool Maximised
}
return false;
}
return User32.IsZoomed(hWnd);
return User32.IsZoomed(_hWnd);
}
set
{
if (value)
{
User32.SendMessage(hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MAXIMIZE, IntPtr.Zero);
User32.SendMessage(_hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MAXIMIZE, IntPtr.Zero);
}
else
{
User32.SendMessage(hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero);
User32.SendMessage(_hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero);
}
}
}
@ -841,7 +845,7 @@ public bool Visible
{
get
{
if (isApp)
if (IsApp)
{
Rectangle windowRectangle = WindowRectangle;
foreach (Screen screen in Screen.AllScreens)
@ -876,16 +880,16 @@ public bool Visible
}
return false;
}
if (isGutter)
if (IsGutter)
{
// gutter is only made available when it's visible
return true;
}
if (isAppLauncher)
if (IsAppLauncher)
{
return IsAppLauncherVisible;
}
return User32.IsWindowVisible(hWnd);
return User32.IsWindowVisible(_hWnd);
}
}
@ -894,7 +898,7 @@ public bool HasParent
get
{
GetParent();
return parentHandle != IntPtr.Zero;
return _parentHandle != IntPtr.Zero;
}
}
@ -935,12 +939,12 @@ public Process Process
/// </summary>
public void Reset()
{
previousWindowRectangle = Rectangle.Empty;
_previousWindowRectangle = Rectangle.Empty;
}
private Rectangle previousWindowRectangle = Rectangle.Empty;
private long lastWindowRectangleRetrieveTime = 0;
private const long CACHE_TIME = TimeSpan.TicksPerSecond * 2;
private Rectangle _previousWindowRectangle = Rectangle.Empty;
private long _lastWindowRectangleRetrieveTime;
private const long CacheTime = TimeSpan.TicksPerSecond * 2;
/// <summary>
/// Gets the bounding rectangle of the window
/// </summary>
@ -950,39 +954,64 @@ public Rectangle WindowRectangle
{
// Try to return a cached value
long now = DateTime.Now.Ticks;
if (previousWindowRectangle.IsEmpty || !frozen)
if (_previousWindowRectangle.IsEmpty || !_frozen)
{
if (previousWindowRectangle.IsEmpty || now - lastWindowRectangleRetrieveTime > CACHE_TIME)
if (_previousWindowRectangle.IsEmpty || now - _lastWindowRectangleRetrieveTime > CacheTime)
{
Rectangle windowRect = Rectangle.Empty;
if (DWM.isDWMEnabled())
if (DWM.IsDwmEnabled())
{
GetExtendedFrameBounds(out windowRect);
bool gotFrameBounds = GetExtendedFrameBounds(out windowRect);
if (IsApp)
{
// Pre-Cache for Maximised call, this is only on Windows 8 apps (full screen)
if (gotFrameBounds)
{
_previousWindowRectangle = windowRect;
_lastWindowRectangleRetrieveTime = now;
}
}
if (gotFrameBounds && Environment.OSVersion.IsWindows10() && !Maximised)
{
// Somehow DWM doesn't calculate it corectly, there is a 1 pixel border around the capture
// Remove this border, currently it's fixed but TODO: Make it depend on the OS?
windowRect.Inflate(-1, -1);
_previousWindowRectangle = windowRect;
_lastWindowRectangleRetrieveTime = now;
return windowRect;
}
}
if (windowRect.IsEmpty)
{
GetWindowRect(out windowRect);
if (GetWindowRect(out windowRect))
{
Win32Error error = Win32.GetLastErrorCode();
LOG.WarnFormat("Couldn't retrieve the windows rectangle: {0}", Win32.GetMessage(error));
}
}
// Correction for maximized windows, only if it's not an app
if (!HasParent && !isApp && Maximised)
if (!HasParent && !IsApp && Maximised)
{
Size size = Size.Empty;
GetBorderSize(out size);
windowRect = new Rectangle(windowRect.X + size.Width, windowRect.Y + size.Height, windowRect.Width - (2 * size.Width), windowRect.Height - (2 * size.Height));
Size size;
// Only if the border size can be retrieved
if (GetBorderSize(out size))
{
windowRect = new Rectangle(windowRect.X + size.Width, windowRect.Y + size.Height, windowRect.Width - (2 * size.Width), windowRect.Height - (2 * size.Height));
}
}
lastWindowRectangleRetrieveTime = now;
_lastWindowRectangleRetrieveTime = now;
// Try to return something valid, by getting returning the previous size if the window doesn't have a Rectangle anymore
if (windowRect.IsEmpty)
{
return previousWindowRectangle;
return _previousWindowRectangle;
}
previousWindowRectangle = windowRect;
_previousWindowRectangle = windowRect;
return windowRect;
}
}
return previousWindowRectangle;
return _previousWindowRectangle;
}
}
@ -1017,8 +1046,12 @@ public Rectangle ClientRectangle
{
get
{
Rectangle clientRect = Rectangle.Empty;
GetClientRect(out clientRect);
Rectangle clientRect;
if (GetClientRect(out clientRect))
{
Win32Error error = Win32.GetLastErrorCode();
LOG.WarnFormat("Couldn't retrieve the client rectangle: {0}", Win32.GetMessage(error));
}
return clientRect;
}
}
@ -1041,10 +1074,10 @@ public void Restore()
{
if (Iconic)
{
User32.SendMessage(hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero);
User32.SendMessage(_hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero);
}
User32.BringWindowToTop(hWnd);
User32.SetForegroundWindow(hWnd);
User32.BringWindowToTop(_hWnd);
User32.SetForegroundWindow(_hWnd);
// Make sure windows has time to perform the action
while (Iconic)
{
@ -1059,11 +1092,11 @@ public WindowStyleFlags WindowStyle
{
get
{
return (WindowStyleFlags)User32.GetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_STYLE);
return (WindowStyleFlags)User32.GetWindowLongWrapper(_hWnd, (int)WindowLongIndex.GWL_STYLE);
}
set
{
User32.SetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_STYLE, new IntPtr((long)value));
User32.SetWindowLongWrapper(_hWnd, (int)WindowLongIndex.GWL_STYLE, new IntPtr((long)value));
}
}
@ -1091,11 +1124,11 @@ public ExtendedWindowStyleFlags ExtendedWindowStyle
{
get
{
return (ExtendedWindowStyleFlags)User32.GetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_EXSTYLE);
return (ExtendedWindowStyleFlags)User32.GetWindowLongWrapper(_hWnd, (int)WindowLongIndex.GWL_EXSTYLE);
}
set
{
User32.SetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_EXSTYLE, new IntPtr((uint)value));
User32.SetWindowLongWrapper(_hWnd, (int)WindowLongIndex.GWL_EXSTYLE, new IntPtr((uint)value));
}
}
@ -1104,7 +1137,7 @@ public ExtendedWindowStyleFlags ExtendedWindowStyle
/// </summary>
/// <param name="capture">The capture to fill</param>
/// <returns>ICapture</returns>
public ICapture CaptureGDIWindow(ICapture capture)
public ICapture CaptureGdiWindow(ICapture capture)
{
Image capturedImage = PrintWindow();
if (capturedImage != null)
@ -1123,17 +1156,19 @@ public ICapture CaptureGDIWindow(ICapture capture)
/// <param name="windowCaptureMode">Wanted WindowCaptureMode</param>
/// <param name="autoMode">True if auto modus is used</param>
/// <returns>ICapture with the capture</returns>
public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptureMode, bool autoMode)
public ICapture CaptureDwmWindow(ICapture capture, WindowCaptureMode windowCaptureMode, bool autoMode)
{
IntPtr thumbnailHandle = IntPtr.Zero;
Form tempForm = null;
bool tempFormShown = false;
try
{
tempForm = new Form();
tempForm.ShowInTaskbar = false;
tempForm.FormBorderStyle = FormBorderStyle.None;
tempForm.TopMost = true;
tempForm = new Form
{
ShowInTaskbar = false,
FormBorderStyle = FormBorderStyle.None,
TopMost = true
};
// Register the Thumbnail
DWM.DwmRegisterThumbnail(tempForm.Handle, Handle, out thumbnailHandle);
@ -1142,14 +1177,14 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
SIZE sourceSize;
DWM.DwmQueryThumbnailSourceSize(thumbnailHandle, out sourceSize);
if (sourceSize.width <= 0 || sourceSize.height <= 0)
if (sourceSize.Width <= 0 || sourceSize.Height <= 0)
{
return null;
}
// Calculate the location of the temp form
Point formLocation;
Rectangle windowRectangle = WindowRectangle;
Point formLocation = windowRectangle.Location;
Size borderSize = new Size();
bool doesCaptureFit = false;
if (!Maximised)
@ -1161,7 +1196,7 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
// Find the screen where the window is and check if it fits
foreach (Screen screen in Screen.AllScreens)
{
if (screen != Screen.PrimaryScreen)
if (!Equals(screen, Screen.PrimaryScreen))
{
workingArea.Union(screen.Bounds);
}
@ -1188,7 +1223,7 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
}
}
}
else
else if (!Environment.OSVersion.IsWindows8OrLater())
{
//GetClientRect(out windowRectangle);
GetBorderSize(out borderSize);
@ -1199,38 +1234,42 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
tempForm.Size = sourceSize.ToSize();
// Prepare rectangle to capture from the screen.
Rectangle captureRectangle = new Rectangle(formLocation.X, formLocation.Y, sourceSize.width, sourceSize.height);
Rectangle captureRectangle = new Rectangle(formLocation.X, formLocation.Y, sourceSize.Width, sourceSize.Height);
if (Maximised)
{
// Correct capture size for maximized window by offsetting the X,Y with the border size
captureRectangle.X += borderSize.Width;
captureRectangle.Y += borderSize.Height;
// and subtracting the border from the size (2 times, as we move right/down for the capture without resizing)
captureRectangle.Width -= 2 * borderSize.Width;
captureRectangle.Height -= 2 * borderSize.Height;
captureRectangle.Inflate(borderSize.Width, borderSize.Height);
}
else if (autoMode)
else
{
// check if the capture fits
if (!doesCaptureFit)
captureRectangle.Inflate(-1, -1);
if (autoMode)
{
// if GDI is allowed.. (a screenshot won't be better than we comes if we continue)
using (Process thisWindowProcess = Process)
// check if the capture fits
if (!doesCaptureFit)
{
if (!isMetroApp && WindowCapture.IsGdiAllowed(thisWindowProcess))
// if GDI is allowed.. (a screenshot won't be better than we comes if we continue)
using (Process thisWindowProcess = Process)
{
// we return null which causes the capturing code to try another method.
return null;
if (!IsMetroApp && WindowCapture.IsGdiAllowed(thisWindowProcess))
{
// we return null which causes the capturing code to try another method.
return null;
}
}
}
}
}
// Prepare the displaying of the Thumbnail
DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES();
props.Opacity = (byte)255;
props.Visible = true;
props.Destination = new RECT(0, 0, sourceSize.width, sourceSize.height);
DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES
{
Opacity = 255,
Visible = true,
Destination = new RECT(0, 0, sourceSize.Width, sourceSize.Height)
};
DWM.DwmUpdateThumbnailProperties(thumbnailHandle, ref props);
tempForm.Show();
tempFormShown = true;
@ -1261,7 +1300,7 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
tempForm.BackColor = Color.Black;
// Make sure everything is visible
tempForm.Refresh();
if (!isMetroApp)
if (!IsMetroApp)
{
// Make sure the application window is active, so the colors & buttons are right
ToForeground();
@ -1291,7 +1330,7 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
// Remove transparency, this will break the capturing
if (!autoMode)
{
tempForm.BackColor = Color.FromArgb(255, conf.DWMBackgroundColor.R, conf.DWMBackgroundColor.G, conf.DWMBackgroundColor.B);
tempForm.BackColor = Color.FromArgb(255, Conf.DWMBackgroundColor.R, Conf.DWMBackgroundColor.G, Conf.DWMBackgroundColor.B);
}
else
{
@ -1302,7 +1341,7 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
}
// Make sure everything is visible
tempForm.Refresh();
if (!isMetroApp)
if (!IsMetroApp)
{
// Make sure the application window is active, so the colors & buttons are right
ToForeground();
@ -1315,10 +1354,10 @@ public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptu
if (capturedBitmap != null)
{
// Not needed for Windows 8
if (!(Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 2))
if (!Environment.OSVersion.IsWindows8OrLater())
{
// Only if the Inivalue is set, not maximized and it's not a tool window.
if (conf.WindowCaptureRemoveCorners && !Maximised && (ExtendedWindowStyle & ExtendedWindowStyleFlags.WS_EX_TOOLWINDOW) == 0)
if (Conf.WindowCaptureRemoveCorners && !Maximised && (ExtendedWindowStyle & ExtendedWindowStyleFlags.WS_EX_TOOLWINDOW) == 0)
{
// Remove corners
if (!Image.IsAlphaPixelFormat(capturedBitmap.PixelFormat))
@ -1375,9 +1414,9 @@ private void RemoveCorners(Bitmap image)
{
using (IFastBitmap fastBitmap = FastBitmap.Create(image))
{
for (int y = 0; y < conf.WindowCornerCutShape.Count; y++)
for (int y = 0; y < Conf.WindowCornerCutShape.Count; y++)
{
for (int x = 0; x < conf.WindowCornerCutShape[y]; x++)
for (int x = 0; x < Conf.WindowCornerCutShape[y]; x++)
{
fastBitmap.SetColorAt(x, y, Color.Transparent);
fastBitmap.SetColorAt(image.Width - 1 - x, y, Color.Transparent);
@ -1506,7 +1545,7 @@ private bool GetWindowRect(out Rectangle rectangle)
/// <summary>
/// Helper method to get the Border size for GDI Windows
/// </summary>
/// <param name="rectangle">out Rectangle</param>
/// <param name="size">out Size</param>
/// <returns>bool true if it worked</returns>
private bool GetBorderSize(out Size size)
{
@ -1570,7 +1609,7 @@ private bool CanFreezeOrUnfreeze(string titleOrProcessname)
return false;
}
foreach (string excludeProcess in excludeProcessesFromFreeze)
foreach (string excludeProcess in ExcludeProcessesFromFreeze)
{
if (titleOrProcessname.ToLower().Contains(excludeProcess))
{
@ -1722,7 +1761,7 @@ public Image PrintWindow()
/// <param name="hWnd">The Window Handle</param>
public WindowDetails(IntPtr hWnd)
{
this.hWnd = hWnd;
this._hWnd = hWnd;
}
/// <summary>
@ -1734,7 +1773,7 @@ public static WindowDetails GetActiveWindow()
IntPtr hWnd = User32.GetForegroundWindow();
if (hWnd != null && hWnd != IntPtr.Zero)
{
if (ignoreHandles.Contains(hWnd))
if (IgnoreHandles.Contains(hWnd))
{
return GetDesktopWindow();
}
@ -1759,7 +1798,7 @@ public bool IsGreenshot
{
try
{
if (!isMetroApp)
if (!IsMetroApp)
{
using (Process thisWindowProcess = Process)
{
@ -1796,7 +1835,7 @@ public static List<WindowDetails> GetAllWindows()
/// <summary>
/// Get all the top level windows, with matching classname
/// </summary>
/// <returns>List<WindowDetails> with all the top level windows</returns>
/// <returns>List WindowDetails with all the top level windows</returns>
public static List<WindowDetails> GetAllWindows(string classname)
{
return new WindowsEnumerator().GetWindows(IntPtr.Zero, classname).Items;
@ -1805,7 +1844,6 @@ public static List<WindowDetails> GetAllWindows(string classname)
/// <summary>
/// Recursive "find children which"
/// </summary>
/// <param name="window">Window to look into</param>
/// <param name="point">point to check for</param>
/// <returns></returns>
public WindowDetails FindChildUnderPoint(Point point)
@ -1839,7 +1877,7 @@ public static string GetClassName(IntPtr hWnd)
/// <summary>
/// Get all the visible top level windows
/// </summary>
/// <returns>List<WindowDetails> with all the visible top level windows</returns>
/// <returns>List WindowDetails with all the visible top level windows</returns>
public static List<WindowDetails> GetVisibleWindows()
{
List<WindowDetails> windows = new List<WindowDetails>();
@ -1859,7 +1897,7 @@ public static List<WindowDetails> GetVisibleWindows()
continue;
}
// Ignore some classes
List<string> ignoreClasses = new List<string>(new string[] { "Progman", "XLMAIN", "Button", "Dwm" }); //"MS-SDIa"
List<string> ignoreClasses = new List<string>(new[] { "Progman", "XLMAIN", "Button", "Dwm" }); //"MS-SDIa"
if (ignoreClasses.Contains(window.ClassName))
{
continue;
@ -1880,7 +1918,7 @@ public static List<WindowDetails> GetVisibleWindows()
/// Get the WindowDetails for all Metro Apps
/// These are all Windows with Classname "Windows.UI.Core.CoreWindow"
/// </summary>
/// <returns>List<WindowDetails> with visible metro apps</returns>
/// <returns>List WindowDetails with visible metro apps</returns>
public static List<WindowDetails> GetMetroApps()
{
List<WindowDetails> metroApps = new List<WindowDetails>();
@ -1939,7 +1977,7 @@ public static List<WindowDetails> GetTopLevelWindows()
continue;
}
// Ignore some classes
List<string> ignoreClasses = new List<string>(new string[] { "Progman", "XLMAIN", "Button", "Dwm" }); //"MS-SDIa"
List<string> ignoreClasses = new List<string>(new[] { "Progman", "XLMAIN", "Button", "Dwm" }); //"MS-SDIa"
if (ignoreClasses.Contains(window.ClassName))
{
continue;
@ -2020,7 +2058,7 @@ public static WindowDetails GetLinkedWindow(WindowDetails windowToLinkTo)
/// Helper method to "active" all windows that are not in the supplied list.
/// One should preferably call "GetVisibleWindows" for the oldWindows.
/// </summary>
/// <param name="oldWindows">List<WindowDetails> with old windows</param>
/// <param name="oldWindows">List WindowDetails with old windows</param>
public static void ActiveNewerWindows(List<WindowDetails> oldWindows)
{
List<WindowDetails> windowsAfter = GetVisibleWindows();

View file

@ -89,7 +89,7 @@ public override void Draw(Graphics graphics, RenderMode rm)
Top + currentStep + Height);
currentStep++;
alpha = alpha - (basealpha / steps);
alpha = alpha - basealpha / steps;
}
}
}

View file

@ -50,10 +50,8 @@ public abstract class DrawableContainer : AbstractFieldHolderWithChildren, IDraw
protected static readonly Color DefaultLineColor = Color.FromArgb(0, 150, 255);
private bool isMadeUndoable;
private bool _isMadeUndoable;
private const int M11 = 0;
private const int M12 = 1;
private const int M21 = 2;
private const int M22 = 3;
protected EditStatus _defaultEditMode = EditStatus.DRAWING;
@ -383,7 +381,7 @@ public void AlignToParent(HorizontalAlignment horizontalAlignment, VerticalAlign
}
if (horizontalAlignment == HorizontalAlignment.Center)
{
Left = (_parent.Width / 2) - (Width / 2) - lineThickness / 2;
Left = _parent.Width / 2 - Width / 2 - lineThickness / 2;
}
if (verticalAlignment == VerticalAlignment.TOP)
@ -396,7 +394,7 @@ public void AlignToParent(HorizontalAlignment horizontalAlignment, VerticalAlign
}
if (verticalAlignment == VerticalAlignment.CENTER)
{
Top = (_parent.Height / 2) - (Height / 2) - lineThickness / 2;
Top = _parent.Height / 2 - Height / 2 - lineThickness / 2;
}
}
@ -569,7 +567,7 @@ private void GripperMouseDown(object sender, MouseEventArgs e)
{
Status = EditStatus.MOVING;
}
isMadeUndoable = false;
_isMadeUndoable = false;
}
private void GripperMouseUp(object sender, MouseEventArgs e)
@ -579,7 +577,7 @@ private void GripperMouseUp(object sender, MouseEventArgs e)
{
_boundsBeforeResize = Rectangle.Empty;
_boundsAfterResize = RectangleF.Empty;
isMadeUndoable = false;
_isMadeUndoable = false;
}
Status = EditStatus.IDLE;
Invalidate();
@ -598,10 +596,10 @@ private void GripperMouseMove(object sender, MouseEventArgs e)
else if (Status.Equals(EditStatus.RESIZING))
{
// check if we already made this undoable
if (!isMadeUndoable)
if (!_isMadeUndoable)
{
// don't allow another undo until we are finished with this move
isMadeUndoable = true;
_isMadeUndoable = true;
// Make undo-able
MakeBoundsChangeUndoable(false);
}
@ -871,6 +869,8 @@ public override bool Equals(object obj)
public override int GetHashCode()
{
// TODO: This actually doesn't make sense...
// Place the container in a list, and you can't find it :)
return left.GetHashCode() ^ top.GetHashCode() ^ width.GetHashCode() ^ height.GetHashCode() ^ GetFields().GetHashCode();
}

View file

@ -383,7 +383,7 @@ public void PullElementsUp(DrawableContainerList elements)
{
continue;
}
if (Count > (i + 1) && !elements.Contains(this[i + 1]))
if (Count > i + 1 && !elements.Contains(this[i + 1]))
{
SwapElements(i, i + 1);
}
@ -554,7 +554,7 @@ public virtual void AddContextMenuItems(ContextMenuStrip menu, Surface surface)
// Copy
item = new ToolStripMenuItem("Copy");
item.Image = ((Image)(editorFormResources.GetObject("copyToolStripMenuItem.Image")));
item.Image = (Image)editorFormResources.GetObject("copyToolStripMenuItem.Image");
item.Click += delegate
{
ClipboardHelper.SetClipboardData(typeof(DrawableContainerList), this);
@ -563,7 +563,7 @@ public virtual void AddContextMenuItems(ContextMenuStrip menu, Surface surface)
// Cut
item = new ToolStripMenuItem("Cut");
item.Image = ((Image)(editorFormResources.GetObject("btnCut.Image")));
item.Image = (Image)editorFormResources.GetObject("btnCut.Image");
item.Click += delegate
{
ClipboardHelper.SetClipboardData(typeof(DrawableContainerList), this);
@ -582,7 +582,7 @@ public virtual void AddContextMenuItems(ContextMenuStrip menu, Surface surface)
// Delete
item = new ToolStripMenuItem("Delete");
item.Image = ((Image)(editorFormResources.GetObject("removeObjectToolStripMenuItem.Image")));
item.Image = (Image)editorFormResources.GetObject("removeObjectToolStripMenuItem.Image");
item.Click += delegate
{
List<DrawableContainer> containersToDelete = new List<DrawableContainer>();

View file

@ -65,11 +65,16 @@ public override void Draw(Graphics graphics, RenderMode renderMode)
/// This allows another container to draw an ellipse
/// </summary>
/// <param name="caller"></param>
/// <param name="rect"></param>
/// <param name="graphics"></param>
/// <param name="renderMode"></param>
/// <param name="lineThickness"></param>
/// <param name="lineColor"></param>
/// <param name="fillColor"></param>
/// <param name="shadow"></param>
public static void DrawEllipse(Rectangle rect, Graphics graphics, RenderMode renderMode, int lineThickness, Color lineColor, Color fillColor, bool shadow)
{
bool lineVisible = (lineThickness > 0 && Colors.IsVisible(lineColor));
bool lineVisible = lineThickness > 0 && Colors.IsVisible(lineColor);
// draw shadow before anything else
if (shadow && (lineVisible || Colors.IsVisible(fillColor)))
{

View file

@ -35,7 +35,7 @@ namespace Greenshot.Drawing.Fields
[Serializable]
public abstract class AbstractFieldHolder : IFieldHolder
{
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
/// <summary>
/// called when a field's value has changed
@ -52,11 +52,7 @@ public abstract class AbstractFieldHolder : IFieldHolder
// this allows us to use default serialization
[NonSerialized]
private Dictionary<FieldType, Field> fieldsByType = new Dictionary<FieldType, Field>();
private List<Field> fields = new List<Field>();
public AbstractFieldHolder()
{
}
private readonly List<Field> fields = new List<Field>();
[OnDeserialized]
private void OnDeserialized(StreamingContext context)

View file

@ -33,7 +33,7 @@ namespace Greenshot.Drawing.Fields
[Serializable()]
public abstract class AbstractFieldHolderWithChildren : AbstractFieldHolder
{
private FieldChangedEventHandler fieldChangedEventHandler;
private readonly FieldChangedEventHandler fieldChangedEventHandler;
[NonSerialized]
private EventHandler childrenChanged;

View file

@ -28,10 +28,6 @@ namespace Greenshot.Drawing.Fields.Binding
/// </summary>
public abstract class AbstractBindingConverter<T1, T2> : IBindingConverter
{
public AbstractBindingConverter()
{
}
public object convert(object o)
{
if (o == null)

View file

@ -32,14 +32,14 @@ namespace Greenshot.Drawing.Fields.Binding
/// </summary>
public class BidirectionalBinding
{
private INotifyPropertyChanged controlObject;
private INotifyPropertyChanged fieldObject;
private string controlPropertyName;
private string fieldPropertyName;
private bool updatingControl = false;
private bool updatingField = false;
private IBindingConverter converter;
private IBindingValidator validator;
private readonly INotifyPropertyChanged _controlObject;
private readonly INotifyPropertyChanged _fieldObject;
private readonly string _controlPropertyName;
private readonly string _fieldPropertyName;
private bool _updatingControl = false;
private bool _updatingField = false;
private IBindingConverter _converter;
private readonly IBindingValidator _validator;
/// <summary>
/// Whether or not null values are passed on to the other object.
@ -49,19 +49,19 @@ public class BidirectionalBinding
/// <summary>
/// Bind properties of two objects bidirectionally
/// </summary>
/// <param name="object1">Object containing 1st property to bind</param>
/// <param name="property1">Property of 1st object to bind</param>
/// <param name="object2">Object containing 2nd property to bind</param>
/// <param name="property2">Property of 2nd object to bind</param>
/// <param name="controlObject">Object containing 1st property to bind</param>
/// <param name="controlPropertyName">Property of 1st object to bind</param>
/// <param name="fieldObject">Object containing 2nd property to bind</param>
/// <param name="fieldPropertyName">Property of 2nd object to bind</param>
public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName)
{
this.controlObject = controlObject;
this.fieldObject = fieldObject;
this.controlPropertyName = controlPropertyName;
this.fieldPropertyName = fieldPropertyName;
_controlObject = controlObject;
_fieldObject = fieldObject;
_controlPropertyName = controlPropertyName;
_fieldPropertyName = fieldPropertyName;
this.controlObject.PropertyChanged += ControlPropertyChanged;
this.fieldObject.PropertyChanged += FieldPropertyChanged;
_controlObject.PropertyChanged += ControlPropertyChanged;
_fieldObject.PropertyChanged += FieldPropertyChanged;
}
/// <summary>
@ -74,7 +74,7 @@ public BidirectionalBinding(INotifyPropertyChanged controlObject, string control
/// <param name="converter">taking care of converting the synchronized value to the correct target format and back</param>
public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingConverter converter) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName)
{
this.converter = converter;
_converter = converter;
}
/// <summary>
@ -88,7 +88,7 @@ public BidirectionalBinding(INotifyPropertyChanged controlObject, string control
/// <param name="validator">validator to intercept synchronization if the value does not match certain criteria</param>
public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingValidator validator) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName)
{
this.validator = validator;
_validator = validator;
}
/// <summary>
@ -103,56 +103,56 @@ public BidirectionalBinding(INotifyPropertyChanged controlObject, string control
/// <param name="validator">validator to intercept synchronization if the value does not match certain criteria</param>
public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingConverter converter, IBindingValidator validator) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName, converter)
{
this.validator = validator;
_validator = validator;
}
public void ControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!updatingControl && e.PropertyName.Equals(controlPropertyName))
if (!_updatingControl && e.PropertyName.Equals(_controlPropertyName))
{
updatingField = true;
synchronize(controlObject, controlPropertyName, fieldObject, fieldPropertyName);
updatingField = false;
_updatingField = true;
Synchronize(_controlObject, _controlPropertyName, _fieldObject, _fieldPropertyName);
_updatingField = false;
}
}
public void FieldPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!updatingField && e.PropertyName.Equals(fieldPropertyName))
if (!_updatingField && e.PropertyName.Equals(_fieldPropertyName))
{
updatingControl = true;
synchronize(fieldObject, fieldPropertyName, controlObject, controlPropertyName);
updatingControl = false;
_updatingControl = true;
Synchronize(_fieldObject, _fieldPropertyName, _controlObject, _controlPropertyName);
_updatingControl = false;
}
}
private void synchronize(INotifyPropertyChanged sourceObject, string sourceProperty, INotifyPropertyChanged targetObject, string targetProperty)
private void Synchronize(INotifyPropertyChanged sourceObject, string sourceProperty, INotifyPropertyChanged targetObject, string targetProperty)
{
PropertyInfo targetPropertyInfo = resolvePropertyInfo(targetObject, targetProperty);
PropertyInfo sourcePropertyInfo = resolvePropertyInfo(sourceObject, sourceProperty);
PropertyInfo targetPropertyInfo = ResolvePropertyInfo(targetObject, targetProperty);
PropertyInfo sourcePropertyInfo = ResolvePropertyInfo(sourceObject, sourceProperty);
if (sourcePropertyInfo != null && targetPropertyInfo != null && targetPropertyInfo.CanWrite)
{
object bValue = sourcePropertyInfo.GetValue(sourceObject, null);
if (converter != null && bValue != null)
if (_converter != null && bValue != null)
{
bValue = converter.convert(bValue);
bValue = _converter.convert(bValue);
}
try
{
if (validator == null || validator.validate(bValue))
if (_validator == null || _validator.validate(bValue))
{
targetPropertyInfo.SetValue(targetObject, bValue, null);
}
}
catch (Exception e)
{
throw new MemberAccessException("Could not set property '" + targetProperty + "' to '" + bValue + "' [" + ((bValue != null) ? bValue.GetType().Name : "") + "] on " + targetObject + ". Probably other type than expected, IBindingCoverter to the rescue.", e);
throw new MemberAccessException("Could not set property '" + targetProperty + "' to '" + bValue + "' [" + (bValue != null ? bValue.GetType().Name : "") + "] on " + targetObject + ". Probably other type than expected, IBindingCoverter to the rescue.", e);
}
}
}
private PropertyInfo resolvePropertyInfo(object obj, string property)
private static PropertyInfo ResolvePropertyInfo(object obj, string property)
{
PropertyInfo ret = null;
string[] properties = property.Split(".".ToCharArray());
@ -170,8 +170,8 @@ private PropertyInfo resolvePropertyInfo(object obj, string property)
public IBindingConverter Converter
{
get { return converter; }
set { converter = value; }
get { return _converter; }
set { _converter = value; }
}
}
}

View file

@ -40,13 +40,10 @@ namespace Greenshot.Drawing.Fields
/// </summary>
public class FieldAggregator : AbstractFieldHolder
{
private List<IDrawableContainer> boundContainers;
private bool internalUpdateRunning = false;
private readonly List<IDrawableContainer> boundContainers;
private bool internalUpdateRunning;
private enum Status
{ IDLE, BINDING, UPDATING };
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
public FieldAggregator()
{

View file

@ -58,7 +58,7 @@ public override void Draw(Graphics graphics, RenderMode rm)
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR);
bool shadow = GetFieldValueAsBool(FieldType.SHADOW);
bool lineVisible = (lineThickness > 0 && Colors.IsVisible(lineColor));
bool lineVisible = lineThickness > 0 && Colors.IsVisible(lineColor);
if (lineVisible)
{
graphics.SmoothingMode = SmoothingMode.HighSpeed;
@ -79,7 +79,7 @@ public override void Draw(Graphics graphics, RenderMode rm)
Rectangle shadowRect = GuiRectangle.GetGuiRectangle(Left + currentStep, Top + currentStep, Width, Height);
graphics.DrawRectangle(shadowPen, shadowRect);
currentStep++;
alpha = alpha - (basealpha / steps);
alpha = alpha - basealpha / steps;
}
}
}

View file

@ -43,7 +43,7 @@ public abstract class AbstractFilter : AbstractFieldHolder, IFilter
remove { propertyChanged -= value; }
}
private bool invert = false;
private bool invert;
public bool Invert
{
get

View file

@ -45,7 +45,7 @@ public BlurFilter(DrawableContainer parent) : base(parent)
AddField(GetType(), FieldType.PREVIEW_QUALITY, 1.0d);
}
public unsafe override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode)
public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode)
{
int blurRadius = GetFieldValueAsInt(FieldType.BLUR_RADIUS);
Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert);
@ -59,7 +59,7 @@ public unsafe override void Apply(Graphics graphics, Bitmap applyBitmap, Rectang
graphics.SetClip(applyRect);
graphics.ExcludeClip(rect);
}
if (GDIplus.isBlurPossible(blurRadius))
if (GDIplus.IsBlurPossible(blurRadius))
{
GDIplus.DrawWithBlur(graphics, applyBitmap, applyRect, null, null, blurRadius, false);
}
@ -72,7 +72,6 @@ public unsafe override void Apply(Graphics graphics, Bitmap applyBitmap, Rectang
}
}
graphics.Restore(state);
return;
}
}
}

View file

@ -60,7 +60,7 @@ public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect
int halfHeight = rect.Height / 2;
int newWidth = rect.Width / magnificationFactor;
int newHeight = rect.Height / magnificationFactor;
Rectangle source = new Rectangle(rect.X + halfWidth - (newWidth / 2), rect.Y + halfHeight - (newHeight / 2), newWidth, newHeight);
Rectangle source = new Rectangle(rect.X + halfWidth - newWidth / 2, rect.Y + halfHeight - newHeight / 2, newWidth, newHeight);
graphics.DrawImage(applyBitmap, rect, source, GraphicsUnit.Pixel);
graphics.Restore(state);
}

View file

@ -40,7 +40,7 @@ public PixelizationFilter(DrawableContainer parent) : base(parent)
public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode)
{
int pixelSize = GetFieldValueAsInt(FieldType.PIXEL_SIZE);
Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert);
ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert);
if (pixelSize <= 1 || rect.Width == 0 || rect.Height == 0)
{
// Nothing to do

View file

@ -42,8 +42,8 @@ public class FreehandContainer : DrawableContainer
private GraphicsPath freehandPath = new GraphicsPath();
private Rectangle myBounds = Rectangle.Empty;
private Point lastMouse = Point.Empty;
private List<Point> capturePoints = new List<Point>();
private bool isRecalculated = false;
private readonly List<Point> capturePoints = new List<Point>();
private bool isRecalculated;
/// <summary>
/// Constructor
@ -130,7 +130,7 @@ public override bool HandleMouseMove(int mouseX, int mouseY)
{
Point previousPoint = capturePoints[capturePoints.Count - 1];
if (GeometryHelper.Distance2D(previousPoint.X, previousPoint.Y, mouseX, mouseY) >= (2 * EditorConfig.FreehandSensitivity))
if (GeometryHelper.Distance2D(previousPoint.X, previousPoint.Y, mouseX, mouseY) >= 2 * EditorConfig.FreehandSensitivity)
{
capturePoints.Add(new Point(mouseX, mouseY));
}
@ -263,7 +263,7 @@ public override Rectangle DrawingBounds
{
int lineThickness = Math.Max(10, GetFieldValueAsInt(FieldType.LINE_THICKNESS));
int safetymargin = 10;
return new Rectangle((myBounds.Left + Left) - (safetymargin + lineThickness), (myBounds.Top + Top) - (safetymargin + lineThickness), myBounds.Width + (2 * (lineThickness + safetymargin)), myBounds.Height + (2 * (lineThickness + safetymargin)));
return new Rectangle(myBounds.Left + Left - (safetymargin + lineThickness), myBounds.Top + Top - (safetymargin + lineThickness), myBounds.Width + 2 * (lineThickness + safetymargin), myBounds.Height + 2 * (lineThickness + safetymargin));
}
return new Rectangle(0, 0, _parent.Width, _parent.Height);
}

View file

@ -99,7 +99,7 @@ public override void Draw(Graphics graphics, RenderMode rm)
Top + currentStep + Height);
currentStep++;
alpha = alpha - (basealpha / steps);
alpha = alpha - basealpha / steps;
}
}
}

View file

@ -74,7 +74,7 @@ public static void DrawRectangle(Rectangle rect, Graphics graphics, RenderMode r
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.None;
bool lineVisible = (lineThickness > 0 && Colors.IsVisible(lineColor));
bool lineVisible = lineThickness > 0 && Colors.IsVisible(lineColor);
if (shadow && (lineVisible || Colors.IsVisible(fillColor)))
{
//draw shadow first
@ -94,7 +94,7 @@ public static void DrawRectangle(Rectangle rect, Graphics graphics, RenderMode r
rect.Height);
graphics.DrawRectangle(shadowPen, shadowRect);
currentStep++;
alpha = alpha - (basealpha / steps);
alpha = alpha - basealpha / steps;
}
}
}

View file

@ -200,7 +200,7 @@ private GraphicsPath CreateTail()
{
Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
int tailLength = GeometryHelper.Distance2D(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2), TargetGripper.Left, TargetGripper.Top);
int tailLength = GeometryHelper.Distance2D(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2, TargetGripper.Left, TargetGripper.Top);
int tailWidth = (Math.Abs(rect.Width) + Math.Abs(rect.Height)) / 20;
// This should fix a problem with the tail being to wide
@ -212,11 +212,11 @@ private GraphicsPath CreateTail()
tail.AddLine(tailWidth, 0, 0, -tailLength);
tail.CloseFigure();
int tailAngle = 90 + (int)GeometryHelper.Angle2D(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2), TargetGripper.Left, TargetGripper.Top);
int tailAngle = 90 + (int)GeometryHelper.Angle2D(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2, TargetGripper.Left, TargetGripper.Top);
using (Matrix tailMatrix = new Matrix())
{
tailMatrix.Translate(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2));
tailMatrix.Translate(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
tailMatrix.Rotate(tailAngle);
tail.Transform(tailMatrix);
}
@ -246,7 +246,7 @@ public override void Draw(Graphics graphics, RenderMode renderMode)
bool shadow = GetFieldValueAsBool(FieldType.SHADOW);
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
bool lineVisible = (lineThickness > 0 && Colors.IsVisible(lineColor));
bool lineVisible = lineThickness > 0 && Colors.IsVisible(lineColor);
Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
if (Selected && renderMode == RenderMode.EDIT)
@ -281,7 +281,7 @@ public override void Draw(Graphics graphics, RenderMode renderMode)
graphics.DrawPath(shadowPen, bubbleClone);
}
currentStep++;
alpha = alpha - (basealpha / steps);
alpha = alpha - basealpha / steps;
}
}
}

View file

@ -136,7 +136,7 @@ public override bool InitContent()
/// </summary>
public override bool HandleMouseDown(int mouseX, int mouseY)
{
return base.HandleMouseDown(mouseX - (Width / 2), mouseY - (Height / 2));
return base.HandleMouseDown(mouseX - Width / 2, mouseY - Height / 2);
}
/// <summary>
@ -169,8 +169,8 @@ protected override void Dispose(bool disposing)
public override bool HandleMouseMove(int x, int y)
{
Invalidate();
Left = x - (Width / 2);
Top = y - (Height / 2);
Left = x - Width / 2;
Top = y - Height / 2;
Invalidate();
return true;
}
@ -191,7 +191,7 @@ public override void Transform(Matrix matrix)
int widthAfter = rect.Width;
int heightAfter = rect.Height;
float factor = (((float)widthAfter / widthBefore) + ((float)heightAfter / heightBefore)) / 2;
float factor = ((float)widthAfter / widthBefore + (float)heightAfter / heightBefore) / 2;
fontSize *= factor;
}

View file

@ -46,8 +46,8 @@ namespace Greenshot.Drawing
/// </summary>
public class Surface : Control, ISurface
{
public static int Count = 0;
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
public static int Count;
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
// Property to identify the Surface ID
private Guid _uniqueId = Guid.NewGuid();
@ -183,7 +183,7 @@ public Guid ID
/// all selected elements, do not serialize
/// </summary>
[NonSerialized]
private DrawableContainerList selectedElements;
private readonly DrawableContainerList selectedElements;
/// <summary>
/// the element we are drawing with, do not serialize
@ -222,7 +222,7 @@ public Guid ID
/// <summary>
/// all stepLabels for the surface, needed with serialization
/// </summary>
private List<StepLabelContainer> _stepLabels = new List<StepLabelContainer>();
private readonly List<StepLabelContainer> _stepLabels = new List<StepLabelContainer>();
public void AddStepLabel(StepLabelContainer stepLabel)
{
@ -244,7 +244,7 @@ public int CountStepLabels(IDrawableContainer stopAtContainer)
int number = 1;
foreach (var possibleThis in _stepLabels)
{
if (possibleThis == stopAtContainer)
if (possibleThis.Equals(stopAtContainer))
{
break;
}
@ -1180,7 +1180,10 @@ private void SurfaceMouseDown(object sender, MouseEventArgs e)
// if a new element has been drawn, set location and register it
if (_drawingElement != null)
{
_drawingElement.Status = _undrawnElement.DefaultEditMode;
if (_undrawnElement != null)
{
_drawingElement.Status = _undrawnElement.DefaultEditMode;
}
_drawingElement.PropertyChanged += ElementPropertyChanged;
if (!_drawingElement.HandleMouseDown(_mouseStart.X, _mouseStart.Y))
{
@ -1540,7 +1543,7 @@ public bool HasSelectedElements
{
get
{
return (selectedElements != null && selectedElements.Count > 0);
return selectedElements != null && selectedElements.Count > 0;
}
}
@ -1606,6 +1609,10 @@ public void ConfirmSelectedConfirmableElements(bool confirm)
{
// create new collection so that we can iterate safely (selectedElements might change due with confirm/cancel)
List<IDrawableContainer> selectedDCs = new List<IDrawableContainer>(selectedElements);
if (_cropContainer == null)
{
return;
}
foreach (IDrawableContainer dc in selectedDCs)
{
if (dc.Equals(_cropContainer))
@ -1619,6 +1626,7 @@ public void ConfirmSelectedConfirmableElements(bool confirm)
}
_cropContainer.Dispose();
_cropContainer = null;
break;
}
}
}
@ -1652,7 +1660,7 @@ public void PasteElementFromClipboard()
{
// Make element(s) only move 10,10 if the surface is the same
Point moveOffset;
bool isSameSurface = (dcs.ParentID == _uniqueId);
bool isSameSurface = dcs.ParentID == _uniqueId;
dcs.Parent = this;
if (isSameSurface)
{

View file

@ -77,7 +77,7 @@ public string Text
internal void ChangeText(string newText, bool allowUndoable)
{
if ((text == null && newText != null) || (text != null && !text.Equals(newText)))
if ((text == null && newText != null) || (text != null && !string.Equals(text, newText)))
{
if (makeUndoable && allowUndoable)
{
@ -139,8 +139,10 @@ protected override void Dispose(bool disposing)
private void Init()
{
_stringFormat = new StringFormat();
_stringFormat.Trimming = StringTrimming.EllipsisWord;
_stringFormat = new StringFormat
{
Trimming = StringTrimming.EllipsisWord
};
CreateTextBox();
@ -208,8 +210,11 @@ private void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e)
// Only dispose the font, and re-create it, when a font field has changed.
if (e.Field.FieldType.Name.StartsWith("FONT"))
{
_font.Dispose();
_font = null;
if (_font != null)
{
_font.Dispose();
_font = null;
}
UpdateFormat();
}
else
@ -218,7 +223,7 @@ private void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e)
}
UpdateTextBoxFormat();
if (_textBox.Visible)
if (_textBox != null && _textBox.Visible)
{
_textBox.Invalidate();
}
@ -231,17 +236,19 @@ public override void OnDoubleClick()
private void CreateTextBox()
{
_textBox = new TextBox();
_textBox = new TextBox
{
ImeMode = ImeMode.On,
Multiline = true,
AcceptsTab = true,
AcceptsReturn = true,
BorderStyle = BorderStyle.None,
Visible = false
};
_textBox.ImeMode = ImeMode.On;
_textBox.Multiline = true;
_textBox.AcceptsTab = true;
_textBox.AcceptsReturn = true;
_textBox.DataBindings.Add("Text", this, "Text", false, DataSourceUpdateMode.OnPropertyChanged);
_textBox.LostFocus += textBox_LostFocus;
_textBox.KeyDown += textBox_KeyDown;
_textBox.BorderStyle = BorderStyle.None;
_textBox.Visible = false;
}
private void ShowTextBox()
@ -348,6 +355,7 @@ protected void UpdateFormat()
}
}
}
_font?.Dispose();
_font = new Font(fam, fontSize, fs, GraphicsUnit.Pixel);
_textBox.Font = _font;
}
@ -392,8 +400,8 @@ private void UpdateTextBoxPosition()
{
lineWidth = 0;
}
_textBox.Width = absRectangle.Width - (2 * lineWidth) + correction;
_textBox.Height = absRectangle.Height - (2 * lineWidth) + correction;
_textBox.Width = absRectangle.Width - 2 * lineWidth + correction;
_textBox.Height = absRectangle.Height - 2 * lineWidth + correction;
}
public override void ApplyBounds(RectangleF newBounds)
@ -459,7 +467,7 @@ public override void Draw(Graphics graphics, RenderMode rm)
DrawSelectionBorder(graphics, rect);
}
if (text == null || text.Length == 0)
if (string.IsNullOrEmpty(text))
{
return;
}
@ -481,12 +489,13 @@ public override void Draw(Graphics graphics, RenderMode rm)
/// <param name="drawingRectange"></param>
/// <param name="lineThickness"></param>
/// <param name="fontColor"></param>
/// <param name="drawShadow"></param>
/// <param name="stringFormat"></param>
/// <param name="text"></param>
/// <param name="font"></param>
public static void DrawText(Graphics graphics, Rectangle drawingRectange, int lineThickness, Color fontColor, bool drawShadow, StringFormat stringFormat, string text, Font font)
{
int textOffset = (lineThickness > 0) ? (int)Math.Ceiling(lineThickness / 2d) : 0;
int textOffset = lineThickness > 0 ? (int)Math.Ceiling(lineThickness / 2d) : 0;
// draw shadow before anything else
if (drawShadow)
{

View file

@ -37,7 +37,7 @@ namespace Greenshot
public partial class ColorDialog : BaseForm
{
private static ColorDialog uniqueInstance;
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private ColorDialog()
{
@ -62,7 +62,7 @@ public static ColorDialog GetInstance()
private readonly List<Button> _colorButtons = new List<Button>();
private readonly List<Button> _recentColorButtons = new List<Button>();
private readonly ToolTip _toolTip = new ToolTip();
private bool _updateInProgress = false;
private bool _updateInProgress;
public Color Color
{

View file

@ -29,7 +29,7 @@ namespace Greenshot.Forms
{
public partial class DropShadowSettingsForm : BaseForm
{
private DropShadowEffect effect;
private readonly DropShadowEffect effect;
public DropShadowSettingsForm(DropShadowEffect effect)
{

View file

@ -505,7 +505,7 @@ protected override void Dispose(bool disposing)
this.propertiesToolStrip.Renderer = new CustomToolStripProfessionalRenderer();
this.propertiesToolStrip.BackColor = System.Drawing.SystemColors.Control;
this.propertiesToolStrip.OverflowButton.DropDown.BackColor = System.Drawing.SystemColors.Control;
this.propertiesToolStrip.Paint += propertiesToolStrip_Paint;
this.propertiesToolStrip.Paint += PropertiesToolStrip_Paint;
this.propertiesToolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsbSaveClose,
this.tsbClose,

File diff suppressed because it is too large Load diff

View file

@ -100,9 +100,9 @@ public void MoveTo(Point screenCoordinates)
/// </summary>
/// <param name="screenCoordinates">Point with the coordinates</param>
/// <returns>Color at the specified screenCoordinates</returns>
static private Color GetPixelColor(Point screenCoordinates)
private static Color GetPixelColor(Point screenCoordinates)
{
using (SafeWindowDCHandle screenDC = SafeWindowDCHandle.fromDesktop())
using (SafeWindowDCHandle screenDC = SafeWindowDCHandle.FromDesktop())
{
try
{

View file

@ -32,9 +32,9 @@ namespace Greenshot
/// </summary>
public partial class ResizeSettingsForm : BaseForm
{
private ResizeEffect effect;
private string value_pixel;
private string value_percent;
private readonly ResizeEffect effect;
private readonly string value_pixel;
private readonly string value_percent;
private double newWidth, newHeight;
public ResizeSettingsForm(ResizeEffect effect)
@ -55,8 +55,8 @@ public ResizeSettingsForm(ResizeEffect effect)
textbox_height.Text = effect.Height.ToString();
newWidth = effect.Width;
newHeight = effect.Height;
combobox_width.SelectedIndexChanged += new System.EventHandler(this.combobox_SelectedIndexChanged);
combobox_height.SelectedIndexChanged += new System.EventHandler(this.combobox_SelectedIndexChanged);
combobox_width.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
combobox_height.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
checkbox_aspectratio.Checked = effect.MaintainAspectRatio;
}
@ -96,7 +96,7 @@ private void displayWidth()
double displayValue;
if (value_percent.Equals(combobox_width.SelectedItem))
{
displayValue = ((double)newWidth / (double)effect.Width) * 100d;
displayValue = (double)newWidth / (double)effect.Width * 100d;
}
else
{
@ -110,7 +110,7 @@ private void displayHeight()
double displayValue;
if (value_percent.Equals(combobox_height.SelectedItem))
{
displayValue = ((double)newHeight / (double)effect.Height) * 100d;
displayValue = (double)newHeight / (double)effect.Height * 100d;
}
else
{
@ -158,16 +158,16 @@ private void textbox_KeyUp(object sender, KeyEventArgs e)
if (isPercent)
{
percent = double.Parse(textbox_width.Text);
newWidth = ((double)effect.Width / 100d) * percent;
newWidth = (double)effect.Width / 100d * percent;
}
else
{
newWidth = double.Parse(textbox_width.Text);
percent = ((double)double.Parse(textbox_width.Text) / (double)effect.Width) * 100d;
percent = (double)double.Parse(textbox_width.Text) / (double)effect.Width * 100d;
}
if (checkbox_aspectratio.Checked)
{
newHeight = ((double)effect.Height / 100d) * percent;
newHeight = (double)effect.Height / 100d * percent;
displayHeight();
}
}
@ -176,16 +176,16 @@ private void textbox_KeyUp(object sender, KeyEventArgs e)
if (isPercent)
{
percent = double.Parse(textbox_height.Text);
newHeight = ((double)effect.Height / 100d) * percent;
newHeight = (double)effect.Height / 100d * percent;
}
else
{
newHeight = double.Parse(textbox_height.Text);
percent = ((double)double.Parse(textbox_height.Text) / (double)effect.Height) * 100d;
percent = (double)double.Parse(textbox_height.Text) / (double)effect.Height * 100d;
}
if (checkbox_aspectratio.Checked)
{
newWidth = ((double)effect.Width / 100d) * percent;
newWidth = (double)effect.Width / 100d * percent;
displayWidth();
}
}

View file

@ -29,7 +29,7 @@ namespace Greenshot.Forms
{
public partial class TornEdgeSettingsForm : BaseForm
{
private TornEdgeEffect effect;
private readonly TornEdgeEffect effect;
public TornEdgeSettingsForm(TornEdgeEffect effect)
{

View file

@ -168,6 +168,7 @@
<Compile Include="Controls\Pipette.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Core\OperatingSystemExtensions.cs" />
<Compile Include="Core\WindowsHelper.cs" />
<Compile Include="Core\BinaryStructHelper.cs" />
<Compile Include="Core\Cache.cs" />

View file

@ -41,9 +41,9 @@ public static int Distance2D(int x1, int y1, int x2, int y2)
//Our end result
int result = 0;
//Take x2-x1, then square it
double part1 = Math.Pow((x2 - x1), 2);
double part1 = Math.Pow(x2 - x1, 2);
//Take y2-y1, then square it
double part2 = Math.Pow((y2 - y1), 2);
double part2 = Math.Pow(y2 - y1, 2);
//Add both of the parts together
double underRadical = part1 + part2;
//Get the square root of the parts

View file

@ -80,7 +80,7 @@ public static RectangleF GetAlignedRectangle(RectangleF currentRect, RectangleF
newRect.X = (targetRect.Width - currentRect.Width) / 2;
break;
case ContentAlignment.TopRight:
newRect.X = (targetRect.Width - currentRect.Width);
newRect.X = targetRect.Width - currentRect.Width;
break;
case ContentAlignment.MiddleLeft:
newRect.Y = (targetRect.Height - currentRect.Height) / 2;
@ -91,18 +91,18 @@ public static RectangleF GetAlignedRectangle(RectangleF currentRect, RectangleF
break;
case ContentAlignment.MiddleRight:
newRect.Y = (targetRect.Height - currentRect.Height) / 2;
newRect.X = (targetRect.Width - currentRect.Width);
newRect.X = targetRect.Width - currentRect.Width;
break;
case ContentAlignment.BottomLeft:
newRect.Y = (targetRect.Height - currentRect.Height);
newRect.Y = targetRect.Height - currentRect.Height;
break;
case ContentAlignment.BottomCenter:
newRect.Y = (targetRect.Height - currentRect.Height);
newRect.Y = targetRect.Height - currentRect.Height;
newRect.X = (targetRect.Width - currentRect.Width) / 2;
break;
case ContentAlignment.BottomRight:
newRect.Y = (targetRect.Height - currentRect.Height);
newRect.X = (targetRect.Width - currentRect.Width);
newRect.Y = targetRect.Height - currentRect.Height;
newRect.X = targetRect.Width - currentRect.Width;
break;
}
return newRect;
@ -351,7 +351,7 @@ public static void Scale(Rectangle boundsBeforeResize, int gripperPosition, int
public static ScaleOptions GetScaleOptions()
{
bool anchorAtCenter = (Control.ModifierKeys & Keys.Control) != 0;
bool maintainAspectRatio = ((Control.ModifierKeys & Keys.Shift) != 0);
bool maintainAspectRatio = (Control.ModifierKeys & Keys.Shift) != 0;
ScaleOptions opts = ScaleOptions.Default;
if (anchorAtCenter) opts |= ScaleOptions.Centered;
if (maintainAspectRatio) opts |= ScaleOptions.Rational;
@ -393,7 +393,7 @@ public double Process(double angle)
public class FixedAngleRoundBehavior : IDoubleProcessor
{
private double fixedAngle;
private readonly double fixedAngle;
public FixedAngleRoundBehavior(double fixedAngle)
{

View file

@ -38,24 +38,24 @@ public class IniConfig
/// <summary>
/// A lock object for the ini file saving
/// </summary>
private static object iniLock = new object();
private static readonly object iniLock = new object();
/// <summary>
/// As the ini implementation is kept someone generic, for reusing, this holds the name of the application
/// </summary>
private static string applicationName = null;
private static string applicationName;
/// <summary>
/// As the ini implementation is kept someone generic, for reusing, this holds the name of the configuration
/// </summary>
private static string configName = null;
private static string configName;
private static string configFolderPath = null;
/// <summary>
/// A Dictionary with all the sections stored by section name
/// </summary>
private static Dictionary<string, IniSection> sectionMap = new Dictionary<string, IniSection>();
private static readonly Dictionary<string, IniSection> sectionMap = new Dictionary<string, IniSection>();
/// <summary>
/// A Dictionary with the properties for a section stored by section name
@ -65,7 +65,7 @@ public class IniConfig
/// <summary>
/// A Dictionary with the fixed-properties for a section stored by section name
/// </summary>
private static Dictionary<string, Dictionary<string, string>> fixedProperties = null;
private static Dictionary<string, Dictionary<string, string>> fixedProperties;
/// <summary>
/// Is the configuration portable (meaning we don't store it in the AppData directory)

View file

@ -30,7 +30,7 @@ public static class IniReader
private const string SECTION_START = "[";
private const string SECTION_END = "]";
private const string COMMENT = ";";
private static char[] ASSIGNMENT = new char[] { '=' };
private static readonly char[] ASSIGNMENT = new[] { '=' };
/**
* Read an ini file to a Dictionary, each key is a section and the value is a Dictionary with name and values.

View file

@ -35,9 +35,9 @@ namespace Greenshot.IniFile
public abstract class IniSection
{
[NonSerialized]
private IDictionary<string, IniValue> values = new Dictionary<string, IniValue>();
private readonly IDictionary<string, IniValue> values = new Dictionary<string, IniValue>();
[NonSerialized]
private IniSectionAttribute iniSectionAttribute = null;
private IniSectionAttribute iniSectionAttribute;
public IniSectionAttribute IniSectionAttribute
{
get

View file

@ -34,10 +34,10 @@ namespace Greenshot.IniFile
/// </summary>
public class IniValue
{
private PropertyInfo propertyInfo;
private FieldInfo fieldInfo;
private IniSection containingIniSection;
private IniPropertyAttribute attributes;
private readonly PropertyInfo propertyInfo;
private readonly FieldInfo fieldInfo;
private readonly IniSection containingIniSection;
private readonly IniPropertyAttribute attributes;
public IniValue(IniSection containingIniSection, PropertyInfo propertyInfo, IniPropertyAttribute iniPropertyAttribute)
{
@ -226,7 +226,7 @@ public void Write(TextWriter writer, bool onlyProperties)
while ((bool)moveNext.Invoke(enumerator, null))
{
var key = current.Invoke(enumerator, null);
var valueObject = item.GetValue(myValue, new object[] { key });
var valueObject = item.GetValue(myValue, new[] { key });
// Write to ini file!
writer.WriteLine("{0}.{1}={2}", attributes.Name, ConvertValueToString(valueType1, key, attributes.Separator), ConvertValueToString(valueType2, valueObject, attributes.Separator));
}
@ -324,7 +324,7 @@ public void UseValueOrDefault(string propertyValue)
LOG.Warn(ex);
//LOG.Error("Problem converting " + stringValue + " to type " + type2.FullName, e);
}
addMethodInfo.Invoke(dictionary, new object[] { newValue1, newValue2 });
addMethodInfo.Invoke(dictionary, new[] { newValue1, newValue2 });
addedElements = true;
}
}
@ -428,7 +428,7 @@ private static object ConvertStringToValueType(Type valueType, string valueStrin
string arraySeparator = separator;
object list = Activator.CreateInstance(valueType);
// Logic for List<>
string[] arrayValues = valueString.Split(new string[] { arraySeparator }, StringSplitOptions.None);
string[] arrayValues = valueString.Split(new[] { arraySeparator }, StringSplitOptions.None);
if (arrayValues == null || arrayValues.Length == 0)
{
return list;
@ -450,7 +450,7 @@ private static object ConvertStringToValueType(Type valueType, string valueStrin
}
if (newValue != null)
{
addMethodInfo.Invoke(list, new object[] { newValue });
addMethodInfo.Invoke(list, new[] { newValue });
}
}
}
@ -460,7 +460,7 @@ private static object ConvertStringToValueType(Type valueType, string valueStrin
if (valueType == typeof(object) && valueString.Length > 0)
{
//LOG.Debug("Parsing: " + valueString);
string[] values = valueString.Split(new Char[] { ':' });
string[] values = valueString.Split(new[] { ':' });
//LOG.Debug("Type: " + values[0]);
//LOG.Debug("Value: " + values[1]);
Type fieldTypeForValue = Type.GetType(values[0], true);

View file

@ -28,14 +28,14 @@ namespace Greenshot.Plugin
{
public class ExportInformation
{
private string uri = null;
private string filepath = null;
private string uri;
private string filepath;
private bool exportMade = false;
private string destinationDesignation = null;
private string destinationDescription = null;
private bool exportMade;
private readonly string destinationDesignation;
private string destinationDescription;
private string errorMessage = null;
private string errorMessage;
public ExportInformation(string destinationDesignation, string destinationDescription)
{

View file

@ -87,10 +87,10 @@ public int CompareTo(object obj)
public class SurfaceOutputSettings
{
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private bool reduceColors;
private bool disableReduceColors;
private List<IEffect> effects = new List<IEffect>();
private readonly List<IEffect> effects = new List<IEffect>();
public SurfaceOutputSettings()
{

View file

@ -55,12 +55,12 @@ public sealed class COMWrapper : RealProxy, IDisposable, IRemotingTypeInfo
/// <summary>
/// The type of which method calls are intercepted and executed on the COM object.
/// </summary>
private Type _InterceptType;
private Type _interceptType;
/// <summary>
/// The humanly readable target name
/// </summary>
private string _TargetName;
private string _targetName;
#endregion Private Data
@ -437,8 +437,8 @@ private COMWrapper(object comObject, Type type, string targetName)
{
_COMObject = comObject;
_COMType = comObject.GetType();
_InterceptType = type;
_TargetName = targetName;
_interceptType = type;
_targetName = targetName;
}
#endregion Construction
@ -451,7 +451,7 @@ private COMWrapper(object comObject, Type type, string targetName)
/// </summary>
~COMWrapper()
{
LOG.DebugFormat("Finalize {0}", _InterceptType.ToString());
LOG.DebugFormat("Finalize {0}", _interceptType);
Dispose(false);
}
@ -475,7 +475,7 @@ private void Dispose(bool disposing)
{
if (null != _COMObject)
{
LOG.DebugFormat("Disposing {0}", _InterceptType.ToString());
LOG.DebugFormat("Disposing {0}", _interceptType);
if (Marshal.IsComObject(_COMObject))
{
try
@ -484,7 +484,7 @@ private void Dispose(bool disposing)
do
{
count = Marshal.ReleaseComObject(_COMObject);
LOG.DebugFormat("RCW count for {0} now is {1}", _InterceptType.ToString(), count);
LOG.DebugFormat("RCW count for {0} now is {1}", _interceptType, count);
} while (count > 0);
}
catch (Exception ex)
@ -514,7 +514,7 @@ private void Dispose(bool disposing)
/// </returns>
public override string ToString()
{
return _InterceptType.FullName;
return _interceptType.FullName;
}
/// <summary>
@ -603,12 +603,12 @@ public static T Cast<T>(object wrapperProxy)
{
throw new ArgumentException("wrapper proxy was no COMWrapper");
}
if (oldWrapper._InterceptType.IsAssignableFrom(newType))
if (oldWrapper._interceptType.IsAssignableFrom(newType))
{
COMWrapper newWrapper = new COMWrapper(oldWrapper._COMObject, newType, oldWrapper._TargetName);
COMWrapper newWrapper = new COMWrapper(oldWrapper._COMObject, newType, oldWrapper._targetName);
return (T)newWrapper.GetTransparentProxy();
}
throw new InvalidCastException(string.Format("{0} is not assignable from {1}", oldWrapper._InterceptType, newType));
throw new InvalidCastException(string.Format("{0} is not assignable from {1}", oldWrapper._interceptType, newType));
}
/// <summary>
@ -665,7 +665,7 @@ public static void DumpTypeInfo(Type type)
{
foreach (MemberInfo memberInfo in type.GetMembers())
{
LOG.InfoFormat("Member: {0};", memberInfo.ToString());
LOG.InfoFormat("Member: {0};", memberInfo);
}
}
catch (Exception memberException)
@ -676,7 +676,7 @@ public static void DumpTypeInfo(Type type)
{
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
LOG.InfoFormat("Property: {0};", propertyInfo.ToString());
LOG.InfoFormat("Property: {0};", propertyInfo);
}
}
catch (Exception propertyException)
@ -687,7 +687,7 @@ public static void DumpTypeInfo(Type type)
{
foreach (FieldInfo fieldInfo in type.GetFields())
{
LOG.InfoFormat("Field: {0};", fieldInfo.ToString());
LOG.InfoFormat("Field: {0};", fieldInfo);
}
}
catch (Exception fieldException)
@ -711,14 +711,14 @@ public override IMessage Invoke(IMessage myMessage)
IMethodCallMessage callMessage = myMessage as IMethodCallMessage;
if (null == callMessage)
{
LOG.DebugFormat("Message type not implemented: {0}", myMessage.GetType().ToString());
LOG.DebugFormat("Message type not implemented: {0}", myMessage.GetType());
return null;
}
MethodInfo method = callMessage.MethodBase as MethodInfo;
if (null == method)
{
LOG.DebugFormat("Unrecognized Invoke call: {0}", callMessage.MethodBase.ToString());
LOG.DebugFormat("Unrecognized Invoke call: {0}", callMessage.MethodBase);
return null;
}
@ -731,18 +731,8 @@ public override IMessage Invoke(IMessage myMessage)
BindingFlags flags = BindingFlags.InvokeMethod;
int argCount = callMessage.ArgCount;
object invokeObject;
Type invokeType;
Type byValType;
object[] args;
object arg;
COMWrapper[] originalArgs;
COMWrapper wrapper;
ParameterModifier[] argModifiers = null;
ParameterInfo[] parameters = null;
ParameterInfo parameter;
if ("Dispose" == methodName && 0 == argCount && typeof(void) == returnType)
{
@ -754,7 +744,7 @@ public override IMessage Invoke(IMessage myMessage)
}
else if ("GetType" == methodName && 0 == argCount && typeof(Type) == returnType)
{
returnValue = _InterceptType;
returnValue = _interceptType;
}
else if ("GetHashCode" == methodName && 0 == argCount && typeof(int) == returnType)
{
@ -777,9 +767,11 @@ public override IMessage Invoke(IMessage myMessage)
}
else
{
invokeObject = _COMObject;
invokeType = _COMType;
var invokeObject = _COMObject;
var invokeType = _COMType;
object[] args;
ParameterInfo parameter;
if (methodName.StartsWith("get_"))
{
// Property Get
@ -822,6 +814,9 @@ public override IMessage Invoke(IMessage myMessage)
}
// Un-wrap wrapped COM objects before passing to the method
Type byValType;
COMWrapper wrapper;
COMWrapper[] originalArgs;
if (null == args || 0 == args.Length)
{
originalArgs = null;
@ -853,7 +848,7 @@ public override IMessage Invoke(IMessage myMessage)
args[i] = new DispatchWrapper(null);
}
}
else if (typeof(Decimal) == byValType)
else if (typeof(decimal) == byValType)
{
// If we're passing a decimal value by reference,
// we need to pass a CurrencyWrapper to avoid a
@ -875,7 +870,7 @@ public override IMessage Invoke(IMessage myMessage)
catch (InvalidComObjectException icoEx)
{
// Should assist BUG-1616 and others
LOG.WarnFormat("COM object {0} has been separated from its underlying RCW cannot be used. The COM object was released while it was still in use on another thread.", _InterceptType.FullName);
LOG.WarnFormat("COM object {0} has been separated from its underlying RCW cannot be used. The COM object was released while it was still in use on another thread.", _interceptType.FullName);
return new ReturnMessage(icoEx, callMessage);
}
catch (Exception ex)
@ -888,7 +883,7 @@ public override IMessage Invoke(IMessage myMessage)
}
if (comEx != null && (comEx.ErrorCode == RPC_E_CALL_REJECTED || comEx.ErrorCode == RPC_E_FAIL))
{
string destinationName = _TargetName;
string destinationName = _targetName;
// Try to find a "catchy" name for the rejecting application
if (destinationName != null && destinationName.Contains("."))
{
@ -896,7 +891,7 @@ public override IMessage Invoke(IMessage myMessage)
}
if (destinationName == null)
{
destinationName = _InterceptType.FullName;
destinationName = _interceptType.FullName;
}
DialogResult result = MessageBox.Show(string.Format("The destination {0} rejected Greenshot access, probably a dialog is open. Close the dialog and try again.",
destinationName), "Greenshot access rejected", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
@ -918,7 +913,7 @@ public override IMessage Invoke(IMessage myMessage)
// Wrap the returned value in an intercepting COM wrapper
if (Marshal.IsComObject(returnValue))
{
returnValue = Wrap(returnValue, returnType, _TargetName);
returnValue = Wrap(returnValue, returnType, _targetName);
}
}
else if (returnType.IsEnum)
@ -939,7 +934,7 @@ public override IMessage Invoke(IMessage myMessage)
continue;
}
arg = args[i];
var arg = args[i];
if (null == arg)
{
continue;
@ -949,7 +944,7 @@ public override IMessage Invoke(IMessage myMessage)
wrapper = null;
byValType = GetByValType(parameter.ParameterType);
if (typeof(Decimal) == byValType)
if (typeof(decimal) == byValType)
{
if (arg is CurrencyWrapper)
{
@ -973,7 +968,7 @@ public override IMessage Invoke(IMessage myMessage)
if (null == wrapper)
{
wrapper = new COMWrapper(arg, byValType, _TargetName);
wrapper = new COMWrapper(arg, byValType, _targetName);
}
arg = wrapper.GetTransparentProxy();
}
@ -995,7 +990,7 @@ public override IMessage Invoke(IMessage myMessage)
/// <returns></returns>
public bool CanCastTo(Type toType, object o)
{
bool returnValue = _InterceptType.IsAssignableFrom(toType);
bool returnValue = _interceptType.IsAssignableFrom(toType);
return returnValue;
}

View file

@ -29,7 +29,7 @@ namespace Greenshot.Interop
[AttributeUsage(AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
public sealed class ComProgIdAttribute : Attribute
{
private string _value;
private readonly string _value;
/// <summary>
/// Extracts the attribute from the specified type.

View file

@ -30,13 +30,13 @@ namespace Greenshot.Memento
/// </summary>
public class AddElementMemento : IMemento
{
private IDrawableContainer drawableContainer;
private Surface surface;
private IDrawableContainer _drawableContainer;
private Surface _surface;
public AddElementMemento(Surface surface, IDrawableContainer drawableContainer)
{
this.surface = surface;
this.drawableContainer = drawableContainer;
_surface = surface;
_drawableContainer = drawableContainer;
}
public void Dispose()
@ -48,8 +48,8 @@ public void Dispose()
protected virtual void Dispose(bool disposing)
{
//if (disposing) { }
drawableContainer = null;
surface = null;
_drawableContainer = null;
_surface = null;
}
public bool Merge(IMemento otherMemento)
@ -60,16 +60,15 @@ public bool Merge(IMemento otherMemento)
public IMemento Restore()
{
// Before
drawableContainer.Invalidate();
_drawableContainer.Invalidate();
// Store the selected state, as it's overwritten by the RemoveElement
bool selected = drawableContainer.Selected;
DeleteElementMemento oldState = new DeleteElementMemento(surface, drawableContainer);
surface.RemoveElement(drawableContainer, false);
drawableContainer.Selected = true;
DeleteElementMemento oldState = new DeleteElementMemento(_surface, _drawableContainer);
_surface.RemoveElement(_drawableContainer, false);
_drawableContainer.Selected = true;
// After
drawableContainer.Invalidate();
_drawableContainer.Invalidate();
return oldState;
}
}

View file

@ -31,8 +31,8 @@ namespace Greenshot.Memento
public class ChangeFieldHolderMemento : IMemento
{
private IDrawableContainer drawableContainer;
private Field fieldToBeChanged;
private object oldValue;
private readonly Field fieldToBeChanged;
private readonly object oldValue;
public ChangeFieldHolderMemento(IDrawableContainer drawableContainer, Field fieldToBeChanged)
{

View file

@ -31,7 +31,7 @@ namespace Greenshot.Memento
public class DeleteElementMemento : IMemento
{
private IDrawableContainer drawableContainer;
private Surface surface;
private readonly Surface surface;
public DeleteElementMemento(Surface surface, IDrawableContainer drawableContainer)
{

View file

@ -32,8 +32,8 @@ namespace Greenshot.Memento
/// </summary>
public class DrawableContainerBoundsChangeMemento : IMemento
{
private List<Point> points = new List<Point>();
private List<Size> sizes = new List<Size>();
private readonly List<Point> points = new List<Point>();
private readonly List<Size> sizes = new List<Size>();
private List<IDrawableContainer> listOfdrawableContainer;
private void StoreBounds()

View file

@ -30,7 +30,7 @@ namespace Greenshot.Memento
public class TextChangeMemento : IMemento
{
private TextContainer textContainer;
private string oldText;
private readonly string oldText;
public TextChangeMemento(TextContainer textContainer)
{

View file

@ -154,7 +154,7 @@ public static void DisableComposition()
/// Helper method for an easy DWM check
/// </summary>
/// <returns>bool true if DWM is available AND active</returns>
public static bool isDWMEnabled()
public static bool IsDwmEnabled()
{
// According to: http://technet.microsoft.com/en-us/subscriptions/aa969538%28v=vs.85%29.aspx
// And: http://msdn.microsoft.com/en-us/library/windows/desktop/aa969510%28v=vs.85%29.aspx

View file

@ -20,6 +20,7 @@
*/
using System;
using System.Diagnostics.CodeAnalysis;
namespace GreenshotPlugin.UnmanagedHelpers
{
@ -27,6 +28,7 @@ namespace GreenshotPlugin.UnmanagedHelpers
/// Window Style Flags
/// </summary>
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowStyleFlags : long
{
//WS_OVERLAPPED = 0x00000000,
@ -79,6 +81,7 @@ public enum WindowStyleFlags : long
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ExtendedWindowStyleFlags : uint
{
WS_EX_DLGMODALFRAME = 0x00000001,
@ -116,6 +119,7 @@ public enum ExtendedWindowStyleFlags : uint
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowPlacementFlags : uint
{
// The coordinates of the minimized window may be specified.
@ -128,6 +132,7 @@ public enum WindowPlacementFlags : uint
WPF_RESTORETOMAXIMIZED = 0x0002
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ShowWindowCommand : uint
{
/// <summary>
@ -155,7 +160,7 @@ public enum ShowWindowCommand : uint
ShowMaximized = 3,
/// <summary>
/// Displays a window in its most recent size and position. This value
/// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
/// is similar to <see cref="ShowWindowCommand.Normal"/>, except
/// the window is not actived.
/// </summary>
ShowNoActivate = 4,
@ -170,13 +175,13 @@ public enum ShowWindowCommand : uint
Minimize = 6,
/// <summary>
/// Displays the window as a minimized window. This value is similar to
/// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
/// <see cref="ShowWindowCommand.ShowMinimized"/>, except the
/// window is not activated.
/// </summary>
ShowMinNoActive = 7,
/// <summary>
/// Displays the window in its current size and position. This value is
/// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
/// similar to <see cref="ShowWindowCommand.Show"/>, except the
/// window is not activated.
/// </summary>
ShowNA = 8,
@ -200,7 +205,8 @@ public enum ShowWindowCommand : uint
ForceMinimize = 11
}
public enum SYSCOLOR : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SYSCOLOR
{
SCROLLBAR = 0,
BACKGROUND = 1,
@ -241,7 +247,8 @@ public enum SYSCOLOR : int
/// ai_productions@verizon.net or osirisgothra@hotmail.com
/// Obtained on pinvoke.net, please contribute your code to support the wiki!
/// </summary>
public enum SystemMetric : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SystemMetric
{
/// <summary>
/// Width of the screen of the primary display monitor, in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, HORZRES).
@ -621,6 +628,7 @@ public enum SystemMetric : int
SM_REMOTECONTROL = 0x2001
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum RegionResult
{
REGION_ERROR = 0,
@ -630,6 +638,7 @@ public enum RegionResult
}
// See http://msdn.microsoft.com/en-us/library/aa969530(v=vs.85).aspx
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DWMWINDOWATTRIBUTE
{
DWMWA_NCRENDERING_ENABLED = 1,
@ -650,6 +659,7 @@ public enum DWMWINDOWATTRIBUTE
DWMWA_LAST
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum GetWindowCommand : uint
{
GW_HWNDFIRST = 0,
@ -662,6 +672,7 @@ public enum GetWindowCommand : uint
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DWM_BB
{
Enable = 1,
@ -669,7 +680,8 @@ public enum DWM_BB
TransitionMaximized = 4
}
public enum ClassLongIndex : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ClassLongIndex
{
GCL_CBCLSEXTRA = -20, // the size, in bytes, of the extra memory associated with the class. Setting this value does not change the number of extra bytes already allocated.
GCL_CBWNDEXTRA = -18, // the size, in bytes, of the extra window memory associated with each window in the class. Setting this value does not change the number of extra bytes already allocated. For information on how to access this memory, see SetWindowLong.
@ -683,7 +695,8 @@ public enum ClassLongIndex : int
GCL_WNDPROC = -24, // the address of the window procedure, or a handle representing the address of the window procedure. You must use the CallWindowProc function to call the window procedure.
}
public enum WindowsMessages : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowsMessages
{
WM_NULL = 0x0000,
WM_CREATE = 0x0001,
@ -891,7 +904,8 @@ public enum WindowsMessages : int
}
// Get/Set WindowLong Enum See: http://msdn.microsoft.com/en-us/library/ms633591.aspx
public enum WindowLongIndex : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowLongIndex
{
GWL_EXSTYLE = -20, // Sets a new extended window style.
GWL_HINSTANCE = -6, // Sets a new application instance handle.
@ -903,7 +917,8 @@ public enum WindowLongIndex : int
// See: http://msdn.microsoft.com/en-us/library/ms633545.aspx
[Flags]
public enum WindowPos : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowPos
{
SWP_ASYNCWINDOWPOS = 0x4000, // If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE = 0x2000, // Prevents generation of the WM_SYNCPAINT message.
@ -922,14 +937,17 @@ public enum WindowPos : int
SWP_SHOWWINDOW = 0x0040 //Displays the window.
}
public enum ScrollBarDirection : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ScrollBarDirection
{
SB_HORZ = 0,
SB_VERT = 1,
SB_CTL = 2,
SB_BOTH = 3
}
public enum ScrollbarCommand : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ScrollbarCommand
{
SB_LINEUP = 0, // Scrolls one line up.
SB_LINEDOWN = 1, // Scrolls one line down.
@ -942,6 +960,7 @@ public enum ScrollbarCommand : int
SB_ENDSCROLL = 8 // Ends scroll.
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ScrollInfoMask
{
SIF_RANGE = 0x1,
@ -956,6 +975,7 @@ public enum ScrollInfoMask
/// See: http://www.pinvoke.net/default.aspx/Enums/SendMessageTimeoutFlags.html
/// </summary>
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SendMessageTimeoutFlags : uint
{
SMTO_NORMAL = 0x0,
@ -965,6 +985,7 @@ public enum SendMessageTimeoutFlags : uint
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
@ -983,7 +1004,8 @@ public enum ProcessAccessFlags : uint
/// See: http://msdn.microsoft.com/en-us/library/aa909766.aspx
/// </summary>
[Flags]
public enum SoundFlags : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SoundFlags
{
SND_SYNC = 0x0000, // play synchronously (default)
SND_ASYNC = 0x0001, // play asynchronously
@ -1001,6 +1023,7 @@ public enum SoundFlags : int
/// Used by GDI32.GetDeviceCaps
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877%28v=vs.85%29.aspx
/// </summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DeviceCaps
{
/// <summary>
@ -1170,7 +1193,8 @@ public enum DeviceCaps
/// Used for User32.SetWinEventHook
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd373640%28v=vs.85%29.aspx
/// </summary>
public enum WinEventHookFlags : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WinEventHookFlags
{
WINEVENT_SKIPOWNTHREAD = 1,
WINEVENT_SKIPOWNPROCESS = 2,
@ -1182,6 +1206,7 @@ public enum WinEventHookFlags : int
/// Used for User32.SetWinEventHook
/// See MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd318066%28v=vs.85%29.aspx
/// </summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WinEvent : uint
{
EVENT_OBJECT_ACCELERATORCHANGE = 32786,
@ -1232,7 +1257,8 @@ public enum WinEvent : uint
/// Used for User32.SetWinEventHook
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd373606%28v=vs.85%29.aspx#OBJID_WINDOW
/// </summary>
public enum EventObjects : int
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum EventObjects
{
OBJID_ALERT = -10,
OBJID_CARET = -8,
@ -1249,6 +1275,7 @@ public enum EventObjects : int
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DesktopAccessRight : uint
{
DESKTOP_READOBJECTS = 0x00000001,

View file

@ -57,7 +57,7 @@ public static bool AreRectangleCornersVisisble(this Region region, Rectangle rec
/// <returns>SafeDeviceContextHandle</returns>
public static SafeDeviceContextHandle GetSafeDeviceContext(this Graphics graphics)
{
return SafeDeviceContextHandle.fromGraphics(graphics);
return SafeDeviceContextHandle.FromGraphics(graphics);
}
}
@ -67,6 +67,7 @@ public static SafeDeviceContextHandle GetSafeDeviceContext(this Graphics graphic
public abstract class SafeObjectHandle : SafeHandleZeroOrMinusOneIsInvalid
{
[DllImport("gdi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteObject(IntPtr hObject);
protected SafeObjectHandle(bool ownsHandle) : base(ownsHandle)
@ -84,9 +85,13 @@ protected override bool ReleaseHandle()
/// </summary>
public class SafeHBitmapHandle : SafeObjectHandle
{
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
private SafeHBitmapHandle() : base(true)
public SafeHBitmapHandle() : base(true)
{
}
[SecurityCritical]
@ -101,8 +106,11 @@ public SafeHBitmapHandle(IntPtr preexistingHandle) : base(true)
/// </summary>
public class SafeRegionHandle : SafeObjectHandle
{
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
private SafeRegionHandle() : base(true)
public SafeRegionHandle() : base(true)
{
}
@ -118,8 +126,11 @@ public SafeRegionHandle(IntPtr preexistingHandle) : base(true)
/// </summary>
public class SafeDibSectionHandle : SafeObjectHandle
{
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
private SafeDibSectionHandle() : base(true)
public SafeDibSectionHandle() : base(true)
{
}
@ -139,23 +150,26 @@ public class SafeSelectObjectHandle : SafeHandleZeroOrMinusOneIsInvalid
[DllImport("gdi32", SetLastError = true)]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
private SafeHandle hdc;
private readonly SafeHandle _hdc;
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
private SafeSelectObjectHandle() : base(true)
public SafeSelectObjectHandle() : base(true)
{
}
[SecurityCritical]
public SafeSelectObjectHandle(SafeDCHandle hdc, SafeHandle newHandle) : base(true)
{
this.hdc = hdc;
_hdc = hdc;
SetHandle(SelectObject(hdc.DangerousGetHandle(), newHandle.DangerousGetHandle()));
}
protected override bool ReleaseHandle()
{
SelectObject(hdc.DangerousGetHandle(), handle);
SelectObject(_hdc.DangerousGetHandle(), handle);
return true;
}
}
@ -173,10 +187,14 @@ protected SafeDCHandle(bool ownsHandle) : base(ownsHandle)
public class SafeCompatibleDCHandle : SafeDCHandle
{
[DllImport("gdi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteDC(IntPtr hDC);
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
private SafeCompatibleDCHandle() : base(true)
public SafeCompatibleDCHandle() : base(true)
{
}
@ -202,23 +220,26 @@ protected override bool ReleaseHandle()
/// </summary>
public class SafeDeviceContextHandle : SafeDCHandle
{
private Graphics graphics = null;
private readonly Graphics _graphics;
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
private SafeDeviceContextHandle() : base(true)
public SafeDeviceContextHandle() : base(true)
{
}
[SecurityCritical]
public SafeDeviceContextHandle(Graphics graphics, IntPtr preexistingHandle) : base(true)
{
this.graphics = graphics;
_graphics = graphics;
SetHandle(preexistingHandle);
}
protected override bool ReleaseHandle()
{
graphics.ReleaseHdc(handle);
_graphics.ReleaseHdc(handle);
return true;
}
@ -227,7 +248,7 @@ public SafeSelectObjectHandle SelectObject(SafeHandle newHandle)
return new SafeSelectObjectHandle(this, newHandle);
}
public static SafeDeviceContextHandle fromGraphics(Graphics graphics)
public static SafeDeviceContextHandle FromGraphics(Graphics graphics)
{
return new SafeDeviceContextHandle(graphics, graphics.GetHdc());
}
@ -261,51 +282,6 @@ public static class GDI32
[DllImport("gdi32", SetLastError = true)]
public static extern int GetDeviceCaps(SafeHandle hdc, DeviceCaps nIndex);
/// <summary>
/// StretchBlt extension for the graphics object
/// Doesn't work?
/// </summary>
/// <param name="target"></param>
/// <param name="source"></param>
public static void StretchBlt(this Graphics target, Bitmap sourceBitmap, Rectangle source, Rectangle destination)
{
using (SafeDeviceContextHandle targetDC = target.GetSafeDeviceContext())
{
using (SafeCompatibleDCHandle safeCompatibleDCHandle = CreateCompatibleDC(targetDC))
{
using (SafeHBitmapHandle hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap()))
{
using (safeCompatibleDCHandle.SelectObject(hBitmapHandle))
{
StretchBlt(targetDC, destination.X, destination.Y, destination.Width, destination.Height, safeCompatibleDCHandle, source.Left, source.Top, source.Width, source.Height, CopyPixelOperation.SourceCopy);
}
}
}
}
}
/// <summary>
/// Bitblt extension for the graphics object
/// </summary>
/// <param name="target"></param>
/// <param name="source"></param>
public static void BitBlt(this Graphics target, Bitmap sourceBitmap, Rectangle source, Point destination, CopyPixelOperation rop)
{
using (SafeDeviceContextHandle targetDC = target.GetSafeDeviceContext())
{
using (SafeCompatibleDCHandle safeCompatibleDCHandle = CreateCompatibleDC(targetDC))
{
using (SafeHBitmapHandle hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap()))
{
using (safeCompatibleDCHandle.SelectObject(hBitmapHandle))
{
BitBlt(targetDC, destination.X, destination.Y, source.Width, source.Height, safeCompatibleDCHandle, source.Left, source.Top, rop);
}
}
}
}
}
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]
@ -441,10 +417,12 @@ public BITMAPINFOHEADER(int width, int height, ushort bpp)
bV5BlueMask = (uint)255;
bV5AlphaMask = (uint)255 << 24;
bV5CSType = 1934772034; // sRGB
bV5Endpoints = new CIEXYZTRIPLE();
bV5Endpoints.ciexyzBlue = new CIEXYZ(0);
bV5Endpoints.ciexyzGreen = new CIEXYZ(0);
bV5Endpoints.ciexyzRed = new CIEXYZ(0);
bV5Endpoints = new CIEXYZTRIPLE
{
ciexyzBlue = new CIEXYZ(0),
ciexyzGreen = new CIEXYZ(0),
ciexyzRed = new CIEXYZ(0)
};
bV5GammaRed = 0;
bV5GammaGreen = 0;
bV5GammaBlue = 0;

View file

@ -111,7 +111,7 @@ public static class GDIplus
[DllImport("gdiplus.dll", SetLastError = true, ExactSpelling = true)]
private static extern int GdipDeleteEffect(IntPtr effect);
private static Guid BlurEffectGuid = new Guid("{633C80A4-1843-482B-9EF2-BE2834C5FDD4}");
private static readonly Guid BlurEffectGuid = new Guid("{633C80A4-1843-482B-9EF2-BE2834C5FDD4}");
// Constant "FieldInfo" for getting the nativeImage from the Bitmap
private static readonly FieldInfo FIELD_INFO_NATIVE_IMAGE = typeof(Bitmap).GetField("nativeImage", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
@ -122,7 +122,7 @@ public static class GDIplus
// Constant "FieldInfo" for getting the nativeImageAttributes from the ImageAttributes
private static readonly FieldInfo FIELD_INFO_NATIVE_IMAGEATTRIBUTES = typeof(ImageAttributes).GetField("nativeImageAttributes", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
private static bool isBlurEnabled = Environment.OSVersion.Version.Major >= 6;
private static bool _isBlurEnabled = Environment.OSVersion.Version.Major >= 6;
/// <summary>
/// Get the nativeImage field from the bitmap
@ -186,9 +186,9 @@ private static IntPtr GetNativeImageAttributes(ImageAttributes imageAttributes)
/// </summary>
/// <param name="radius"></param>
/// <returns></returns>
public static bool isBlurPossible(int radius)
public static bool IsBlurPossible(int radius)
{
if (!isBlurEnabled)
if (!_isBlurEnabled)
{
return false;
}
@ -209,7 +209,7 @@ public static bool isBlurPossible(int radius)
/// <returns>false if there is no GDI+ available or an exception occured</returns>
public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radius, bool expandEdges)
{
if (!isBlurPossible(radius))
if (!IsBlurPossible(radius))
{
return false;
}
@ -248,7 +248,7 @@ public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radiu
}
catch (Exception ex)
{
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem using GdipBitmapApplyEffect: ", ex);
return false;
}
@ -269,7 +269,7 @@ public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radiu
}
catch (Exception ex)
{
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem cleaning up ApplyBlur: ", ex);
}
}
@ -281,7 +281,7 @@ public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radiu
/// <returns>false if there is no GDI+ available or an exception occured</returns>
public static bool DrawWithBlur(Graphics graphics, Bitmap image, Rectangle source, Matrix transform, ImageAttributes imageAttributes, int radius, bool expandEdges)
{
if (!isBlurPossible(radius))
if (!IsBlurPossible(radius))
{
return false;
}
@ -325,7 +325,7 @@ public static bool DrawWithBlur(Graphics graphics, Bitmap image, Rectangle sourc
}
catch (Exception ex)
{
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem using GdipDrawImageFX: ", ex);
return false;
}
@ -346,7 +346,7 @@ public static bool DrawWithBlur(Graphics graphics, Bitmap image, Rectangle sourc
}
catch (Exception ex)
{
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem cleaning up DrawWithBlur: ", ex);
}
}

View file

@ -67,6 +67,7 @@ public class Kernel32
public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags, StringBuilder lpExeName, ref uint lpdwSize);
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
@ -76,6 +77,7 @@ public class Kernel32
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
/// <summary>

View file

@ -43,44 +43,16 @@ public static class Shell32
#region Structs
[StructLayout(LayoutKind.Sequential)]
private struct SHITEMID
{
public ushort cb;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] abID;
}
[StructLayout(LayoutKind.Sequential)]
private struct ITEMIDLIST
{
public SHITEMID mkid;
}
[StructLayout(LayoutKind.Sequential)]
private struct BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpszTitle;
public uint ulFlags;
public IntPtr lpfn;
public int lParam;
public IntPtr iImage;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public readonly IntPtr hIcon;
public readonly int iIcon;
public readonly uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
public readonly string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
public readonly string szTypeName;
};
#endregion Structs
@ -160,7 +132,7 @@ public enum FolderType
/// Returns an icon for a given file extension - indicated by the name parameter.
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx
/// </summary>
/// <param name="name">Filename</param>
/// <param name="filename">Filename</param>
/// <param name="size">Large or small</param>
/// <param name="linkOverlay">Whether to include the link icon</param>
/// <returns>System.Drawing.Icon</returns>
@ -168,24 +140,24 @@ public static Icon GetFileIcon(string filename, IconSize size, bool linkOverlay)
{
SHFILEINFO shfi = new SHFILEINFO();
// SHGFI_USEFILEATTRIBUTES makes it simulate, just gets the icon for the extension
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
if (true == linkOverlay)
if (linkOverlay)
{
flags += Shell32.SHGFI_LINKOVERLAY;
flags += SHGFI_LINKOVERLAY;
}
// Check the size specified for return.
if (IconSize.Small == size)
{
flags += Shell32.SHGFI_SMALLICON;
flags += SHGFI_SMALLICON;
}
else
{
flags += Shell32.SHGFI_LARGEICON;
flags += SHGFI_LARGEICON;
}
SHGetFileInfo(Path.GetFileName(filename), Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags);
SHGetFileInfo(Path.GetFileName(filename), FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags);
// Only return an icon if we really got one
if (shfi.hIcon != IntPtr.Zero)

View file

@ -28,8 +28,8 @@ namespace GreenshotPlugin.UnmanagedHelpers
[StructLayout(LayoutKind.Sequential), Serializable()]
public struct SIZE
{
public int width;
public int height;
public int Width;
public int Height;
public SIZE(Size size) : this(size.Width, size.Height)
{
@ -37,13 +37,13 @@ public SIZE(Size size) : this(size.Width, size.Height)
public SIZE(int width, int height)
{
this.width = width;
this.height = height;
Width = width;
Height = height;
}
public Size ToSize()
{
return new Size(width, height);
return new Size(Width, Height);
}
}

View file

@ -64,7 +64,7 @@ public static class User32
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsWindowVisible(IntPtr hWnd);
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
@ -82,10 +82,10 @@ public static class User32
public static extern int ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int cch);
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int cch);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetWindowTextLength(IntPtr hWnd);
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
public static extern uint GetSysColor(int nIndex);
@ -114,16 +114,16 @@ public static class User32
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsIconic(IntPtr hWnd);
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsZoomed(IntPtr hwnd);
public static extern bool IsZoomed(IntPtr hwnd);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32", SetLastError = true)]
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetClassLong(IntPtr hWnd, int nIndex);
[DllImport("user32", SetLastError = true, EntryPoint = "GetClassLongPtr")]
@ -133,17 +133,17 @@ public static class User32
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32", SetLastError = true)]
public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32", SetLastError = true)]
public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
[DllImport("user32", SetLastError = true, EntryPoint = "GetWindowLong")]
public extern static int GetWindowLong(IntPtr hwnd, int index);
public static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32", SetLastError = true, EntryPoint = "GetWindowLongPtr")]
public extern static IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex);
public static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex);
[DllImport("user32", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int index, int styleFlags);
@ -162,10 +162,10 @@ public static class User32
public static extern bool GetWindowInfo(IntPtr hwnd, ref WindowInfo pwi);
[DllImport("user32", SetLastError = true)]
public extern static int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public static extern int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32", SetLastError = true)]
public extern static int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
@ -173,10 +173,10 @@ public static class User32
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowScrollBar(IntPtr hwnd, ScrollBarDirection scrollBar, bool show);
public static extern bool ShowScrollBar(IntPtr hwnd, ScrollBarDirection scrollBar, [MarshalAs(UnmanagedType.Bool)] bool show);
[DllImport("user32", SetLastError = true)]
public static extern int SetScrollPos(IntPtr hWnd, Orientation nBar, int nPos, bool bRedraw);
public static extern int SetScrollPos(IntPtr hWnd, Orientation nBar, int nPos, [MarshalAs(UnmanagedType.Bool)] bool bRedraw);
[DllImport("user32", SetLastError = true)]
public static extern RegionResult GetWindowRgn(IntPtr hWnd, SafeHandle hRgn);
@ -231,7 +231,7 @@ public static class User32
public static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);
[DllImport("user32", SetLastError = true)]
private static extern bool GetPhysicalCursorPos(out POINT cursorLocation);
@ -446,7 +446,11 @@ protected override bool ReleaseHandle()
/// </summary>
public class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeIconHandle() : base(true)
/// <summary>
/// Needed for marshalling return values
/// </summary>
[SecurityCritical]
public SafeIconHandle() : base(true)
{
}
@ -473,28 +477,30 @@ public class SafeWindowDCHandle : SafeHandleZeroOrMinusOneIsInvalid
[DllImport("user32", SetLastError = true)]
private static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
private IntPtr hWnd;
private readonly IntPtr _hWnd;
[SecurityCritical]
private SafeWindowDCHandle() : base(true)
/// <summary>
/// Needed for marshalling return values
/// </summary>
public SafeWindowDCHandle() : base(true)
{
}
[SecurityCritical]
public SafeWindowDCHandle(IntPtr hWnd, IntPtr preexistingHandle) : base(true)
{
this.hWnd = hWnd;
_hWnd = hWnd;
SetHandle(preexistingHandle);
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
protected override bool ReleaseHandle()
{
bool returnValue = ReleaseDC(hWnd, handle);
bool returnValue = ReleaseDC(_hWnd, handle);
return returnValue;
}
public static SafeWindowDCHandle fromDesktop()
public static SafeWindowDCHandle FromDesktop()
{
IntPtr hWndDesktop = User32.GetDesktopWindow();
IntPtr hDCDesktop = GetWindowDC(hWndDesktop);

View file

@ -853,7 +853,7 @@ public static Bitmap AddShadow(Image sourceImage, float opacity, int size, float
public static void Blur(Bitmap sourceImage, int radius)
{
if (GDIplus.isBlurPossible(radius))
if (GDIplus.IsBlurPossible(radius))
{
GDIplus.ApplyBlur(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), radius, false);
}