C++ 设置LayeredWindowAttributes使窗口透明只是工作时间的一部分

C++ 设置LayeredWindowAttributes使窗口透明只是工作时间的一部分,c++,winapi,directx,transparency,C++,Winapi,Directx,Transparency,我试图使窗口透明,以便只有部分内容可见,我尝试使用SetLayeredWindowAttributes来实现这一点,这使窗口透明如我所愿,但只有当部分windows图片在我桌面的可见区域之外时,它才起作用。出于某种原因,每当窗口完全在屏幕上时,它都会重新绘制其黑色背景(我使用的透明颜色是为了不被看到),这就是问题的一个视频示例。我不确定到底是什么原因导致这只是为了安全,我张贴了完整的代码 #define _WIN32_WINNT 0x501 #include "C:\Program Files\

我试图使窗口透明,以便只有部分内容可见,我尝试使用
SetLayeredWindowAttributes
来实现这一点,这使窗口透明如我所愿,但只有当部分windows图片在我桌面的可见区域之外时,它才起作用。出于某种原因,每当窗口完全在屏幕上时,它都会重新绘制其黑色背景(我使用的透明颜色是为了不被看到),这就是问题的一个视频示例。我不确定到底是什么原因导致这只是为了安全,我张贴了完整的代码

#define _WIN32_WINNT 0x501
#include "C:\Program Files\Microsoft DirectX SDK (August 2008)\Include\D3dx9core.h"
#include "C:\Documents and Settings\Death\My Documents\Downloads\DXSprite\DXSprite\resource.h"
#include <windows.h>
#include <string>
#include <stdio.h>
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND                        g_hWnd                  = NULL;
LPDIRECT3D9                 g_pD3D                  = NULL;
LPDIRECT3DDEVICE9           g_pD3DDevice            = NULL;
ID3DXSprite *               g_pD3DXSprite           = NULL;
LPDIRECT3DTEXTURE9          g_pTexture              = NULL;
const int                   SCREEN_WIDTH            = 800;
const int                   SCREEN_HEIGHT           = 600;
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HRESULT InitializeD3D       ( );
void RenderFrame            ( );

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance,
                HINSTANCE hPrevInstance,
                LPSTR     lpCmdLine,
                int       nCmdShow )
{
WNDCLASSEX  winClass;
MSG         uMsg;
HRESULT     hr;

memset(&uMsg,0,sizeof(uMsg));

winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize        = sizeof(WNDCLASSEX);
winClass.style         = CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc   = WindowProc;
winClass.hInstance     = hInstance;
winClass.hIcon         = LoadIcon(hInstance, (LPCTSTR)IDC_DXSPRITE);
    winClass.hIconSm       = LoadIcon(hInstance, (LPCTSTR)IDC_DXSPRITE);
winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName  = NULL;
winClass.cbClsExtra    = 0;
winClass.cbWndExtra    = 0;

if( !RegisterClassEx(&winClass) )
    return E_FAIL;

g_hWnd = CreateWindowEx( WS_EX_LAYERED, "MY_WINDOWS_CLASS",
                         "Direct3D 9 - ID3DXSprite Example",
                         WS_OVERLAPPEDWINDOW | WS_VISIBLE ,
                         0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL );

if( g_hWnd == NULL )
    return E_FAIL;

SetLayeredWindowAttributes(g_hWnd, RGB(0x00,0x00,0x00), 0, LWA_COLORKEY});
ShowWindow( g_hWnd, nCmdShow );
//----------------------------------------------------------------
// Create the DirectX device
//----------------------------------------------------------------
if (FAILED( InitializeD3D()))
    return 0;


//----------------------------------------------------------------
// CREATE THE ID3DXSprite
//----------------------------------------------------------------

// Create the ID3DXSprite interface object
hr = D3DXCreateSprite(g_pD3DDevice, &g_pD3DXSprite );
if( FAILED(hr) )
    return hr;


//----------------------------------------------------------------
// LOAD THE TEXTURE FOR THE SPRITE
//----------------------------------------------------------------

// --------------------------------------------------------
// Load the texture.  I decided to use the extended
// version of the texture loading function just to show what
// it would look like.
// The texture was created with Photoshop with a transparent
// background to start with.  Then line cross hairs were added.
//
// Note - If you don't use a texture image that has a power of
// 2 size for the width or height then the image may not load
// properly.  This image is 256x256.
//
D3DXCreateTextureFromFileEx(
    g_pD3DDevice,
    "C:\\Documents and Settings\\Death\\My Documents\\45handold2.tga",              // Our texture image!
    D3DX_DEFAULT,               // width
    D3DX_DEFAULT,               // height
    D3DX_DEFAULT,               // MIP levels
    0,                          // usage
    D3DFMT_DXT1,                // texture format
    D3DPOOL_MANAGED,            // mem pool
    D3DX_DEFAULT,               // filter
    D3DX_DEFAULT,               // MIP filter
    0,                          // transparent color key
    NULL,                       // image info struct
    NULL,                       // palette
    &g_pTexture);               // the returned texture, if success

if ( FAILED(hr) )
    return hr;





// ---------
// Main Loop
// ---------
while( uMsg.message != WM_QUIT )
{
    if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
    {
        TranslateMessage( &uMsg );
        DispatchMessage( &uMsg );
    }
}

// -------------------------
// Release directx resources
// -------------------------
if (g_pD3DXSprite)
{
    g_pD3DXSprite->Release();
    g_pD3DXSprite = NULL;
}

if (g_pTexture)
{
    g_pTexture->Release();
    g_pTexture = NULL;
}

if (g_pD3DDevice)
{
    g_pD3DDevice->Release();
    g_pD3DDevice = NULL;
}




UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );
return (int)uMsg.wParam;
}

//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND   hWnd,
                         UINT   msg,
                         WPARAM wParam,
                         LPARAM lParam )
{

switch( msg )
{
    case WM_KEYDOWN:
    {
        switch( wParam )
        {
            case VK_ESCAPE:
                PostQuitMessage(0);
                break;

        }
    }
    break;

    case WM_CLOSE:
    {
        PostQuitMessage(0);
    }

    case WM_DESTROY:
    {
        PostQuitMessage(0);
    }
    break;

    default:
    {
        RenderFrame();
        return DefWindowProc( hWnd, msg, wParam, lParam );
    }
    break;
}

return 0;
}

//-----------------------------------------------------------------------------
// Name: InitializeD3D()
// Desc: Create DirectX interface objects
//       Initialize the view matrix.
//       Setup render states that will not need changing throughout
//       the life of the application.
//-----------------------------------------------------------------------------
HRESULT InitializeD3D( )
{
HRESULT hr;

// Create a direct 3D interface object
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

if( g_pD3D == NULL )
{
    // TO DO: Respond to failure of Direct3DCreate9
    return E_FAIL;
}

D3DDISPLAYMODE d3ddm;

if( FAILED( hr = g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
{
    // TO DO: Respond to failure of GetAdapterDisplayMode
    return hr;
}


//
if( FAILED( hr = g_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
                                            d3ddm.Format, D3DUSAGE_DEPTHSTENCIL,
                                            D3DRTYPE_SURFACE, D3DFMT_D16 ) ) )
{
    if( hr == D3DERR_NOTAVAILABLE )
        // POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!
        return hr;
}

//
// Do we support hardware vertex processing? If so, use it.
// If not, downgrade to software.
//

D3DCAPS9 d3dCaps;

if( FAILED( hr = g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT,
                                   D3DDEVTYPE_HAL, &d3dCaps ) ) )
{
    // TO DO: Respond to failure of GetDeviceCaps
    return hr;
}

DWORD dwBehaviorFlags = 0;

if( d3dCaps.VertexProcessingCaps != 0 )
    dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
    dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

//
// Everything checks out - create a simple, windowed device.
//

D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(d3dpp));

d3dpp.BackBufferFormat       = d3ddm.Format;
d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed               = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;

// Attempt to create a HAL device, end app on failure just to keep things
// simple.  In other words we are not trying to create a REF device if the
// HAL fails.
if( FAILED( hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
                                  dwBehaviorFlags, &d3dpp, &g_pD3DDevice ) ) )
{
//    char blah[100];
  //  snprintf (blah, 1000, "%d", hr);
    //MessageBox (NULL,blah,NULL,NULL);
}


// If we get here everything worked!
return S_OK;
}


//-----------------------------------------------------------------------------
// Name: RenderFrame()
// Desc: Draw the image to the framebuffer.
//-----------------------------------------------------------------------------
void RenderFrame( )
{
if (!g_pD3DDevice && !g_pD3DXSprite && !g_pTexture)
    return;


// Clear the frame & depth buffer ready for drawing (Black color)
g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,  0x00000000, 1.0f, 0     );

g_pD3DDevice->BeginScene();
{
    //-------------------------
    // Render the sprite
    //

    D3DXVECTOR3 vecPos = D3DXVECTOR3(0,0,0);

    if (g_pD3DXSprite && g_pTexture)
    {
        g_pD3DXSprite->Begin( D3DXSPRITE_ALPHABLEND );
        g_pD3DXSprite->Draw(g_pTexture, NULL, NULL, &vecPos, 0xffffffff);
        g_pD3DXSprite->End();
    }


}
g_pD3DDevice->EndScene();


// Frame buffer to Front buffer
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

}
#定义_WIN32_WINNT 0x501
#包括“C:\Program Files\Microsoft DirectX SDK(2008年8月)\include\D3dx9core.h”
#包括“C:\Documents and Settings\Death\My Documents\Downloads\DXSprite\DXSprite\resource.h”
#包括
#包括
#包括
//-----------------------------------------------------------------------------
//全球的
//-----------------------------------------------------------------------------
HWND g_HWND=NULL;
LPDIRECT3D9 g_pD3D=NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice=NULL;
ID3DXSprite*g_pD3DXSprite=NULL;
LPDIRECT3DTEXTURE9 g_pTexture=NULL;
屏幕宽度=800;
屏幕上的常数=600;
//-----------------------------------------------------------------------------
//原型
//-----------------------------------------------------------------------------
LRESULT回调WindowProc(HWND-HWND、UINT-uMsg、WPARAM-WPARAM、LPARAM-LPARAM);
HRESULT初始化为3d();
void RenderFrame();
//-----------------------------------------------------------------------------
//名称:WinMain()
//Desc:应用程序的入口点
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
WNDCLASSEX绞车;
MSG-uMsg;
HRESULT-hr;
memset(&uMsg,0,sizeof(uMsg));
winClass.lpszClassName=“MY_WINDOWS_类”;
winClass.cbSize=sizeof(WNDCLASSEX);
winClass.style=CS_HREDRAW | CS_VREDRAW;
WinClasss.lpfnWndProc=WindowProc;
winClass.hInstance=hInstance;
winClass.hIcon=加载图标(hInstance,(LPCTSTR)IDC\U DXSPRITE);
winClass.hIconSm=加载图标(hInstance,(LPCTSTR)IDC\U DXSPRITE);
winClass.hCursor=LoadCursor(空,IDC_箭头);
winClass.hbrBackground=(HBRUSH)GetStockObject(黑色画笔);
winClass.lpszMenuName=NULL;
winClass.cbClsExtra=0;
winClass.cbWndExtra=0;
if(!RegisterClass(&winClass))
返回E_失败;
g_hWnd=CreateWindowEx(WS_EX_分层,“我的WINDOWS_类”,
“Direct3D 9-ID3DXSprite示例”,
WS|U重叠窗口| WS|U可见,
0,0,屏幕宽度,屏幕高度,NULL,NULL,hInstance,NULL);
如果(g_hWnd==NULL)
返回E_失败;
SetLayeredWindowAttributes(g_hWnd,RGB(0x00,0x00,0x00),0,LWA_COLORKEY});
展示窗口(g_hWnd,nCmdShow);
//----------------------------------------------------------------
//创建DirectX设备
//----------------------------------------------------------------
如果(失败(初始化为3D())
返回0;
//----------------------------------------------------------------
//创建ID3DXSprite
//----------------------------------------------------------------
//创建ID3DXSprite接口对象
hr=D3DXCreateSprite(g_pd3ddence和g_pD3DXSprite);
如果(失败(小时))
返回人力资源;
//----------------------------------------------------------------
//加载精灵的纹理
//----------------------------------------------------------------
// --------------------------------------------------------
//加载纹理。我决定使用扩展的
//版本的纹理加载函数只是为了显示什么
//看起来像。
//纹理是用Photoshop创建的,带有透明的
//首先是背景。然后添加了线十字线。
//
//注意-如果您不使用具有
//2宽度或高度的大小,则图像可能无法加载
//对。此图像是256x256。
//
D3DXCreateTextureFromFileEx(
g_PD3D设备,
“C:\\Documents and Settings\\Death\\My Documents\\45handold2.tga”,//我们的纹理图像!
D3DX_默认值,//宽度
D3DX_默认值,//高度
D3DX_默认值,//MIP级别
0,//用法
D3DFMT_DXT1,//纹理格式
D3DPOOL\u托管,//内存池
D3DX_默认值,//过滤器
D3DX_默认值,//MIP过滤器
0,//透明颜色键
NULL,//图像信息结构
NULL,//调色板
&g_pTexture);//如果成功,则返回纹理
如果(失败(小时))
返回人力资源;
// ---------
//主回路
// ---------
while(uMsg.message!=WM_退出)
{
if(peek消息(&uMsg,NULL,0,0,PM_-REMOVE))
{
翻译信息(uMsg);
调度消息(&uMsg);
}
}
// -------------------------
//发布directx资源
// -------------------------
if(g_pD3DXSprite)
{
g_pD3DXSprite->Release();
g_pD3DXSprite=NULL;
}
if(g_pTexture)
{
g_pTexture->Release();
g_pTexture=NULL;
}
if(g_pD3DDevice)
{
g_pD3DDevice->Release();
g_pD3DDevice=NULL;
}
注销类(“我的WINDOWS类”,winClass.hInstance);
返回(int)uMsg.wParam;
}
//-----------------------------------------------------------------------------
//名称:WindowProc()
//Desc:窗口的消息处理程序
/