acm19931223 2015-04-09 15:28 采纳率: 100%
浏览 1881
已采纳

创建窗口过程中的问题

本人最近在做毕业设计,需要基于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;

}

  • 写回答

6条回答 默认 最新

  • a530317920 2015-04-10 06:43
    关注

    CreateWindowEx 第二个参数是classname,要和你注册窗口类型一样吧,要不怎么知道注册哪一个

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件