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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?