Posted in Programming by Mr. Novocain on August 5, 2010 at 22:13

fakeWindows

Hey! You should read C# 4.0 in a Nutshell: The Definitive Reference.

Copies entire screenbuffer to a memorybuffer, then draws the buffer to a window form which has ontop property.

Hence user has a ‘fake’ Windows showing, an image that captures all their input without giving any responce.

A screenshot would be silly.


Source code (DrawingBoard.cs)

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Net;
  7. using System.Diagnostics;
  8. using System.IO;
  9.  
  10. namespace FakeWindows
  11. {
  12.     public partial class DrawingBoard : Form
  13.     {
  14.         public DrawingBoard()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         [DllImport("user32.dll", SetLastError = false)]
  20.         static extern IntPtr GetDesktopWindow();
  21.  
  22.         [DllImport("user32.dll")]
  23.         static extern IntPtr GetWindowDC(IntPtr hWnd);
  24.  
  25.         [DllImport("gdi32.dll", SetLastError = true)]
  26.         static extern IntPtr CreateCompatibleDC(IntPtr hdc);
  27.  
  28.         [DllImport("gdi32.dll")]
  29.         static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
  30.  
  31.         public const int SRCCOPY = 13369376;
  32.         [DllImport("gdi32.dll")]
  33.         public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp);
  34.  
  35.         public const int SM_CXSCREEN = 0;
  36.         public const int SM_CYSCREEN = 1;
  37.         [DllImport("user32.dll")]
  38.         public static extern int GetSystemMetrics(int abc);
  39.  
  40.         [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
  41.         public static extern IntPtr DeleteDC(IntPtr hDc);
  42.  
  43.         [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
  44.         public static extern IntPtr DeleteObject(IntPtr hDc);
  45.  
  46.         [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  47.         public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  48.  
  49.         [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
  50.         public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
  51.  
  52.         Bitmap bmp;
  53.         private void DrawingBoard_Load(object sender, EventArgs e)
  54.         {
  55.             // gets handle for the desktop window (whole screen)
  56.             IntPtr hwnd = GetDesktopWindow();
  57.             if (hwnd == IntPtr.Zero) { return; }
  58.  
  59.             // get device context
  60.             IntPtr hdc = GetWindowDC(hwnd);
  61.             if (hdc == IntPtr.Zero) { return; }
  62.  
  63.             // create a new dc for us to use
  64.             IntPtr hmemdc = CreateCompatibleDC(hdc);
  65.             if (hmemdc == IntPtr.Zero) { return; }
  66.  
  67.             // get screen resolution
  68.             int cx = GetSystemMetrics(SM_CXSCREEN);
  69.             int cy = GetSystemMetrics(SM_CYSCREEN);
  70.  
  71.             // create a bitmap to draw on
  72.             IntPtr hbitmap = CreateCompatibleBitmap(hdc, cx, cy);
  73.             if (hbitmap == IntPtr.Zero) { return; }
  74.  
  75.             IntPtr hold = SelectObject(hmemdc, hbitmap);
  76.  
  77.             // copy all data from desktop window’s dc to our new dc
  78.             bool bitted = BitBlt(hmemdc, 0, 0, cx, cy, hdc, 0, 0, SRCCOPY);
  79.  
  80.             // clean up & store the bitmap
  81.             SelectObject(hmemdc, hold);
  82.             DeleteDC(hmemdc);
  83.             ReleaseDC(hwnd, hdc);
  84.  
  85.             bmp = Image.FromHbitmap(hbitmap);
  86.             DeleteObject(hbitmap);
  87.  
  88.             // cover the whole screen
  89.             this.WindowState = FormWindowState.Maximized;
  90.             this.Refresh();
  91.         }
  92.  
  93.         int x = 0;
  94.         private void timerPaint_Tick(object sender, EventArgs e)
  95.         {
  96.             // make sure our window is on top & in focus (so we capture all user input)
  97.             this.BringToFront();
  98.             this.Focus();
  99.  
  100.             if (x == 100)
  101.             { // only re-paint every 100 ms (we don’t want to flicker)
  102.                 this.Refresh();
  103.                 x = 0;
  104.             } x++;
  105.         }
  106.  
  107.         private void DrawingBoard_Paint(object sender, PaintEventArgs e)
  108.         {
  109.             // draw the ‘fake’ windows onto the window
  110.             this.BackgroundImage = bmp;
  111.         }
  112.  
  113.         private void DrawingBoard_FormClosing(object sender, FormClosingEventArgs e)
  114.         {
  115.             // e.Cancel = false will deny user to close the program
  116.             e.Cancel = false;
  117.         }
  118.     }
  119. }

Source code (DrawingBoard.Designer.cs)

  1. namespace FakeWindows
  2. {
  3.     partial class DrawingBoard
  4.     {
  5.         private System.ComponentModel.IContainer components = null;
  6.  
  7.         protected override void Dispose(bool disposing)
  8.         {
  9.             if (disposing && (components != null)) { components.Dispose(); }
  10.             base.Dispose(disposing);
  11.         }
  12.  
  13.         private void InitializeComponent()
  14.         {
  15.             this.components = new System.ComponentModel.Container();
  16.             this.timerPaint = new System.Windows.Forms.Timer(this.components);
  17.             this.SuspendLayout();
  18.            
  19.             // timerPaint
  20.             this.timerPaint.Enabled = true;
  21.             this.timerPaint.Interval = 1;
  22.             this.timerPaint.Tick += new System.EventHandler(this.timerPaint_Tick);
  23.            
  24.             // DrawingBoard
  25.             this.ClientSize = new System.Drawing.Size(1, 1);
  26.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  27.             this.Name = "DrawingBoard";
  28.             this.ShowIcon = false;
  29.             this.ShowInTaskbar = false;
  30.             this.TopMost = true;
  31.             this.WindowState = System.Windows.Forms.FormWindowState.Minimized; //minimized on startup to wait for maximize after screen has been copied
  32.             this.Load += new System.EventHandler(this.DrawingBoard_Load);
  33.             this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawingBoard_Paint);
  34.             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DrawingBoard_FormClosing);
  35.             this.ResumeLayout(false);
  36.         }
  37.  
  38.         private System.Windows.Forms.Timer timerPaint;
  39.     }
  40. }

Incoming search terms for the article:

 

Leave a Reply