C# ScreenCapture [duplicate] - c#

This question already has answers here:
Capture screenshot of active window?
(13 answers)
Closed 3 years ago.
I am trying to capture the screen using Visual Studio 12 (sorry, we have to use 12) cand C#.
I found an algorithm here: Capture screenshot of active window?
ScreenCapture sc = new ScreenCapture();
I tried this and got an error message that the type or namespace is not found.
This is not unexpected. I figured I just need another "using..." statement up at the top.
I tried to do a search for which package to do a "using" on to get ScreenCapture() but I come up empty handed. Does anyone know?

I think it is probably giving you an error around using System.Runtime.InteropServices;
You need to add the reference of the DLL System.Runtime.InteropServices

This lib doesn't seem to exist on NuGet. However, you can find it at Developer Fusion.
The downloads section only includes a zip file with the following class, that you can copy-paste in your VS Project
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace ScreenShotDemo
{
/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
{
/// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow( User32.GetDesktopWindow() );
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest,hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename,format);
}
/// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename,format);
}
/// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
int nXSrc,int nYSrc,int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
}
/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
}
}
}

According to the documentation you need to use Windows.Media.Capture. Please note that
Important
The ScreenCapture feature is only supported for mobile devices running Windows Phone 8.1. This API is not supported on Windows 10.
You can check this answer which works on all windows desktop platforms.

Related

Take screenshot and send it to printer Asp.net MVC 5

I am trying to implement the functionality of taking screenshot and sending the capture to printer. No need to implement the print functionality,user will decide whether to print or cancel that particular document. After digging around the internet and tweaking it to my requirement I am able to save the screenshot locally but unable to send it to printer.
public ActionResult About()
{
ScreenCapture sc = new ScreenCapture();
// capture entire screen, and save it to a file
Image img = sc.CaptureScreen();
// capture this window, and save it
sc.CaptureScreenToFile("C:\\temp2.png", ImageFormat.Png);
return View();
}
public class ScreenCapture
{
/// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
/// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename, format);
}
/// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}
}
Any leads/help will be appreciated. I have come across the PrintDocument class but they were mostly used in WindowsForm/WPF, was not very confident about using in MVC. Thanks in Advance

Screenshot of all open windows

I'm trying to save screenshot of all the open windows on windows 7.
Issue is that instead of getting screenshot of open windows, it's giving me cmd.exe screenshot. (For example instead of taking google chrome screenshot it shows black cmd.exe screenshot).
I tried the same code on windows 10 before and it seemed to be working.
I have used following code from the http://www.developerfusion.com/code/4630/capture-a-screen-shot/ to get the screenshot of a particular window.
namespace ScreenShotDemo
{
/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
{
/// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
//User32.RECT windowRect = new User32.RECT();
//User32.GetWindowRect(handle, ref windowRect);
//int width = windowRect.right - windowRect.left;
//int height = windowRect.bottom - windowRect.top;
User32.WINDOWINFO info = new User32.WINDOWINFO();
User32.GetWindowInfo(handle, ref info);
int width = info.rcWindow.right - info.rcWindow.left;
int height = info.rcWindow.bottom - info.rcWindow.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
/// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename, format);
}
/// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
/// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
public static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWINFO
{
public uint cbSize;
public RECT rcWindow;
public RECT rcClient;
public uint dwStyle;
public uint dwExStyle;
public uint dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public ushort atomWindowType;
public ushort wCreatorVersion;
public WINDOWINFO(Boolean? filler)
: this() // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
{
cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
}
}
}
}
}
To get the list of open windows I'm using following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using HWND = System.IntPtr;
/// <summary>Contains functionality to get all the open windows.</summary>
public static class OpenWindowGetter
{
/// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
/// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
public static IDictionary<HWND, string> GetOpenWindows()
{
HWND shellWindow = GetShellWindow();
Dictionary<HWND, string> windows = new Dictionary<HWND, string>();
EnumWindows(delegate (HWND hWnd, int lParam)
{
if (hWnd == shellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd, builder, length + 1);
windows[hWnd] = builder.ToString();
return true;
}, 0);
return windows;
}
private delegate bool EnumWindowsProc(HWND hWnd, int lParam);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(HWND hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(HWND hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
}
Following is the Main class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScreenShotDemo;
using System.Drawing.Imaging;
namespace ScreenCaptureTest
{
class Program
{
static void Main(string[] args)
{
long start = DateTime.Now.Millisecond;
captureAllWindowsToFile();
//captureAllWindowsToFile2();
//captureDesktopToFile();
long end = DateTime.Now.Millisecond;
Console.WriteLine("time taken: " + (end - start));
//Console.Read();
}
static void captureAllWindowsToFile()
{
IDictionary<IntPtr, string> windowDict = OpenWindowGetter.GetOpenWindows();
ScreenCapture sc = new ScreenCapture();
int captureI = 0;
string message = "";
foreach(KeyValuePair<IntPtr, string> window in windowDict)
{
string imageName = "test" + ++captureI + ".jpg";
message +=imageName + " : " + window.Value + "\n";
sc.CaptureWindowToFile(window.Key, imageName, ImageFormat.Jpeg);
}
Console.WriteLine(message);
}
static void captureDesktopToFile()
{
ScreenCapture sc = new ScreenCapture();
sc.CaptureScreenToFile("current.jpg", ImageFormat.Jpeg);
}
}
}
Any help is appreciated.
I finally found the issue.
I had changed windows 7 settings for best performance, once I changed it to normal Aero settings. It works as expected.

"Custom Cursor cannot be converted to String" Exception is thrown while converting the cursor to string

I am trying to change the cursor of my control on certain conditions. I have created my own custom cursor and assigned it to the Cursor.Current property. Everything working fine up-to this.
When I check whether the current control cursor and the new cursor is same or not based on their names by converting cursor using ToString() method, I get:
Custom Cursor cannot be converted to String
There is no problem in converting the System's default cursor and it raise only when converting custom cursor. Any one please tell me why this exception raised only on custom cursor?
this is the place where i got the error...
**if (m_cursorAction.ToString() != newCursor.ToString())
m_cursorAction = newCursor;**
Here is my cursor implementation code:
Bitmap bitmap = new Bitmap(140, 25);
Graphics g = Graphics.FromImage(bitmap);
using (Font f = new Font("SEGOE UI", 10))
g.DrawString("Node 30", f, System.Drawing.Brushes.Black, 0, 0);
Cursor.Current = MyCursor.CreateCursor(bitmap, 140, 25);
**if (m_cursorAction.ToString() != newCursor.ToString())**
m_cursorAction = newCursor;
public class MyCursor
{
#region Class members
private static IntPtr ptr;
public struct IconInfo
{
public bool bIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
#endregion
#region Class Public Methods
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool DestroyIcon(IntPtr handle);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern IntPtr CreateIconIndirect([System.Runtime.InteropServices.In]ref IconInfo icon);
[System.Runtime.InteropServices.DllImport("gdi32")]
extern internal static bool DeleteObject(IntPtr hObject);
/// <summary>
/// Create a custom cursor with the given bitmap
/// </summary>
/// <param name="bmp">Bitmap for the cursor.</param>
/// <param name="xHotSpot">x hot spot to the cursor.</param>
/// <param name="yHotSpot">y hot spot to the cursor.</param>
public static System.Windows.Forms.Cursor CreateCursor(System.Drawing.Bitmap bmp, int xHotSpot, int yHotSpot)
{
IntPtr bmpPtr= bmp.GetHicon();
IconInfo icon = new IconInfo();
GetIconInfo(bmpPtr, ref icon);
icon.xHotspot = xHotSpot;
icon.yHotspot = yHotSpot;
icon.bIcon = false;
DestroyIcon(bmpPtr);
DeleteObject(bmpPtr);
ptr = CreateIconIndirect(ref icon);
System.Windows.Forms.Cursor cursor = new System.Windows.Forms.Cursor(ptr);
//delete the GDI objects and icon
DeleteObject(icon.hbmColor);
DeleteObject(icon.hbmMask);
DestroyIcon(icon.hbmColor);
DestroyIcon(icon.hbmMask);
return cursor;
}
/// <summary>
/// Destroy the custom cursor
/// </summary>
public static void Destroy()
{
DestroyIcon(ptr);
DeleteObject(ptr);
}
#endregion
}
Because it delegates to CursorConverter.ConvertTo, which is designed to throw that exception only for custom cursors.
Just don't use string comparison to test whether cursors are equal. Instead, use the Cursor.Equals method which is designed for comparing cursors.
One way, for custom Cursors, if you know what you're looking for, is to check the Handle:
// 65569 = Hand Pointer:
int handle = 65569;
if (handle == System.Windows.Forms.Cursor.Current.Handle.ToInt32())
MessageBox.Show("Hand Pointer Cursor");

Get user attention without stealing focus

I have a program that lets the user open several forms. Once a given event occurs (ex : 30 seconds have passed) I need to get user attention on the Form that triggered the event, without stealing the focus.
I already get the form on top with:
f.TopMost = true;
but I'd like to implement some alternative to that. Since changing the border color of the frame seems a nearly impossible task ( this solution would have been the best one), does anyone has an idea on how to get attention without stealing focus?
Option A: You need to use FlashWindowEx from the windows API. This isn't available in .NET, so you need to use PInvoke.
Option B: Use a balloon tip from the system tray. This is built into .NET, but requires that your application use a notification icon, which you might not want. More details here: http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.showballoontip.aspx
Here is the example for how to use Option A:
pInvoke.net has the best example: http://pinvoke.net/default.aspx/user32.FlashWindowEx
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
User-Defined Types:
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
public UInt32 cbSize;
public IntPtr hwnd;
public UInt32 dwFlags;
public UInt32 uCount;
public UInt32 dwTimeout;
}
Notes:
//Stop flashing. The system restores the window to its original state.
public const UInt32 FLASHW_STOP = 0;
//Flash the window caption.
public const UInt32 FLASHW_CAPTION = 1;
//Flash the taskbar button.
public const UInt32 FLASHW_TRAY = 2;
//Flash both the window caption and taskbar button.
//This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
public const UInt32 FLASHW_ALL = 3;
//Flash continuously, until the FLASHW_STOP flag is set.
public const UInt32 FLASHW_TIMER = 4;
//Flash continuously until the window comes to the foreground.
public const UInt32 FLASHW_TIMERNOFG = 12;
Tips & Tricks:
Please add some!
Sample Code:
/// <summary>
/// Flashes a window
/// </summary>
/// <param name="hWnd">The handle to the window to flash</param>
/// <returns>whether or not the window needed flashing</returns>
public static bool FlashWindowEx(IntPtr hWnd)
{
FLASHWINFO fInfo = new FLASHWINFO();
fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = hWnd;
fInfo.dwFlags = FLASHW_ALL;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;
return FlashWindowEx(ref fInfo);
}
...
/// Minor adjust to the code above
/// <summary>
/// Flashes a window until the window comes to the foreground
/// Receives the form that will flash
/// </summary>
/// <param name="hWnd">The handle to the window to flash</param>
/// <returns>whether or not the window needed flashing</returns>
public static bool FlashWindowEx(Form frm)
{
IntPtr hWnd = frm.Handle;
FLASHWINFO fInfo = new FLASHWINFO();
fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = hWnd;
fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;
return FlashWindowEx(ref fInfo);
}
Here is the official Microsoft example: http://msdn.microsoft.com/en-us/library/ms679347(v=vs.85).aspx
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
/// <summary>
/// The size of the structure in bytes.
/// </summary>
public uint cbSize;
/// <summary>
/// A Handle to the Window to be Flashed. The window can be either opened or minimized.
/// </summary>
public IntPtr hwnd;
/// <summary>
/// The Flash Status.
/// </summary>
public FlashWindowFlags dwFlags; //uint
/// <summary>
/// The number of times to Flash the window.
/// </summary>
public uint uCount;
/// <summary>
/// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
/// </summary>
public uint dwTimeout;
}
public enum FlashWindowFlags : uint
{
/// <summary>
/// Stop flashing. The system restores the window to its original state.
/// </summary>
FLASHW_STOP = 0,
/// <summary>
/// Flash the window caption.
/// </summary>
FLASHW_CAPTION = 1,
/// <summary>
/// Flash the taskbar button.
/// </summary>
FLASHW_TRAY = 2,
/// <summary>
/// Flash both the window caption and taskbar button.
/// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
/// </summary>
FLASHW_ALL = 3,
/// <summary>
/// Flash continuously, until the FLASHW_STOP flag is set.
/// </summary>
FLASHW_TIMER = 4,
/// <summary>
/// Flash continuously until the window comes to the foreground.
/// </summary>
FLASHW_TIMERNOFG = 12
}
public static bool FlashWindow(IntPtr hWnd,
FlashWindowFlags fOptions,
uint FlashCount,
uint FlashRate)
{
if(IntPtr.Zero != hWnd)
{
FLASHWINFO fi = new FLASHWINFO();
fi.cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO));
fi.dwFlags = fOptions;
fi.uCount = FlashCount;
fi.dwTimeout = FlashRate;
fi.hwnd = hWnd;
return FlashWindowEx(ref fi);
}
return false;
}
public static bool StopFlashingWindow(IntPtr hWnd)
{
if(IntPtr.Zero != hWnd)
{
FLASHWINFO fi = new FLASHWINFO();
fi.cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO));
fi.dwFlags = (uint)FlashWindowFlags.FLASHW_STOP;
fi.hwnd = hWnd;
return FlashWindowEx(ref fi);
}
return false;
}
In Windows 7, a progress bar on a form is represented in its taskbar button; you might leverage that. There's also got to be a way to simply highlight the taskbar button, like IM programs do when you get a new message.

Is there a way to make a console window flash in the task bar programmatically

Basically I made console app that performs some task that takes a few minutes. I'd like to have it flash in the taskbar to let me know when it's done doing its thing.
Using the answer that #Zack posted and another one to find the handle of a console app I came up with this and it works great.
class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
public UInt32 cbSize;
public IntPtr hwnd;
public UInt32 dwFlags;
public UInt32 uCount;
public Int32 dwTimeout;
}
public const UInt32 FLASHW_ALL = 3;
static void Main(string[] args)
{
Console.WriteLine("Flashing NOW");
FlashWindow(Process.GetCurrentProcess().MainWindowHandle);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
private static void FlashWindow(IntPtr hWnd)
{
FLASHWINFO fInfo = new FLASHWINFO();
fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = hWnd;
fInfo.dwFlags = FLASHW_ALL;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;
FlashWindowEx(ref fInfo);
}
}
Combining the answer in the question linked in #Zack's comment and getting the hwnd of a console window using this I was able to get it working. This is the class I created:
public static class FlashWindow
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
public UInt32 cbSize;
public IntPtr hwnd;
public UInt32 dwFlags;
public UInt32 uCount;
public UInt32 dwTimeout;
}
public const UInt32 FLASHW_ALL = 3;
public static void Flash()
{
FLASHWINFO fInfo = new FLASHWINFO();
fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = GetConsoleWindow();
fInfo.dwFlags = FLASHW_ALL;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;
FlashWindowEx(ref fInfo);
}
}
It doesn't ever stop flashing until it's closed but that wasn't important for my purposes.
I read that it wasn't possible to get the window handle of a console window through any direct means, but it seems to be pretty simple in .NET actually. So, it's pretty much the same as this question:
class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
public UInt32 cbSize;
public IntPtr hwnd;
public UInt32 dwFlags;
public UInt32 uCount;
public UInt32 dwTimeout;
}
public const UInt32 FLASHW_STOP = 0;
public const UInt32 FLASHW_CAPTION = 1;
public const UInt32 FLASHW_TRAY = 2;
public const UInt32 FLASHW_ALL = 3;
public const UInt32 FLASHW_TIMER = 4;
public const UInt32 FLASHW_TIMERNOFG = 12;
static void Main(string[] args)
{
// Give you a few seconds to alt-tab away :)
Thread.Sleep(2000);
// Flash on the task bar, until the window becomes the foreground window.
// Constants for other behaviors are defined above.
FLASHWINFO fInfo = new FLASHWINFO();
fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = Process.GetCurrentProcess().MainWindowHandle;
fInfo.dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;
FlashWindowEx(ref fInfo);
// Wait for input so the app doesn't finish right away.
Console.ReadLine();
}
}
I looked into the issue #Davy8 had in which the flashing doesn't stop. The solution was pretty simple, just pass the FLASHW_STOP constant. To show this, I further augmented #Davy8's static class to include a StopFlashing static function. I also decided to add comments based on the Microsoft Documentation such that it is easy to see why these are applied in C#.
/// <summary>
/// Class for flashing a console window
/// <see cref="https://learn.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-flashwinfo"/>
/// </summary>
public static class FlashWindow
{
/// <summary>
/// Flashes the specified window. It does not change the active state of the window.
/// </summary>
/// <param name="pwfi">FLASHWINFO</param>
/// <see cref="https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-flashwindowex"/>
/// <returns>If the window caption was drawn as active before the call, the return value is nonzero. Otherwise, the return value is zero.</returns>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
/// <summary>
/// Retrieves the window handle used by the console associated with the calling process.
/// </summary>
/// <see cref="https://learn.microsoft.com/en-us/windows/console/getconsolewindow"/>
/// <returns>The return value is a handle to the window used by the console associated with the calling process or NULL if there is no such associated console.</returns>
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
/// <summary>
/// Contains the flash status for a window and the number of times the system should flash the window.
/// <see cref="https://learn.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-flashwinfo"/>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
/// <summary>
/// The size of the structure, in bytes
/// </summary>
public UInt32 cbSize;
/// <summary>
/// A handle to the window to be flashed. The window can be either opened or minimized.
/// </summary>
public IntPtr hwnd;
/// <summary>
/// The flash status. This parameter can be one or more of the following values.
/// </summary>
public UInt32 dwFlags;
/// <summary>
/// The number of times to flash the window.
/// </summary>
public UInt32 uCount;
/// <summary>
/// The rate at which the window is to be flashed, in milliseconds. If dwTimeout is zero, the function uses the default cursor blink rate.
/// </summary>
public UInt32 dwTimeout;
}
/// <summary>
/// Flash both the window caption and taskbar button. This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
/// </summary>
public const UInt32 FLASHW_ALL = 0x00000003;
/// <summary>
/// Flash the window caption.
/// </summary>
public const UInt32 FLASHW_CAPTION = 0x00000001;
/// <summary>
/// Stop flashing. The system restores the window to its original state.
/// </summary>
public const UInt32 FLASHW_STOP = 0x00000004;
/// <summary>
/// Flash continuously, until the FLASHW_STOP flag is set.
/// </summary>
public const UInt32 FLASHW_TIMER = 4;
/// <summary>
/// Flash continuously until the window comes to the foreground.
/// </summary>
public const UInt32 FLASHW_TIMERNOFG = 0x0000000C;
/// <summary>
/// Flash the taskbar button.
/// </summary>
public const UInt32 FLASHW_TRAY = 0x00000002;
/// <summary>
/// Create an instance of the FLASHWINFO structure
/// </summary>
/// <param name="flashwConstant">One of the provided FLASHW contant values</param>
/// <param name="uCount">uCount to initialize the struct</param>
/// <param name="dwTimeout">dwTimeout to initalize the struct</param>
/// <returns>A fully instantiated FLASHWINFO struct</returns>
private static FLASHWINFO GetFLASHWINFO(UInt32 flashwConstant, UInt32 uCount = UInt32.MaxValue, UInt32 dwTimeout = 0)
{
FLASHWINFO fInfo = new FLASHWINFO
{
hwnd = GetConsoleWindow(),
dwFlags = flashwConstant,
uCount = uCount,
dwTimeout = dwTimeout
};
fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
return fInfo;
}
/// <summary>
/// Flashes the console window (continues indefinitely)
/// </summary>
public static void Flash()
{
FLASHWINFO fInfo = GetFLASHWINFO(FLASHW_ALL);
FlashWindowEx(ref fInfo);
}
/// <summary>
/// Stops the flashing of the console window
/// </summary>
public static void StopFlash()
{
FLASHWINFO fInfo = GetFLASHWINFO(FLASHW_STOP);
FlashWindowEx(ref fInfo);
}
}

Categories

Resources