VC + + based on DX implementation of Screenshot Program sample code _c language

Source: Internet
Author: User
Tags clear screen

This article describes the program example for VC + + image effect of the screenshot example, need DirectX 3.0 version, the code of the Getscreen function is the key to this screenshot program. Run this program to end with ESC key. Ddutil.h and Ddutil.cpp files are required in your code, please download them yourself. With regard to the Initddraw () function, the function is to initialize the DirectDraw environment, create a page-changing chain (home page, a back buffer), and create a timer.

The specific functional code is as follows:

#include <windows.h> #include <windowsx.h> #include <stdio.h> #include <ddraw.h> #include < math.h> #include "ddutil.h" #define TITLE "truncate diagram example"//window title #define CLASSNAME "Getscreen"//window class name #define COLORS Hinstanc E hinst; Application instance handle HWND Hwndmain;      Main Window handle Lpdirectdraw lpdd;  DirectDraw Object Lpdirectdrawsurface lpddsprimary;   Main Page Lpdirectdrawsurface Lpddsback;   Background buffer lpdirectdrawsurface LpDDSPic1;    Off-screen page 1 Lpdirectdrawpalette lpddpal;    Palette BOOL bactive;
Is the application active?
Hbitmap hbm;
RECT RECT;
function declares void freeobjects (void);
BOOL initddraw (void);
BOOL initsurfaces (void);
void Updateframe (void);
void Makerect (RECT *rect, long left, long up, long right, long bottom); Screenshot function, is the key//function Name: Getscreen//parameter: Pointer to screen rectangle//return value: Bitmap device///////
 HANDLE Getscreen (lprect lprect) {//define the screen DC and memory DC HDC HSCRDC,HMEMDC; Define Bitmap device HANDLE Hbitmap, Holdbitmap;
 Defines the screen coordinate variable unsigned int nx,ny,nx2,ny2;
 unsigned int nwidth,nheight;
 Defines the screen resolution variable unsigned int XSCRN,YSCRN;
 Make sure that the screen rectangle is not empty if (Isrectempty (lprect)) return null;
 Create a screen of DC Hscrdc=createdc ("DISPLAY", null,null,null);
 Created Memory DC Hmemdc=createcompatibledc (HSCRDC);
 Pay the screen rectangle coordinates to the coordinate variable nx=lprect->left;
 Ny=lprect->top;
 Nx2=lprect->right;
 Ny2=lprect->bottom;
 Obtain screen resolution xscrn=getdevicecaps (hscrdc,horzres);
 Yscrn=getdevicecaps (Hscrdc,vertres);
 if (nx<0) nx=0;
 if (ny<0) ny=0;
 if (NX2&GT;XSCRN) Nx2=xscrn;
 if (NY2&GT;YSCRN) Ny2=yscrn;
 Get screen width and length nwidth=nx2-nx;
 Nheight=ny2-ny;
 Obtain a screen image and pay a bitmap device Hbitmap=createcompatiblebitmap (hscrdc,nwidth,nheight);
 Holdbitmap= (HBITMAP) SelectObject (HMEMDC,HBITMAP);
 BitBlt (hmemdc,0,0,nwidth,nheight,hscrdc,nx,ny,srccopy);
 Hbitmap= (HBITMAP) SelectObject (HMEMDC,HOLDBITMAP);
 Removal Equipment DeleteDC (HSCRDC);
 DeleteDC (HMEMDC);
return hbitmap; }//*******************************************************************//function: FreEobject//function: Free all DirectDraw objects//******************************************************************* void

  Freeobjects (void) {//Free HBM Bitmap Object DeleteObject (HBM);
      if (LPDD!= null)/Free DirectDraw Object {if (lpddsprimary!= null)//Free master face {lpddsprimary->release ();
    Lpddsprimary = NULL;
      } if (LpDDSPic1!= NULL)/Free off screen page 1 {lpddspic1->release ();
    LpDDSPic1 = NULL;
      } if (Lpddpal!= NULL)//Free palette {lpddpal->release ();
    Lpddpal = NULL;
    } lpdd->release ();
  LPDD = NULL; }//*******************************************************************//function: Restoreall//function: Restore page memory after page loss//*****
 HRESULT Restoreall (void) {HRESULT ddrval;
 Restore the main page, which will also restore all pages in the page ddrval = Lpddsprimary->restore ();
 Restore off-screen page ddrval = Lpddspic1->restore ();
  Redraw the page image initsurfaces ();
return ddrval; }
//********************************************************Functions: WindowProc//function: The message processing process of the main window//******************************************************************* 
  LRESULT CALLBACK WinProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {switch (message) {
 Case Wm_setcursor:setcursor (LoadCursor (NULL, Idc_arrow));
  return TRUE;
    Case wm_activateapp://Application Activation message bactive = WParam;
  Break
      Case wm_keydown://Keystroke message switch (wParam) {case vk_escape:postmessage (hWnd, wm_close, 0, 0);
 Break
  } break;
    Case wm_destroy://Destroy window message freeobjects ();
    PostQuitMessage (0);
  Break
//Call the default process process return DefWindowProc (hWnd, message, WParam, LParam);
//******************************************************************//function: Initwindow ()//function: Create main window. BOOL Initwindow (hinstance hinstance, int
  nCmdShow) {WNDCLASS WC;//window class structure//Fill window class structure Wc.style = 0;
  Wc.lpfnwndproc = WinProc;
Wc.cbclsextra = 0;  Wc.cbwndextra = 0;
  Wc.hinstance = hinstance;
  Wc.hicon = LoadIcon (hinstance, idi_application);
  Wc.hcursor = LoadCursor (NULL, Idc_arrow);
 Wc.hbrbackground = (hbrush) getstockobject (Black_brush);//select Black Brush as window background wc.lpszmenuname = NULL;
 Wc.lpszclassname = CLASSNAME;
 Registration window class registerclass (&AMP;WC);
 Create main window hwndmain= CreateWindowEx (0, CLASSNAME,//window class name, must be consistent with above Wc.lpszclassname title,//Window header name ws_popup, 0, 0,
  GetSystemMetrics (Sm_cxscreen), GetSystemMetrics (Sm_cyscreen), NULL, NULL, HINSTANCE, NULL);
 if (!hwndmain) return FALSE;
 Display and Update window ShowWindow (Hwndmain, ncmdshow);
return TRUE; //******************************************************************//function: Initddraw ()//function: Initialize DirectDraw environment,
Create a page-break chain (Home page, a background buffer)//and create a timer.
  BOOL Initddraw (void) {Ddsurfacedesc ddsd;
  Ddscaps Ddscaps;
  HRESULT Ddrval; Create DirectDraw Object ddrval = Directdrawcreate (NULL,; lpdd, NULL);
  if (ddrval!= DD_OK) return FALSE; Get Full-screen Exclusive Mode ddrval = Lpdd->setcooperativelevel (Hwndmain, ddscl_exclusive |
  Ddscl_fullscreen);
  if (ddrval!= DD_OK) return FALSE; Set the display mode to the current screen resolution, 16-bit enhanced display mode Ddrval = Lpdd->setdisplaymode (GetSystemMetrics (Sm_cxscreen), GetSystemMetrics (SM
  _cyscreen), COLORS);
  if (ddrval!= DD_OK) return FALSE;
  Fill page chain structure ddsd.dwsize = sizeof (DDSD); Ddsd.dwflags = Ddsd_caps |
  Ddsd_backbuffercount;
             Ddsd.ddsCaps.dwCaps = Ddscaps_primarysurface |
             Ddscaps_flip |
 Ddscaps_complex;
 Background buffer quantity is 2 Ddsd.dwbackbuffercount = 2;
  Create a page-break chain that includes the main page and its back buffer ddrval = Lpdd->createsurface (&AMP;DDSD, &lpddsprimary, NULL);
  if (ddrval!= DD_OK) return FALSE;
  Gets the page pointer to the back buffer ddscaps.dwcaps = Ddscaps_backbuffer;
  Ddrval = Lpddsprimary->getattachedsurface (&ddscaps, &lpddsback);
 if (ddrval!= DD_OK) return FALSE;
  Create an off-screen page zeromemory (&AMP;DDSD, sizeof (DDSD)); Ddsd.dwsize = sizeof (DDSD); Ddsd.dwflags = Ddsd_caps | Ddsd_height |
  Ddsd_width;
  Ddsd.ddsCaps.dwCaps = Ddscaps_offscreenplain;
  Ddsd.dwwidth =getsystemmetrics (Sm_cxscreen);
  Ddsd.dwheight = GetSystemMetrics (Sm_cyscreen);
 if (Lpdd->createsurface (&AMP;DDSD, &lpddspic1, NULL)!= DD_OK) return FALSE; Call page initialization function if (!
  Initsurfaces ()) return FALSE;
return TRUE; //******************************************************************//function: WinMain ()//function: Application portal//************** int PASCAL WinMain (hinstance hinstance, hinstance hprevinstance
 , LPSTR lpcmdline, int ncmdshow) {msg msg;
 Hinst=hinstance;
 Obtain screen resolution Rect.left=rect.top=0;
 Rect.right=getsystemmetrics (Sm_cxscreen);
 Rect.bottom=getsystemmetrics (Sm_cyscreen);
 Call the screenshot function hbm= (HBITMAP) Getscreen (&rect); Initializes the main window if (!
 Initwindow (HINSTANCE, nCmdShow)) return FALSE; Initialize the DirectDraw environment if (! Initddraw ()) {MessageBox (Hwndmain, "error occurred during initialization of DirectDraw!") "," Error ", MB_OK);
 Freeobjects ();
 DestroyWindow (Hwndmain);
 return FALSE; //Enter the message loop while (1) {if (PeekMessage (&msg, NULL, 0, 0, pm_noremove)) {if (!
  GetMessage (&msg, NULL, 0, 0)) return msg.wparam; 
  TranslateMessage (&AMP;MSG);
 DispatchMessage (&AMP;MSG);
 else if (bactive) {updateframe ();
 else WaitMessage ();
return msg.wparam; //******************************************************************//function: Initsurfaces ()//function: Initialize page image//********
 BOOL initsurfaces (void) {if (hbm = NULL) return FALSE;

  Ddcopybitmap (LpDDSPic1, hbm, 0, 0, GetSystemMetrics (sm_cxscreen), GetSystemMetrics (Sm_cyscreen));
return TRUE;
 }//Update screen void Updateframe (void) {HRESULT ddrval;
 Clear screen background buffer ddbltfx ddbltfx;
 ddbltfx.dwsize = sizeof (DDBLTFX);
 Ddbltfx.dwfillcolor = Ddcolormatch (Lpddsback, RGB (0,0,0)); LPDDSBACK-&GT;BLT (NULL, NULL, NULL, DDBLT_WAIT |
 Ddblt_colorfill, &AMP;DDBLTFX);
 Call the implementation function of grayscale RECT srect, Drect; MAkerect (&srect, 0, 0, rect.right, rect.bottom);
 Makerect (&drect, 0, 0, rect.right, rect.bottom);
  Blit the background image to the back buffer Lpddsback->blt (&drect, LpDDSPic1, &srect, ddblt_wait, NULL); Page-Change while (1) {ddrval = Lpddsprimary->flip (NULL, ddflip_wait);//Call the page-change function if (Ddrval = = DD_OK)//Success exits WH
    Ile cycle break;
    else if (Ddrval = = dderr_surfacelost)//If the page is lost, restore the page, and then continue while loop Restoreall ();
 else break;
 } void Makerect (RECT *rect, long left, long up, long right, long bottom) {rect->left=left;
 rect->top=top;
 rect->right=right;
rect->bottom=bottom; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.