Win32 OpenGL透明背景

时间:2016-06-24 15:29:02

标签: c++ winapi opengl

我在Win32 Window上的opengl中进行了简单的绘制。我知道如何清除边框和它的好看,但是当我在这个窗口上绘制OpenGL时,我有一个黑色的背景。我在网上测试了很多东西,但都没有。我告诉你代码,也许有人会知道为什么?

#include <iostream>
#include <windows.h>
#include <gl\gl.h>

LRESULT CALLBACK WindowProcedure( HWND window, UINT message, WPARAM wParam,     LPARAM lParam ) {
if ( message ) {     
    return DefWindowProc( window, message, wParam, lParam );
}

return 0;
}

int main()
{
POINT  ptVertex[4]; 
HDC    hDC = NULL;      
HGLRC  hRC = NULL;      
GLuint PixelFormat;

WNDCLASSEX wincl;    

wincl.hInstance = 0;
wincl.lpszClassName = "Window";
wincl.lpfnWndProc = WindowProcedure;    
wincl.style = CS_HREDRAW | CS_VREDRAW;       
wincl.cbSize = sizeof (WNDCLASSEX);

wincl.hIcon        = NULL;
wincl.hIconSm      = NULL;
wincl.hCursor      = NULL;
wincl.lpszMenuName = NULL;               

wincl.cbClsExtra = 0;                  
wincl.cbWndExtra = 0;                 

wincl.hbrBackground = HBRUSH(COLOR_WINDOW);

if( RegisterClassEx( &wincl ) )
{
    HWND window = CreateWindowEx( WS_EX_LAYERED, "Window", 0,
               WS_OVERLAPPED | WS_POPUP, 0, 0,
               200, 200, 0, 0, 0, 0 ) ;
    if( window )
    {
        ptVertex[0].x = 0;
        ptVertex[0].y = 0;
        ptVertex[1].x = 0;
        ptVertex[1].y = 200;
        ptVertex[2].x = 200;
        ptVertex[2].y = 200;
        ptVertex[3].x = 200;
        ptVertex[3].y = 0;

        SetWindowLong( window, GWL_EXSTYLE, GetWindowLong( window, GWL_EXSTYLE ) | WS_EX_LAYERED );
        SetLayeredWindowAttributes(window, 0x0, 0, LWA_COLORKEY);
        HRGN region = CreatePolygonRgn( ptVertex, 4, ALTERNATE );
        SetWindowRgn( window, region, false );
        ShowCursor( false );

        hDC = GetDC( window );

    PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
8,0, 8,8, 8,16, 8,24, //A guess for lil endian; usually these are 0 (making them 0 doesn't help)
0, 0,0,0,0,
24, //depth
8,  //stencil
0,  //aux
PFD_MAIN_PLANE,
0,
0, 0, 0
};

        PixelFormat=ChoosePixelFormat( hDC, &pfd );
        SetPixelFormat( hDC, PixelFormat, &pfd );
        hRC = wglCreateContext( hDC );
        wglMakeCurrent( hDC, hRC )
        ShowWindow( window, SW_SHOWDEFAULT ) ;

glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES);                              // Drawing Using Triangles
    glColor3f(1.0f,0.0f,0.0f);                      // Set The Color To Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
    glColor3f(0.0f,1.0f,0.0f);                      // Set The Color To Green
    glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
    glColor3f(0.0f,0.0f,1.0f);                      // Set The Color To Blue
    glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
glEnd();

SwapBuffers( hDC );

        MSG msg ;

        for( ;; ) {
            if ( GetMessage( &msg, 0, 0, 0 ) )
                DispatchMessage( &msg ) ;

        }
    }
}
}

0 个答案:

没有答案
相关问题