Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting the active window handle

Getting the active window handle

Scheduled Pinned Locked Moved C#
tutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CyberKewl
    wrote on last edited by
    #1

    I would like to know how to get the handle of the currently active window. I've tried using Findwindow and it works, but i want the whole view of the active window (including the toolbar and menubar) as i would like to create a program to take a screenshot of the active window (something like ALT+PRINTSCREEN but using BitBlt to do the capturing).

    H 1 Reply Last reply
    0
    • C CyberKewl

      I would like to know how to get the handle of the currently active window. I've tried using Findwindow and it works, but i want the whole view of the active window (including the toolbar and menubar) as i would like to create a program to take a screenshot of the active window (something like ALT+PRINTSCREEN but using BitBlt to do the capturing).

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      I'm not sure what problems you're having, but the typical way is similar to the following pseudo code:

      HWND hWnd = FindWindow(NULL, "MyApp");
      RECT rc;
      GetClientRect(hWnd, &rc);
      HDC hDC = GetDC(hWnd);
      HDC hCDC = CreateCompatibleDC(hDC);
      int width = rc.right - rc.left;
      int height = rc.bottom - rc.top;
      BitBlt(hCDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY | CAPTUREBLT);

      Is this what you're doing (roughly)?

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      C 1 Reply Last reply
      0
      • H Heath Stewart

        I'm not sure what problems you're having, but the typical way is similar to the following pseudo code:

        HWND hWnd = FindWindow(NULL, "MyApp");
        RECT rc;
        GetClientRect(hWnd, &rc);
        HDC hDC = GetDC(hWnd);
        HDC hCDC = CreateCompatibleDC(hDC);
        int width = rc.right - rc.left;
        int height = rc.bottom - rc.top;
        BitBlt(hCDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY | CAPTUREBLT);

        Is this what you're doing (roughly)?

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        C Offline
        C Offline
        CyberKewl
        wrote on last edited by
        #3

        Yeah more or less. Here's the exact code : hWnd = FindWindow(null, "Form1"); RECT rc = new RECT(); GetWindowRect(hWnd, ref rc); this.Text = "LEFT" + rc.left + " RIGHT " + rc.right + " TOP " + rc.top; Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics gr1 = Graphics.FromImage(myImage); IntPtr dc1 = gr1.GetHdc(); IntPtr dc2 = GetDC(hWnd); BitBlt(dc1, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, dc2, 0, 0, 13369376); gr1.ReleaseHdc(dc1); ReleaseDC(GetDC(hWnd)), dc2); GC.Collect(); Graphics gr = Graphics.FromHwnd(pictureBox1.Handle); pictureBox1.Image = myImage;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups