#include "stdafx.h" #include "resource.h" #define initguid # include
#define safage (x) f (x) {x-> release (); x = null;
// function declaration BOOL InitWindow (HINSTANCE hInstance, int nCmdShow); LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void DrawImage (); LPCTSTR PicName = "E: / CJD / material / pic / Warcraft /1.BMP ";
// Variable Description HWND HWND; // Window Handle
/ * ================================================================================================================================================================ ============================= LPDIRECTDRAW7 and LPDIRECTDRAWSURFACE7 type (7 is the version number) is predefined in ddraw.h header files Point to the long pointer of IDirectDraw7 and iDirectdrawSurface7 (previously added LP represents long point), from the later "->" instead of "." It can also be seen.
DD is the abbreviation of DirectDraw, and DDS is the abbreviation of DirectDrawSurface, so habits we are using the variables from LPDD and LPDDSxxx.
Although VC.NET comes with DirectX SDK is version 8.1, because Microsoft stops from DirectX 8.0 to update DirectDraw, DirectDRAW currently has 7.0. ============================================================================================================================================================================================================= ============================== * /
LPDIRECTDRAW7 lpDD = NULL; // pointer LPDIRECTDRAWSURFACE7 lpDDSPrimary DirectDraw objects = NULL; // pointer LPDIRECTDRAWCLIPPER lpClip DirectDraw main page = NULL; // DirectDraw objects cut LPDIRECTDRAWSURFACE7 lpBmp = NULL; // pointer stored background page LPDIRECTDRAWSURFACE7 bitmap_surface ( ???? LPCTSTR file_name) {HDC hdc; HBITMAP bit; LPDIRECTDRAWSURFACE7 surf;? // Loading BMP bit = (HBITMAP) LoadImage (NULL, file_name, IMAGE_BITMAP, 0,0, ?? LR_DEFAULTSIZE | LR_LOADFROMFILE) ;? if ( ! bit) // Load failed, return null ?? return null;? // Get BMP size? Bitmap bitmap;? getObject (bitmap), & bitmap);? // Save BMP size? int Surf_Width = bitmap .BMWIDTH; INT SURF_HEIGHT = Bitmap.bmheight;
? // Create a page? HRESULT DDRVAL;? DDSURFACEDESC2 DDSD;? // Empty DDSurfaceDesc2 structure? ZeromeMory (& DDSD, SIZEOF (DDSD)); DDSD.dwsize = sizeof (ddsurfaceDesc2);? // Set the padding mark? DDSD.DWFLAGS = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;?????? // offscreen page ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; // specify the width and height ddsd.dwWidth = 1024; ddsd.dwHeight = 768; // create offscreen Page? DDRVAL = LPDD-> CreateSurface (& DDSD, & Surf, null) ;? if (ddrval! = Dd_ok) {?? // failed, release BMP ?? deleteObject (bit); ?? Return NULL;?}? Else // Create a success? {?? // get DC ?? Surf-> getDC (& HDC); ?? // Get consistent with DC ?? HDC bit_dc = createcompatibleDC (HDC); ?? // ?? SELECTOBJECT (Bit_DC, Bit); ?? // Transfer BMP ?? Bitblt (HDC, 0, 0, Surf_Width, Surf_Height, Bit_DC, 0, 0, Srccopy); ?? // Release DC ?? Surf-> ReleaseDC (HDC); ?? DELETEDC (Bit_DC);?}? // Release BMP? DeleteObject (bit);? // Return the off-screen page pointer? Return Surf;}
// ******************************************************** *********** // Function: initddraw () // Function: DX initialization function // ********************** ************************************************************************************** BOOL INITDDRAW () {? DDSurfaceDesc2 DDSD; // DirectDraw Page Description? HRESULT HRET;? / * ??? Before all work, remember to join the two lib files ??? In the Object / Library modules column of the LINK page under menu Project-Setting? "DDRAW. Lib "and" DXGUID.LIB ", the latter may not join, and use #define initguid instead ??? PS: It is best to choose Setting for All configurations ??? This will be effective under debug and release? * /? / * ??? If you want to use DirectDraw, you must create a DirectDraw object that is the core of the DirectDraw interface. ??? Use the DirectDrawCreateex () function to create a DirectDraw object, which is defined in DDRAW.H, which is as follows:
?????? HRESULT WINAPI DirectDrawCreateex (?????? Guid Far * lpguid, // Pointer to the GUID of the DirectDRAW interface, NULL indicates the default (current) ?????? lpvoid * LPLPDD, ?? // Used to accept the address of the initial DirectDraw object? ?????? REFIID IID, ?????? // IID_IDIRECTDRAW7, current version ?????? IUNKNOWN FAR * PUNKOUTER? // Null ? Retain ???); ?????? All DirectDRAW functions are the HRESULT type, which is a 32-bit value. ??? function call successfully expressed "DD_OK", all the error value flags are "DDERR", such as: ??? DDERR_DIRECTDRAWALREADYCREATED ??? DDERR_OUTOFMEMORY? * /? // Use IF here (XXX! = DD_OK) Method for error detection, this is the most common method ???? IF (DirectDrawCreateex (NULL, (LPVOID *) & LPDD, IID_IDIRECTDRAW7, NULL)! = DD_OK) ?? Return false; // Create a DirectDRAW object? After the DirectDrawCreate function calls successfully, the LPDD has point to a DirectDraw object. It is the highest level leader of the entire DirectDRAW interface, and the following steps are under its control. • We use idirectdraw7 :: setCooperativeEvel () to set the DirectDraw program to the system's control level. The prototype is as follows:? HRESULT SETCOOPERATIVELEVEL (HWND HWND, DWORD DWFLAGS)? The first parameter is a window handle, we give it hwnd, allow the DirectDRAW object to contact the main window. ???? The second parameter is the control level flag. Here is DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN, indicating that we expect DirectDraw to work in exclusive and full-screen. ????? The control level describes how DirectDRAW is working with the display device and system. • DirectDraw control level is generally used to determine that the application is running in full screen mode (must be used simultaneously with exclusive mode), or runs in window mode. However, the control level of DirectDraw can also set the following two: ???? (1) allows Press Ctrl Alt DEL to restart (only for exclusive mode, DDSCL_AllowReboot). ??? (2) Does not allow minimization or restore of the DirectDraw application (DDSCL_NOWIDOWCHANGES). ????? Ordinary control level (DDSCL_NORMAL) indicates that our DirectDraw application will run in the form of a window. Under this control level, we will not change the display resolution, or make a change operation (this is an important operation). Other than this, we cannot call functions that will have a fierce reaction to the memory, such as LOCK (). ???? When the application is a full screen and the exclusive control level, we can fully utilize hardware resources. • At this point, other applications can still create a page, using DirectDraw or GDI functions, just unable to change the display mode.
? * / ???? if (lpdd-> setcooperativeelevel (hwnd, ddscl_normal)! = DD_OK) ??????? Return false; // set DirectDraw control level? / *? Next We use iDirectdraw7 :: setDisplayMode () To set the display mode, its original shape is:? HRESULT SETDISPLAYMODE (? DWORD DWWIDTH, DWORD DWHEIGHT, / / DWWIDTH AND DWHEIGHT to set the width and height of the display mode.? DWORD DWBPP, // DWBPP Used to set the display mode The color digit number.? DWORD dwrefreshrate, // dwrefreshrate Set the refresh rate of the screen, 0 is the use of the default value.? DWORD dwflags // dwflags Now the only valid value is DDSDM_STANDARDVGAMODE?) ;? * / ???? f (LPDD -> setDisplayMode (1024, 768, 32, 0, ddsdm_standardvgamode)! = Dd_ok) ??????? Return false; // Set display mode? / *? HRESULT CREATECLIPPER (? DWORD dwflags, // Now no, must Set to 0? LPDIRECTDRAWCLIPPER FAR * LPLPDDDRAWCLIPPER FAR * LPLPDDCLIPPER, / / Pointer to the tailor object? IUNKNOWN FAR * PUNKOUTER / / / / I don't have to set it to null?);? Return DD_OK? * /? // created when successful (Must be performed before setting the DirectDraw control level)? HRET = LPDD-> CreateClipper (NULL, & LPCLIP, NULL) ;? IF (hret! = Dd_ok) {?? return false ;?}? LPCLIP -> sethwnd (0, hwnd);
??? / * ?? Before creating a page, first you need to fill a DDSurfaceDesc2 structure, which is the abbreviation of DirectDraw Surface Description, meaning the page description of DirectDraw. ?? It is very large, it can only make a simplest introduction. What should you pay attention to, you must empty it before filling this structure! ? * /
? // Start creating the home page, first empty the page description? ZeromeMory (& DDSD, SIZEOF (DDSD));
? // Plip page description ??? DDSD.dwsize = sizeof (ddsd); // DDSD.DWFLAGS = DDSD_CAPS = DDSCAPS_PRIMARYSURFACE; / / Main page ???????? • The first parameter of the createSurface () function is the address of the DDSurfaceDesc2 structure of the page information, which is & DDSD ;? The second parameter is the address of the home page pointer, here is & lpddsprimary; The third parameter must now be NULL, reserved for this function. ??? If the function call is successful, LPDDSPrimary will become a legitimate homepage object. • Since the working mode of the program has been set in front, it is the entire display screen, which is actually our entire display screen. • The graphic drawn on the main page will immediately reflect on our display screen. ? * / ???? if (LPDD, & LPDDSPRIMARY, NULL)! = DD_OK) ?????? Return false; // Create the main page ??? LPDDSPRIMARY-> setClipper (LPCLIP); / / Settings Cut? // Load picture? Lpbmp = bitmap_surface (picname);? // Draw ??? DrawImage (); rue True;} VOID CLEANUP (VOID) {? // Release interface? SafeRelease (lpbmp); ? SAFERELEASE (LPDDSPRIMARY); / / Delete the main page? SafeRelease (LPCLIP); // Delete the cut? SafeRelease (LPDD); // Delete DDRAW object? // ps: The order is just in the order}
Void DrawImage () {? if (lpbmp == null)? {?? return;?}? // full screen picture? LPDDSPRIMARY-> BLT (NULL, LPBMP, NULL, DDBLT_WAIT, NULL);}
// ******************************************************** ************ // Function: WinMain () // Function: Windows program portfolio function. Create a main window, process message loop // ************************************************* ****************** INT PASCAL WINMAIN (Hinstance Hinstance, Hinstance HPREVINSTANCE, LPSTR LPCMDLINE, INT NCMDSHOW) {? if (! initwindow (hinstance, ncmdshow) Return False; // Create a main window? // Return False while creating unsuccessful, return the program? MSG msg ;? // Enter the message loop:? For (;;)? {? I (peekmessage (& msg, null, 0, 0 , PM_REMOVE))? {?? if (msg.Message == wm_quit) Break; ?? TranslateMessage (& msg); ?? DISPATCHMESSAGE (& MSG) ;?}?} ?? Return msg.wpaham;
// ******************************************************** *********** // Function: initwindow () // Function: Create window // ********************** **********************************************
Static Bool INITWINDOW (Hinstance Hinstance, Int Ncmdshow) {? // Define Window Style: • WNDCLASS WC;? wc.style = CS_VREDRAW | CS_HREDRAW; / / Form Class Class WinPfnWndProc = (WinProc) WinProc; // The pointer of the window message processing function? Wc.cbclsextra = 0; // Assign an additional byte number after the window structure? Wc.cbwndextra = 0; // All byte number of additional bytes after assigning the window instance? Wc.hinstance = Hinstance; // The handle of the application corresponding to the window wc.hicon = null; // window icon? wc.hcursor = loadcursor (null, idc_arrow); // Window mouse? wc.hbrbackground = Createsolidbrush (RGB (RGB 0, 0, 0)); // Black background? Wc.lpszMenuname = makeintResource (idR_Menu); // Window menu resource name? Wc.lpszclassname = "my_test"; // Name of the window class? RegisterClass (& WC) ; // Register the window ??? // Create a window according to the parameters ???? hWnd = createwindow ("my_test", // Creating the name of the window class used by the window? "24 hours learning DX_ 2 hours ", // window title ?? WS_POPUP | WS_CAPTION, // window style, defined as the x, y coordinate of the normal type ?? 0, 0, // window position, the width, height? ? Null, // parent window handle ?? null, // menu handle ?? hinstance, // application handle ?? NULL) ;? IF (! HWnd) Return False; showwindow (hwnd, ncmdshow); // Display Window? UpdateWindow (hwnd); // Refresh window? InitdDraw (); // Initialization DX? Return True;}
// ******************************************************** *********** // Function: WinProc () // Function: Process Window Message // ********************** ***********************************************