zoukankan      html  css  js  c++  java
  • Windows 分层窗口 桌面上透明 Direct3D

    Windows 分层窗口 桌面上透明 Direct3D

      1 //IDirect3DSurface9 GetDC  UpdateLayeredWindow
      2 
      3 #include <Windows.h>
      4 #include <mmsystem.h>
      5 #include <d3dx9.h>
      6 #pragma warning( disable : 4996 ) 
      7 #include <strsafe.h>
      8 #pragma warning( default : 4996 )
      9 
     10 //include path $(DXSDK_DIR)Include;
     11 //library path $(DXSDK_DIR)Libx86
     12 //library d3dx9.lib d3d9.lib Winmm.lib
     13 
     14 
     15 LPDIRECT3D9             g_pD3D = NULL;
     16 LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;
     17 LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
     18 
     19 HWND g_hWnd = NULL;
     20 IDirect3DSurface9*        g_pkRenderTarget = NULL;
     21 IDirect3DSurface9*        g_pkOffscreenPlainSurface = NULL;
     22 
     23 struct CUSTOMVERTEX
     24 {
     25     FLOAT x, y, z;
     26     DWORD color;
     27 };
     28 
     29 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
     30 
     31 HRESULT InitD3D(HWND hWnd)
     32 {
     33     if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
     34     {
     35         return E_FAIL;
     36     }
     37 
     38     D3DPRESENT_PARAMETERS d3dpp;
     39     ZeroMemory(&d3dpp, sizeof(d3dpp));
     40     d3dpp.Windowed = TRUE;
     41     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
     42     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
     43 
     44     if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
     45     {
     46         return E_FAIL;
     47     }
     48 
     49     g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
     50     g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
     51 
     52     return S_OK;
     53 }
     54 
     55 HRESULT InitGeometry()
     56 {
     57     CUSTOMVERTEX g_Vertices[] =
     58     {
     59         { -1.0f, -1.0f, 0.0f, 0xffff0000, },
     60         { 1.0f, -1.0f, 0.0f, 0xff0000ff, },
     61         { 0.0f, 1.0f, 0.0f, 0xffffffff, },
     62     };
     63 
     64     if (FAILED(g_pd3dDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL)))
     65     {
     66         return E_FAIL;
     67     }
     68 
     69     VOID* pVertices;
     70     if (FAILED(g_pVB->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0)))
     71     {
     72         return E_FAIL;
     73     }
     74     memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
     75     g_pVB->Unlock();
     76 
     77     return S_OK;
     78 }
     79 
     80 VOID Cleanup()
     81 {
     82     if (g_pVB != NULL)
     83     {
     84         g_pVB->Release();
     85     }
     86     if (g_pd3dDevice != NULL)
     87     {
     88         g_pd3dDevice->Release();
     89     }
     90     if (g_pD3D != NULL)
     91     {
     92         g_pD3D->Release();
     93     }
     94 }
     95 
     96 VOID SetupMatrices()
     97 {
     98     D3DXMATRIXA16 matWorld;
     99     UINT iTime = timeGetTime() % 1000;
    100     FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
    101     D3DXMatrixRotationY(&matWorld, fAngle);
    102     g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
    103 
    104     D3DXVECTOR3 vEyePt(0.0f, 3.0f, -5.0f);
    105     D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);
    106     D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);
    107     D3DXMATRIXA16 matView;
    108     D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);
    109     g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
    110 
    111     D3DXMATRIXA16 matProj;
    112     D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f);
    113     g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
    114 
    115 }
    116 
    117 VOID Render()
    118 {
    119 
    120     RECT kRect;
    121     GetWindowRect(g_hWnd, &kRect);
    122 
    123     UINT uiWndWidth = kRect.right - kRect.left;
    124     UINT uiWndHeight = kRect.bottom - kRect.top;
    125 
    126     if (!g_pkOffscreenPlainSurface)
    127     {
    128         g_pd3dDevice->CreateOffscreenPlainSurface(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pkOffscreenPlainSurface, 0);
    129         g_pd3dDevice->CreateRenderTarget(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &g_pkRenderTarget, 0);
    130     }
    131     else
    132     {
    133         D3DSURFACE_DESC kDesc;
    134         g_pkOffscreenPlainSurface->GetDesc(&kDesc);
    135 
    136         if (kDesc.Width != uiWndWidth || kDesc.Width != uiWndHeight)
    137         {
    138             g_pkOffscreenPlainSurface->Release();
    139             g_pkRenderTarget->Release();
    140             g_pd3dDevice->CreateOffscreenPlainSurface(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pkOffscreenPlainSurface, 0);
    141             g_pd3dDevice->CreateRenderTarget(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &g_pkRenderTarget, 0);
    142         }
    143     }
    144 
    145     if (!g_pkOffscreenPlainSurface || !g_pkRenderTarget)
    146     {
    147         return;
    148     }
    149 
    150     HRESULT hr = g_pd3dDevice->SetRenderTarget(0, g_pkRenderTarget);
    151     g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    152 
    153     if (SUCCEEDED(g_pd3dDevice->BeginScene()))
    154     {
    155         SetupMatrices();
    156 
    157         g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
    158         g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
    159         g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
    160 
    161         g_pd3dDevice->EndScene();
    162     }
    163 
    164     g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
    165 
    166     hr = g_pd3dDevice->GetRenderTargetData(g_pkRenderTarget, g_pkOffscreenPlainSurface);
    167 
    168     HDC hDC = 0;
    169     //g_pkRenderTarget->GetDC(&hDC);
    170     g_pkOffscreenPlainSurface->GetDC(&hDC);
    171 
    172     POINT kPoint = { 0, 0 };
    173     SIZE kSize = { uiWndWidth, uiWndHeight };
    174 
    175     BLENDFUNCTION kBlend;
    176     kBlend.AlphaFormat = AC_SRC_ALPHA;
    177     kBlend.SourceConstantAlpha = 255;
    178     kBlend.BlendFlags = 0;
    179     kBlend.BlendOp = AC_SRC_OVER;
    180 
    181     UpdateLayeredWindow(g_hWnd, 0, &kPoint, &kSize, hDC, &kPoint, 0, &kBlend, ULW_ALPHA);
    182 
    183     g_pkOffscreenPlainSurface->ReleaseDC(hDC);
    184     //g_pkRenderTarget->ReleaseDC(hDC);
    185 }
    186 
    187 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    188 {
    189     switch (msg)
    190     {
    191     case WM_DESTROY:
    192         Cleanup();
    193         PostQuitMessage(0);
    194         return 0;
    195     }
    196 
    197     return DefWindowProc(hWnd, msg, wParam, lParam);
    198 }
    199 
    200 INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, INT)
    201 {
    202     UNREFERENCED_PARAMETER(hInst);
    203 
    204     WNDCLASSEX wc =
    205     {
    206         sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL
    207     };
    208     RegisterClassEx(&wc);
    209 
    210     g_hWnd = CreateWindowEx(WS_EX_LAYERED, L"D3D Tutorial", L"D3D Tutorial 03: Matrices", WS_POPUP | WS_THICKFRAME, 100, 100, 512, 512, NULL, NULL, wc.hInstance, NULL);
    211 
    212     if (SUCCEEDED(InitD3D(g_hWnd)))
    213     {
    214         if (SUCCEEDED(InitGeometry()))
    215         {
    216             ShowWindow(g_hWnd, SW_SHOWDEFAULT);
    217             UpdateWindow(g_hWnd);
    218 
    219             MSG msg;
    220             ZeroMemory(&msg, sizeof(msg));
    221             while (msg.message != WM_QUIT)
    222             {
    223                 if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
    224                 {
    225                     TranslateMessage(&msg);
    226                     DispatchMessage(&msg);
    227                 }
    228                 else
    229                 {
    230                     Render();
    231                 }
    232             }
    233         }
    234     }
    235 
    236     UnregisterClass(L"D3D Tutorial", wc.hInstance);
    237     return 0;
    238 }
  • 相关阅读:
    iOS设置app应用程序文件共享
    UIButton 一个控件 实现 左图标右文本的效果
    centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误
    centos7 使用 omnibus包安装方式,安装 gitlab7.4
    iOS app 企业内部发布及HTTPS服务器配置
    centos7 安装mariaDB 以及 phpmyadmin的安装
    centos7 最小化安装 无 ifconfig,netstat 的安装
    windows 下使用 Filezilla server 搭建 ftp 服务器
    PostgreSQL 登录时在命令行中输入密码
    Docker Error response from daemon: driver failed programming external connectivity on endpoint webserver
  • 原文地址:https://www.cnblogs.com/diaoss/p/11583453.html
Copyright © 2011-2022 走看看