Thomas

Thomas

  • NA
  • 24
  • 0

Capturing screenshot using shortcut keys

Jul 3 2014 2:18 AM

Hi there.

 I have this application that basically captures a screenshot of the active window. In essence, if I double-click the application from its folder, it will capture a screenshot of the folder. If I create a shortcut to the application and double-click the shortcut it still Works perfectly. However, if I use shortcut keys to start the shortcut, it captures the process bar(start menu, systray etc.) and not the "active folder". I use windows 8, Visual Studio 2012. I have tried to build both in .net framework 3,5 and 4,5. I hope that anyone can help me, my code is below:

using System;

using System.Data;

using System.Diagnostics;

using System.Drawing;

using System.Runtime.InteropServices;

using System.Threading;

using System.Windows.Forms;

namespace HardCopy

{

public partial class HardCopy : Form

{

Bitmap screenShotBMP;

Rectangle rect = new Rectangle();

[DllImport("user32.dll")]

static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", SetLastError = true)]

static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);

[StructLayout(LayoutKind.Sequential)]

public struct RECT

{

public int Left;

public int Top;

public int Right;

public int Bottom;

}

public HardCopy()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.Hide();

Thread.Sleep(200);

GetWindowRect(GetForegroundWindow(), out rect);

screenShotBMP = new Bitmap(rect.Width - rect.X, rect.Height - rect.Y);

using (Graphics gr = Graphics.FromImage(screenShotBMP))

{

gr.CopyFromScreen(rect.Location, Point.Empty, rect.Size);

}

pictureBox1.Image = screenShotBMP;

this.Show();

}

}

}

 

Answers (2)