程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> opengl-創建窗口過程中的問題

opengl-創建窗口過程中的問題

編輯:編程綜合問答
創建窗口過程中的問題

本人最近在做畢業設計,需要基於OpenGL顯示,在創建窗口的過程中遇到了hWnd創建失敗的問題,但是沒有辦法查出來具體問題在哪,請教各位大神了。。跪求解答,萬分感謝,救急!!!

#define WIN32_LEAN_AND_MEAN //裁剪過大的windows庫

#include "windows.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "GLaux.h"

static LPCTSTR lpszAppName = "OpenGL App";

float angle = 0.0f;
HDC g_HDC;
//HBRUSH hBlueBrush,hRedBrush;

void SetupPixelFormat(HDC hDC)
{
GLuint nPixelFormat; //像素格式變量

static  PIXELFORMATDESCRIPTOR pfd =
{

    sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        32,
        0, 0, 0, 0, 0, 0,
        0,
        0,
        0,
        0, 0, 0, 0,
        16,
        0,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0,
};

nPixelFormat = ChoosePixelFormat(hDC,&pfd);

SetPixelFormat(hDC, nPixelFormat, &pfd);

}

//windows事件處理器
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC;
static HDC hDC;
int width,height;

switch(message)
{

case WM_CREATE:

    hDC = GetDC(hWnd);
    g_HDC = hDC;
    SetupPixelFormat(hDC);

    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC,hRC);

    /*MessageBox(NULL,"error5。","關閉錯誤",MB_OK | MB_ICONINFORMATION);return 0;
    break;*/

case WM_CLOSE:
    wglMakeCurrent(hDC,NULL);
    wglDeleteContext(hRC);

    PostQuitMessage(0);

/*  MessageBox(NULL,"error4。","關閉錯誤",MB_OK | MB_ICONINFORMATION);return 0;
    break;*/


case WM_SIZE:
    height = HIWORD(lParam);
    width  = LOWORD(lParam);

    if(height == 0)
    {height = 1;}

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    /*MessageBox(NULL,"error3。","關閉錯誤",MB_OK | MB_ICONINFORMATION);return 0;
    break;*/

/*
case WM_PAINT:
{
PAINTSTRUCT ps;
HBRUSH hOldBrush;

        BeginPaint(hWnd, &ps);

        hOldBrush = SelectObject(ps.hdc, hRedBrush);
        Rectangle(ps.hdc, 100,100,150,150);

        SelectObject(ps.hdc, hOldBrush);
        EndPaint(hWnd, &ps);
    }

    break;

*/
default:
return DefWindowProc(hWnd,message,wParam,lParam);//break;

}

return (0L);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
HWND hWnd;
BOOL done;

/*
hBlueBrush = CreateSolidBrush(RGB(0,0,255));
hRedBrush = CreateSolidBrush(RGB(255,0,0));
*/
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszClassName = lpszAppName;
wc.lpszMenuName = NULL;
wc.hIconSm = LoadIcon(wc.hInstance,IDI_WINLOGO);

if(!RegisterClassEx(&wc))
//  return 0;
{MessageBox(NULL,"error1。","關閉錯誤",MB_OK | MB_ICONINFORMATION);return 0;}

hWnd = CreateWindowEx(          NULL,
                                "MyClass",
                                "The OpenGL window Application",
                                WS_OVERLAPPEDWINDOW | WS_VISIBLE | 
                                WS_SYSMENU | WS_CLIPCHILDREN |
                                WS_CLIPSIBLINGS,
                                100,100,
                                400,400,
                                NULL,
                                NULL,
                                hInstance,
                                NULL);

if(!hWnd)
{
    MessageBox(NULL,"error2。","關閉錯誤",MB_OK | MB_ICONINFORMATION);
    return 0;
}

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

done = FALSE;

while(!done)
{
    PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE);

        if(msg.message == WM_QUIT)
            {done = TRUE;}

        else
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();

            angle = angle + 0.1f;
            if(angle >= 360.f)
                angle = 0.0f;
            glTranslatef(0.0f, 0.0f, -5.0f);    //向後移動五個單位
            glRotatef(angle, 0.0f, 0.0f, 1.0f); //繞z軸轉

            glColor3f(1.0f, 0.0f, 0.0f);
            glBegin(GL_TRIANGLES);
                glVertex3f(0.0f, 0.0f, 0.0f);
                glVertex3f(1.0f, 0.0f, 0.0f);
                glVertex3f(1.0f, 1.0f, 0.0f);
            glEnd();

            SwapBuffers(g_HDC);
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

}


return msg.wParam;

}

最佳回答:


CreateWindowEx 第二個參數是classname,要和你注冊窗口類型一樣吧,要不怎麼知道注冊哪一個

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved