UpdateOverlay GetDc

/*******************************************************************
//  Copyright (c) Tom Yutian Tang 2007-2008 huarong_china@sohu.com
// 
*****************************************************************
**/
http://www.hackchina.com/r/151193/overlay.cpp__html

#define NAME "Ove
rlay"
#define TITLE "DirectDraw Over
lays"

#define WIN32_LEAN_AN
D_MEAN
#include <st
dio.h>
#include <wind
ows.h>
#include <windo
wsx.h>
#include <dd
raw.h>

#include "reso
urce.h"

#include "OverlayMa
nager.h"

OverlayManager gOverlayManager;

LPDIRECTDRAW            lpDD;               // DirectDraw object.
LPDIRECTDRAWSURFACE     lpDDS
Primary;       // DirectDraw primary surface.
LPDIRECTDRAWSURFACE
     lpDDSOverlay;      // DirectDraw overlay surface.
BOOL                    bVisible = FALSE;   // Overlay on or off.
RECT                    rSrc, rDest;        // Overlay rectangles.
DDCAPS                 ddcaps;             // Device caps.

DWORD AlignDown( DWORD dwNumber, DWORD dwAlignment )
{
    DWORD   dwResult;
   
    if ( dwNumber == 1 ) return dwNumber;

    dwResult = dwNumber - ( dwNumber % dwAlignment );

    return dwResult;
}

DWORD AlignUp( DWORD dwNumber, DWORD dwAlignment )
{
    DWORD   dwResult;

    if ( dwNumber == 1 ) return dwNumber;

    dwResult = dwNumber + ( dwAlignment - 1 );

    dwResult -= dwNumber % dwAlignment;

    return dwResult;
}

BOOL LoadImage( LPDIRECTDRAWSURFACE lpDDS, LPSTR szImage )
{
    HBITMAP         hbm;
   HDC             hdcImage = NULL;
    HDC             hdcSurf  = NULL;
    BOOL 
         bReturn  = FALSE;
    DDSURFACEDESC   ddsd;

    ZeroMemory( &ddsd, sizeof( ddsd ) );
    ddsd.dwSize = sizeof( ddsd );

    if ( FAILED( lpDDS->Get
SurfaceDesc( &ddsd ) ) )
{
        goto E
xit;
    }

    // If the pixel format isn't some flavor of RGB, we can't handle it.
    if ( ( ddsd.ddpfPixelFormat.dwFlags != DDPF_RGB ) ||
         ( ddsd.ddpfPixelFormat.dwRGBBitCount < 16 ) )

 
  {
        OutputDebugString( "Non-palettized RGB mode required./n" );
        goto Exit;       
    }

    // Try loading the image.
    hbm = ( HBITMAP )Loa
dImage( NULL, szImage,
            IMAGE_BITMAP, ddsd.dwWidth,
            ddsd.dwHe
ight, LR_LOADFROMFILE | LR_CREATEDIBSECTION );

    if ( hbm == NULL )
{
       OutputDebugString( " Couldn't find the resource./n" );
       goto Exit;
    }

    // Create a DC and select the image into it.
   hdcImage = CreateCompatibleDC( NULL );
   SelectObject( hdcImage, hbm );
  
    // Get a DC for the surface.
    if ( FAILED( lpDDS->GetDC( &hdcSurf ) ) )
{
        OutputDebugString( "Couldn't get a DC./n" );
        goto Exit;
    }
   
    // The BitBlt will perform format conversion as necessary.
    if ( BitBlt( hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight,
         hdcImage, 0, 0, SRCCOPY ) == FALSE )
{
        OutputDebugString( "Blt failed./n" );
        goto Exit;
    }

    // Success.
    bReturn = TRUE;
   
Exit:
    // Clean up
everything.
    if ( hdcSurf )
        lpDDS->ReleaseDC( hdcSurf );
    if ( hdcImage )
        DeleteDC( hdcImage );
    if ( hbm )
      DeleteObject( hbm );

    return bReturn;
}

BOOL LoadImage_1( LPDIRECTDRAWSURFACE lpDDS, LPSTR szImage )
{
    HBITMAP         hbm;
    HDC             hdc
Image = NULL;
   HDC             hdcSurf = NULL;
    BOOL            bReturn  = FALSE;
    DDSURFACEDESC   ddsd;

    ZeroMemory( &ddsd, sizeof( ddsd ) );
    ddsd.dwSize = sizeof( ddsd );

    if ( FAILED( lpDDS->Get
SurfaceDesc( &ddsd ) ) )
{
        goto Exit;
    }

    // If the pixel format isn't some flavor of RGB, we can't handle it.
if (ddsd.ddpfPixelFormat.dwFlags==DDPF_FOURCC)
{
if (ddsd.ddpfPixelFormat.dwFourCC== MAKEFOURCC('U', 'Y', 'V', 'Y'))
{
int tyt=0;
}
}
    else if ( ( ddsd.ddpfPixelFormat.dwFlags != DDPF_RGB ) ||
         ( ddsd.ddpfPixelFormat.dwRGBBitCount < 1
6 ) )

    {
        OutputDebugString( "Non-palettized RGB
mode required./n" );
     goto Exit;       
    }

    // Try loading the image.
    hbm = ( HBITMAP )LoadImage( NULL, szImage,
           IMAGE_BITMAP,
0,
            0, LR_LOADFROMFILE | LR_CREATEDIBSECTION );

    if ( hbm == NULL )
{
        OutputDebugString( " Couldn't find the resource./n" );
        goto Exit;
    }

BITMAP Bitmap;  
GetObject(hbm, sizeof(Bitmap), (LPSTR)&Bitmap); 

    // Create a DC and select the image into it.
    hdcImage = CreateCompatibleDC( NULL );
    SelectObject( hdcImage, hbm );
  
    // Get a DC for the surface.
    if ( FAILED( lpDDS->GetDC( &hdcSurf ) ) )
{
        OutputDebugString( "Couldn't get a DC./n" );
        goto Exit;
  
}
   
    // The BitBlt will perform format conversion as necessary.
    //if ( BitBlt( hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight,
i
f ( BitBlt( hdcSurf, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight,
    
  hdcImage, 0, 0, SRCCOPY ) == FALSE )
{
        OutputDebugString( "Blt failed./n" );
       goto Exit;
    }

    // Success.
  
bReturn = TRUE;
   
Exit:
    // Clean up everything.
  if ( hdcSurf )
        lpDDS->ReleaseDC(
hdcSurf );
    if ( hdcImage )
       DeleteDC( hdcImage );
 
  if ( hbm )
        Delet
eObject( hbm );

    return bReturn;
}

void ReleaseObjects( void )
{
  if ( lpDD != NULL )
    {
      
if ( lpDDSPrimary != NULL )
        {
           if ( lpDDSOverlay != NULL )
         {
             
  lpDDSOverlay->Release();
                lpDDSOverlay = NULL;
        }
          lpDDSPrimary->Release();
           lpDDSPrimary = NULL;
        }
 
     lpDD->Release();
    lpDD = NULL;
    }
}

B
OOL Fail( HWND hwnd,  char *szMsg )
{
    ReleaseObjects();
   OutputDebugString( szMsg );
    EndDialog(
hwnd, FALSE );
    return FALSE;
}

DDPIXELFORMAT PixelFormatToDdPixelFormat(PixelFormat format)
{
const D
DPIXELFORMAT formats[] =
{
// PIXEL_FORMAT_32BPP
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0xFF0000, 0x0000FF00, 0x000000FF, 0},
// PIXEL_FORMAT_24BPP
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0xFF0000, 0x0000FF00, 0x000000FF, 0},
// PIXEL_FORMAT_16BPP_R5G6B5
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0xF800, 0x07E0, 0x001F, 0},
// PIXEL_FORMAT_16BPP_R5G5B5
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x7C00, 0x03e0, 0x001F, 0},
// PIXEL_FORMAT_UYVY
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_YUY2
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'), 0, 0, 0, 0, 0},

// PIXEL_FORMAT_IMC1
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('I', 'M', 'C', '1'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_IMC2
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('I', 'M', 'C', '2'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_IMC4
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('I', 'M', 'C', '4'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_YV12
{ sizeo
f(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'V', '1', '2'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_NV12
{ sizeof(DDP
IXELFORMAT), DDPF_FOURCC, MAKEFOURCC('N', 'V', '1', '2'), 0, 0, 0, 0, 0}

};


switch(format)
{
case PIXEL_FORMAT_32BPP:
return formats[0];

case PIXEL_FORMAT
_24BPP:
return formats[1];

case PIXEL_FORMAT_16BPP_R5G6B5:
return formats[2];

case PIXEL_FORMAT_16BPP_R5G5B5:
return formats[3];

case PIXEL_FORMAT_UYVY:
return formats[4];

case PIXEL_FORMAT_YUY2:
return fo
rmats[5];


case PIXEL_FORMAT_IMC1:
return formats[6];

case
PIXEL_FORMAT_IMC2:
return formats[7];

case PIX
EL_FORMAT_IMC4:
return formats[8];

case PIXEL_FORMAT_YV12:
return formats[9];

case PIXEL_FORMAT_NV12:
return formats[10];

}

DDPIXELFORMAT tmp={0
};

return tmp;
}

UINT AAA(LPVOID pParam)
{
HDC hdcRender;
if (
SUCCEEDED( lpDDSPrimary->GetDC( &hdcRender ) ) )
{
MessageBox(NULL,"OK!",NULL,MB_OK);
}

else
{
MessageBox(NULL,"OK!",NULL,MB_OK);
}
return 0;
}

LRESULT CALLBACK DialogProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{

    DDSURFACEDESC   ddsd_primary,ddsd_overlay;
    DDCOLORKEY      ddckey;     // For SetColorKey
.
   DWORD           dwFlags;    // For updating the overlay.

    switch ( message )
   {
        case WM_INITD
IALOG:

#if 0
gOverlayManager.Res
et(400,400);
//IDirectDrawSurface *lpDDSPrimary=;
gOverlayManager.primarySurface_->QueryInterface(IID_IDirectDrawSurface,(void **)&lpDDSPrimary);
//IDirectDrawSurface *lpDDSOverlay=gOverlayManager.overlaySurface_;
gOverlayManager.overlaySurface_->QueryInterface(IID_IDirectDrawSurface,(void **)&lpDDSOverlay);
#else
       
   // Create the DirectDraw object -- we just need an IDirectDraw
          // interface so we won't bother to query an IDirectDraw2.
            if ( FAILED( DirectDrawCreate( NULL, &lpDD, NULL ) ) )
{
                return Fail( hwnd, "Couldn't create DirectDraw object./n" );
           }

            // Set normal cooperative levl.
       
   if ( FAILED( lpDD->SetCooperativeLevel( hwnd, DDSCL_NORMAL ) ) )
{
                return Fail( hwnd, "Couldn't set cooperat
ive level./n" );
            }

         
  // Store the caps in our global and check for overlay cap bit.
            ddcaps.dwSize = sizeof( ddcaps );
            if ( FAILED( lpDD->GetCaps( &ddcaps, NULL ) ) )
{
           
    return Fail( hwnd, "Couldn't get caps./n" );
            }
            if ( !( ddcaps.dwCaps & DDCAPS_OVERLAY ) )
{
MessageBox(NULL,"No overlay capability",NULL,MB_OK);
                return Fail( hwnd, "No overlay capability./n" );
           }

            // Check to ma
ke sure that an overlay is available for use.
            if ( ddcaps.dwCurrVisibleOverlays ==
                 ddcaps.dwMaxVisibleOverlays )
{
                return Fail( hwnd, "No overlays available./n" );
            }

            // Create the primary surface.
           ddsd_primary.dwSize = sizeof( ddsd_primary );
            ddsd_primary.dwFlags = DDSD_CAPS;
            ddsd_primary.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

 
          if ( FAILED( lpDD->CreateSurface( &ddsd_primary, &lpDDSPrimary, NULL ) ) )
{
                return Fail( hwnd, "Couldn't create prim
ary surface./n" );
            }

//CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AAA,NULL,0,NULL);
//break;

if (lpDDSPrimary->Get
SurfaceDesc(&ddsd_primary)==DD_OK)
{
// ����ɫ��
/*
char tmp[512];
sprintf(tmp,"lpDDSPrimary, dwRGBBitCount=%d dwYUVBitCount=%d /r/n",

ddsd_primary.ddpfPixelFormat.dwRGBBitCount,ddsd_primary.ddpfPixelFormat.dwYUVBitCount);
MessageBox(NULL,tmp,NULL,MB_OK);*/
}


           
// Now we'll try to create an overlay surface. We have to guess
            // at the format, as DirectDraw doesn't provide us with a li
st of
 
          // possibilities. YUV formats are most common, but we'll try RGB
            // so we don't have to convert our image.

            ddsd_overlay.dwSize = sizeof( ddsd_overlay );
            ddsd_overlay.dwFlags = DDSD_CAPS;

            ddsd_overlay.ddsCa
ps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY ;//| DDSCAPS_FLIP;// | DDSCAPS_COMPLEX;

            ddsd_overlay.dwFlags= DDSD_CAPS |
DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;

            ddsd_overlay.dwHeight = 64;
            ddsd_overlay.dwWidth = 64;

memse
t(&ddsd_overlay.ddpfPixelFormat,0,sizeof(DDPIXELFORMAT));
ddsd_overlay.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);

#if 1 // Try to
create overlay
{
BOOL bOverlay_Created=FALSE;
PixelFormat pixelFormat_ = PIXEL_FORMAT_UNKNOWN;
for(int format = PIXEL_FO
RMAT_FIRST; format <= PIXEL_FORMAT_LAST; format++)
{
// Attempt to create the surface
with this pixel format
ddsd_overlay.ddpfPixelFormat = PixelFormatToDdPixelFormat(static_cast<PixelFormat>(format));
if ( lpDD->CreateSurface(&ddsd_overlay, &lpDDSOverlay, 0)==S_OK)
{
pixelFormat_ =static_cast<PixelFormat>(format);

char msg[1024];
switch (pixe
lFormat_)
{
case PIXEL_FORMAT_32BPP:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_32BPP created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_24BPP:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_24BPP created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_16BPP_R5G6B5:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_16BPP_R5G6B5 created sucessfully , use this overlay ?");
break;
case PI
XEL_FORMAT_16BPP_R5G5B5:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_16BPP_R5G5B5 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_UYVY:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_UYVY created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_YUY2:
sprintf(msg,"The overl
ay with pixel format PIXEL_FORMAT_YUY2 created sucessfully , use this overlay ?");
break;

case PIXEL_FORMAT_IMC1:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_IMC1 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_IMC2:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_IMC2 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_IMC4:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_IMC4 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_YV12:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_YV12 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_NV12:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_NV12 created sucessfully , use this overlay ?");
break;

default:
sprintf(msg,"The pixel format Unknown of overlay created sucessfully ???!!!");
break;

}

int res=MessageBox(NULL,msg,NULL,MB_YESNO);
if (res==IDYES)
{
bOverlay_Created=TR
UE;
break;
}

}
}
if (!bOverlay_Created)
{
MessageBox(NULL,"Failed to create overlay, exit !",NULL,MB_OK);
exit(1);
}

}
#else
ddsd_overlay.ddpfPixelFormat.dwFourCC = 0; //MAKEFOURCC('U', 'Y', 'V', 'Y')
ddsd_overlay.ddpfPixelFormat.dwRGBAlphaBitMask = 0;

// Try the current color depth firstly.
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfP
ixelFormat.dwRGBBitCount = 24;
            ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x000000ff;

// Try the first format.
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format DDPF_RGB current color depth failed./n" );

ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 16;
ddsd_overlay.ddpfPixelForma
t.dwRBitMask = 0x7C00;
ddsd_overlay.ddpfPix
elFormat.dwGBitMask = 0x03E0;
ddsd_overlay.ddpfPixelFormat.
dwBBitMask = 0x001F;
           
if ( FAILED( lpDD->
CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "O
verlay format RGB555 failed./n" );

// RGB565.
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 16;
ddsd_overlay.ddpfPixelFor
mat.dwRBitMask = 0xF800;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x07E0;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x001F;

if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format RGB565 failed./n" );

// RGB888
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 24;
ddsd_overlay.ddpfPix
elFormat.dwRBitMask = 0x00ff0000;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd_overlay.d
dpfPixelFormat.dwBBitMask = 0x000000ff;

if (
FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
// YUV -- 16
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_YUV;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 16;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x7C00;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x03E0;
ddsd_overlay.ddpfPixelFormat
.dwBBitMask = 0x001F;

if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format YUV -- 16 failed./n" );

// YUV -- 24
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_YUV;
ddsd_overlay.d
dpfPixelFormat.dwRGBBitCount = 24;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd_overlay.ddpfPixelFormat.dwBBitMas
k = 0x000000ff;

if ( FAILED( lpDD->CreateSurface
( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
MessageBox(NULL,"Could not create overlay surface in current display mode !",NULL,MB_OK);
return Fail( hwnd, "Couldn't create overlay surface./n" );
}
else
{
MessageBox(NULL,"create overlay surface with YUV-24",NULL,MB_OK);
}
}
else
{
Messag
eBox(NULL,"create overlay surface with YUV-16",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with RGB
888",NULL,MB_OK);
}
}
else
{
Me
ssageBox(NULL,"create overlay surface with RGB565",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with RGB555",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"creat
e overlay surface with DDPF_RGB current color depth !",NULL,MB_OK);
}
#endif // Try to create overlay

#endif

            // If source color key is a
vailable for overlays, set a
            // color key of p
ure green and enable the check box. Use
            // the g
reen bit mask that was successfully created.

            // L
oad a bitmap into our overlay.
            // Our bitma
p is 64 pixels wide, which will satisfy
 
          // the common alignment requirements of 4 and 8 pixels.

            LoadImage_1( lpDDSOverlay, "xrules.bmp" );

// Load a
bitmap into primary surface.
LoadImage_1( lpDDSPrimary, "bluesq_1.bmp" );

// Set Color key of primary surf
ace
DDCOLORKEY ddckey;
ddckey.dwColorSpaceLow
Value = ddsd_primary.ddpfPixelFormat.dwBBitMask; // ��ɫ
ddckey.dwColorSpaceHighValue = ddsd_primary.ddpfPixelFormat.dwBBitMask;
if (lpDDSPrimary->SetColorKey(
//DDCKEY_
SRCOVERLAY,
DDCKEY_DESTOVERLAY,
&ddckey )==DD_OK)
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the primary surface successful !/r/n",NULL,MB_OK);
}
else
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the primary surface FAILED !/r/n",NULL,MB_OK);
}

// Set Color key of overlay surface
            if ( ddcaps.dwCKeyCaps & DDCKEYCAPS_SRCOVERLAY )
            {
                ddckey.dwColorSpaceLowValue = ddsd_overlay.ddpfPixelFormat.dwGBitMask;

                ddckey.dwColorSpaceHighValue = ddsd_overlay.ddpfPixelFormat.dwGBitMask;

      
        if (lpDDSOverlay->SetColorKey(
DDCKEY_SRCOVERLAY,
                                          &ddckey )==DD_OK)
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the overlay surface successful !/r/n",NULL,MB_OK);
}
else
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the overlay surface FAILED !/r/n",NULL,MB_OK);
}

               EnableWindow( GetDlgItem( hwnd, IDC_COLORKEY ),
                              
TRUE );
            }
else
{
MessageBox(NULL,"Create_DDraw_Surface(): Dont find DDCKEYCAPS_SRCOVERLAY support !/r/n",NULL,MB_OK);
}

/*
RECT re
ct_1;
rect_1.left=0; rect_1.top=0
; rect_1.right=64; rect_1.bottom=64;
lpDDSOverlay->Blt(&rect_1,lpDDSPrimary,&rect_1,DDBLT_WAIT,NULL);
LoadImage_1( lpDDSPrimary, "xrules.bmp" );*/

            // Prepare the overlay for display by setting the source
           // and destination rectangles. This is the tricky part.

            // We'll start with the upper left corners of both
            // source and destination. Using zeros is the easy
            // way out -- zero aligns with everything.
            rSrc.left = 0;
            rSrc.top = 0;
            rDest.left = 0;
            rDest.top = 0;

            // To show the entire sprite rectangle, we need a bottom
            // right coordinates of 64. Alignment only applies to the right
            // x coordinates if DDCAPS_ALIGNSIZESRC is set in DDCAPS.
            rSrc.bottom = 64;
            rSrc.right = 64;

            // A value of 64 will probably work, since it aligns with
            // both 4 and 8. If it doesn't, we have to go
            // smaller.
            if ( ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC )
{
                rSrc.right = AlignDown( rSrc.right, ddcaps.dwAlignSizeSrc );
            }

            // The destination rectangle must comply both with
            // alignment and scaling limitations. Again, it's the
            // right X coord that we're concerned with. If you
            // want the aspect ratio to remain correct, you should
            // adjust the bottom or Y coordinates as well.

            // Stretching is specified "times 1000" so that it can be
            // expressed as an integer value. A value of 1000 indicates
            // no stretching. We'd like 4x if we can get it. A value of 0
// for dwMaxOverlayStretch indicates no maximum limit.

            if ( ( ddcaps.dwMinOverlayStretch <= 4000 ) &&
                 ( ( ddcaps.dwMaxOverlayStretch >= 4000 ) ||
     ddcaps.dwMaxOverlayStretch == 0 ) )
            {
                // Add 999 to compensate for integer truncation.
                rDest.right = ( ( rSrc.right * 4000 +
                                    999 ) / 1000 );
            }
            else
            // Take the minimum allowable if we can't get 4000.
            {
                // Add 999 to compensate for integer truncation.
                rDest.right = ( ( rSrc.right * ddcaps.dwMinOverlayStretch +
                                    999 ) / 1000 );
if ( rDest.right == 0 ) rDest.right = rSrc.right;
            }

            // Align the result, if necessary. Align upward to avoid
            // messing up our minimum stretch adjustment.
            if ( ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST )
{
                rDest.right = AlignUp( rDest.right, ddcaps.dwAlignSizeDest );
            }

            // Our image is square, so adjust the bottom as well.
            rDest.bottom  = rDest.right;

rDest=rSrc;

            break;

        case WM_COMMAND:
           
            switch ( LOWORD( wParam ) )
            {
                case IDC_SHOW:
                    if ( bVisible )
                    {
                        // Hide the overlay.
                        if ( FAILED( lpDDSOverlay->UpdateOverlay( NULL,
                                lpDDSPrimary, NULL, DDOVER_HIDE, NULL ) ) )
{
                            return Fail( hwnd,
                                "Couldn't hide the overlay./n" );
                        }
                        bVisible = FALSE;
                        SetDlgItemText( hwnd, IDC_SHOW, "Show" );
                    }
                    else
                    {
                        // If the Use ColorKey check box is checked,
                        // include the appropriate flag.
                        if ( SendDlgItemMessage( hwnd, IDC_COLORKEY,
                             BM_GETCHECK, 0, 0 ) == BST_CHECKED )
                        {
                            dwFlags = DDOVER_SHOW
//| DDOVER_KEYSRC;
| DDOVER_KEYDEST;
                        }
                        else
                        {
                            dwFlags = DDOVER_SHOW;
                        }

                        // Show the overlay.
                        if ( FAILED( lpDDSOverlay->UpdateOverlay( &rSrc,
                                lpDDSPrimary, &rDest, dwFlags, NULL ) ) )
                        {
lpDDSOverlay->UpdateOverlayZOrder(DDOVERZ_SENDTOFRONT, 0);

char tmp[1024];
sprintf(tmp,"Couldn't show the overlay. rSrc(%d,%d,%d,%d) rDest(%d,%d,%d,%d) Err=%d /n",
rSrc.left,rSrc.top,rSrc.right,rSrc.bottom,
rDest.left,rDest.top,rDest.right,rDest.bottom,
GetLastError());
MessageBox(NULL,tmp,NULL,MB_OK);
                            //return Fail( hwnd,
                              //  "Couldn't show the overlay./n" );
                        }
else
{
bVisible = TRUE;
SetDlgItemText( hwnd, IDC_SHOW, "Hide" );
}
                    }
                    break;

                case IDCANCEL:
                    EndDialog( hwnd, FALSE );
                    return TRUE;
            }
            break;
           
            case WM_CLOSE:
                EndDialog( hwnd, TRUE );
                return TRUE;

                break;
    }
    return FALSE;
}

int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine, int nCmdShow )
{
    DialogBox( hInstance, MAKEINTRESOURCE( IDD_MAIN ),
               NULL, ( DLGPROC )DialogProc );

    return TRUE;
}
...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值