Skip to content

Commit

Permalink
Add PrintScreen solution
Browse files Browse the repository at this point in the history
  • Loading branch information
j3soon committed Mar 15, 2018
1 parent 05e0e33 commit 4384a50
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 0 deletions.
22 changes: 22 additions & 0 deletions PrintScreen.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintScreen", "PrintScreen\PrintScreen.csproj", "{A7E52050-A979-480A-9D22-F8483B3DB538}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A7E52050-A979-480A-9D22-F8483B3DB538}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7E52050-A979-480A-9D22-F8483B3DB538}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7E52050-A979-480A-9D22-F8483B3DB538}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7E52050-A979-480A-9D22-F8483B3DB538}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions PrintScreen/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
70 changes: 70 additions & 0 deletions PrintScreen/PrintScreen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A7E52050-A979-480A-9D22-F8483B3DB538}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PrintScreen</RootNamespace>
<AssemblyName>PrintScreen</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ConfigurationData, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ConfigurationData.1.0.0\lib\net45\ConfigurationData.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="LowLevelControls, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\LowLevelControls.1.1.0\lib\net45\LowLevelControls.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
124 changes: 124 additions & 0 deletions PrintScreen/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfigurationData;
using llc = LowLevelControls;

namespace PrintScreen
{
class Program
{
static llc.KeyboardHook kbdHook = new llc.KeyboardHook();
static Config config = new Config(Path.Combine(AssemblyDirectory, "config"));
static volatile bool loop = true;
static bool prscDown = false;
static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}

static void Main(string[] args)
{
Console.WriteLine("PrintScreen (made by j3soon)");
Console.WriteLine("1. Press PrintScreen to save the entire screen.");
Console.WriteLine("2. Press Alt+PrintScreen to save the current window.");
Console.WriteLine("3. Press Ctrl+C to exit.");
config.DefaultConfigEvent += () => { config["dir"] = @"%UserProfile%\Desktop\Screenshots\"; };
config.Load();
config.Save();
Console.WriteLine("4. The captured screens will be saved in: " + config["dir"]);
kbdHook.KeyDownEvent += kbdHook_KeyDownEvent;
kbdHook.KeyUpEvent += kbdHook_KeyUpEvent;
kbdHook.InstallGlobalHook();
Console.CancelKeyPress += Console_CancelKeyPress;
while (loop)
{
Application.DoEvents();
Thread.Sleep(1);
}
kbdHook.UninstallGlobalHook();
config.Save();
}

private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
loop = false;
}

private static bool kbdHook_KeyDownEvent(llc.KeyboardHook sender, uint vkCode, bool injected)
{
if (vkCode == (uint)Keys.PrintScreen && !prscDown)
{
if (llc.Keyboard.IsKeyDown((int)Keys.Menu))
Console.WriteLine("Saved Current Window - " + PrintWindow());
else
Console.WriteLine("Saved Current Screen - " + PrintScreen());
prscDown = true;
return true;
}
return false;
}

private static bool kbdHook_KeyUpEvent(llc.KeyboardHook sender, uint vkCode, bool injected)
{
if (vkCode == (uint)Keys.PrintScreen)
prscDown = false;
return false;
}

static String GetScreenshotName()
{
String path = Environment.ExpandEnvironmentVariables(config["dir"]);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(path, DateTime.Now.ToString("yyyyMMdd_HH-mm-ss"));
int num = 0;
for (; File.Exists(Path.Combine(path, num == 0 ? "" : ("_" + num)) + ".png"); num++) ;
return path + (num == 0 ? "" : ("_" + num)) + ".png";
}

static String PrintScreen()
{
Rectangle bounds = SystemInformation.VirtualScreen;
Bitmap bmp = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bmp))
g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
String name = GetScreenshotName();
bmp.Save(name, ImageFormat.Png);
return name;
}

static String PrintWindow()
{
llc.Natives.RECT rect = llc.Window.GetWindowBounds(llc.Window.GetForegroundWindow());
Rectangle bounds = new Rectangle
{
X = rect.left,
Y = rect.top,
Width = rect.right - rect.left,
Height = rect.bottom - rect.top
};
Bitmap bmp = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bmp))
g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
String name = GetScreenshotName();
bmp.Save(name, ImageFormat.Png);
return name;
}
}
}
36 changes: 36 additions & 0 deletions PrintScreen/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PrintScreen")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PrintScreen")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a7e52050-a979-480a-9d22-f8483b3db538")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
5 changes: 5 additions & 0 deletions PrintScreen/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ConfigurationData" version="1.0.0" targetFramework="net45" />
<package id="LowLevelControls" version="1.1.0" targetFramework="net45" />
</packages>

0 comments on commit 4384a50

Please sign in to comment.