get screenshot

This is a discussion on get screenshot within the Windows Programming forums, part of the Platform Specific Boards category; I'm making a program that I want to simply get a screenshot of the desktop and then display that single ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    get screenshot

    I'm making a program that I want to simply get a screenshot of the desktop and then display that single image in the background of the window I create. I can get everything to work so far except to get the bitmap to be drawn in the window (I'm assuming it gets captured). This is my first time working with windows, so on a large scale I don't know what I'm doing. I'd really appreciate if someone could look at my file and see if you can tell me what I'm doing wrong.
    Attached Files Attached Files

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Screen capture ( desktop capture ) questions arise periodically so I have modified your code to accomplish that and as a reference for future questions. Further examples can be found in the CodeGuru article various methods for capturing the screen.
    Code:
    /*screen capture example*/
    #include <windows.h>
    
    #define SCREENWIDTH GetSystemMetrics(SM_CXSCREEN)
    #define SCREENHEIGHT GetSystemMetrics(SM_CYSCREEN)
    
    HBITMAP g_hDeskBmp;
    HDC     g_hMemDC;
    int     g_nDCdata;
    
    void CaptureDesktop()
    {
      /*capture desktop and store for later use*/
      HDC hdcDesk;
      hdcDesk=GetDC(HWND_DESKTOP); /*equivalent to hdcDesk=GetDC((HWND)0);*/
      g_hMemDC=CreateCompatibleDC(0);
      g_hDeskBmp=CreateCompatibleBitmap(hdcDesk, SCREENWIDTH, SCREENHEIGHT);
      /*fill the bitmap with contents of desktop*/
      g_nDCdata=SaveDC(g_hMemDC);
      SelectObject(g_hMemDC,g_hDeskBmp);
      BitBlt(g_hMemDC, 0, 0, SCREENWIDTH, SCREENHEIGHT,hdcDesk,0,0,SRCCOPY);
      ReleaseDC(HWND_DESKTOP,hdcDesk);
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    
      switch(msg)
      {
      
      case WM_CREATE:
      {
        CaptureDesktop();
        break;
      }
      
      case WM_PAINT:
      {
        PAINTSTRUCT ps;
        BeginPaint(hwnd,&ps);
          BitBlt(ps.hdc,0,0,SCREENWIDTH,SCREENHEIGHT,g_hMemDC,0,0, SRCCOPY);
        EndPaint(hwnd,&ps);
        break;
      }
      
      case WM_DESTROY:
        /*restore memory dc to original state
        and free up gdi resources*/
        RestoreDC(g_hMemDC,g_nDCdata);
        DeleteDC(g_hMemDC);
        DeleteObject(g_hDeskBmp);
        PostQuitMessage(0);
        break;
      default:
        return DefWindowProc(hwnd,msg,wParam,lParam);
      }
      return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
               LPSTR lpCmdLine, int nCmdShow)
    {
      HWND       hWindow;
      MSG        msg;
      WNDCLASSEX wc;
      
      /*registering the window class*/
      wc.cbSize    = sizeof(WNDCLASSEX);
      wc.style    = 0;
      wc.lpfnWndProc  = WndProc;
      wc.cbClsExtra  = 0;
      wc.cbWndExtra  = 0;
      wc.hInstance  = hInstance;
      wc.hIcon    = NULL;
      wc.hCursor    = LoadCursor(NULL,IDC_ARROW);
      wc.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
      wc.lpszMenuName  = NULL;
      wc.lpszClassName= "windowclass";
      wc.hIconSm    = NULL;
    
      /*make sure window class has registered*/
      if(!RegisterClassEx(&wc))
      {
        MessageBox(NULL,"Window Registration Failed","Error!",
          MB_ICONEXCLAMATION | MB_OK);
        return 0;
      }
    
      /*create screensaver window*/
      hWindow=CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"screensaver",
        WS_OVERLAPPEDWINDOW,0,0,
        SCREENWIDTH,SCREENHEIGHT,
        NULL,NULL,hInstance,NULL);
    
      ShowWindow(hWindow,nCmdShow);
      UpdateWindow(hWindow);
    
      if(hWindow == NULL)
      {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
          MB_ICONEXCLAMATION | MB_OK);
        return 0;
      }
      
      /*main message loop*/
      while(GetMessage(&msg,NULL,0,0)>0)
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int)msg.wParam;
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I would suggest looking up the DIB library and making use of it. It has several very very useful functions. You can capture the entire screen or parts of a screen and draw bitmaps to windows, and save bitmaps, etc.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    thanks guys. Ken, your code works great. I have one more question, which is how can I make the window so that it doesnt have any borders, especially the one at the toP?

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>how can I make the window so that it doesnt have any borders, especially the one at the toP?<<

    Set the window style in your call to CreateWindowEx to WS_POPUP. Either set the extended style parameter of CreateWindowEx to zero or, for something like a screensaver, WS_EX_TOPMOST may be a good idea.

    For more information see extended window styles and window styles.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX screenshot via C (app crashes)
    By Delusionz in forum C Programming
    Replies: 6
    Last Post: 01-11-2009, 08:55 AM
  2. taking screenshot
    By qwertylol2 in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2007, 12:26 AM
  3. Taking Screenshot - Full Screen Dos
    By loko in forum C Programming
    Replies: 12
    Last Post: 07-16-2005, 01:23 AM
  4. screenshot
    By Draco in forum Windows Programming
    Replies: 2
    Last Post: 04-25-2004, 09:46 AM
  5. Taking a screenshot in DirectDraw
    By Stan100 in forum Game Programming
    Replies: 3
    Last Post: 11-13-2003, 10:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21