bl WndCreate(char* title, int width, int height, int bits, bl ifFullScreen)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc;//窗口类
DWORD dwExStyle;//窗口的扩展风格
DWORD dwStyle;//窗口风格
RECT WindowRect;//窗口大小
DEVMODE dmScreenSettings;
//设定窗口大小
WindowRect.left = (long)0;
WindowRect.right = (long)width;
WindowRect.top = (long)0;
WindowRect.bottom = (long)height;
fullScreen=ifFullScreen;//设定全局的全屏指示变量
hInstance = GetModuleHandle (NULL);//设定进程句柄
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;//设定窗口风格
wc.lpfnWndProc = (WNDPROC) WndProc;//设定消息处理函数
wc.cbClsExtra = 0;//无类附加信息
wc.cbWndExtra = 0;//无窗口附加信息
wc.hInstance = hInstance;//设定类中的进程句柄
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);//使用默认图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW);//使用默认鼠标指针
wc.hbrBackground = NULL;//无背景画笔
wc.lpszMenuName = NULL;//没有菜单
wc.lpszClassName = "MyClass";//设定类名称
if (!RegisterClass(&wc))//注册窗口类
{
MessageBox(NULL,"注册窗口类失败。","错误",MB_OK|MB_ICONEXCLAMATION);
return f;
}
if (fullScreen)//程序请求全屏状态,设法改变显示设置
{
MessageBox (NULL, "全屏状态准备中", "信息", MB_OK);
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));//清空变量
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;//宽
dmScreenSettings.dmPelsHeight = height;//高
dmScreenSettings.dmBitsPerPel = bits;//BPP;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
//尝试改变显示设置
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
//无法使用全屏模式
if (MessageBox (NULL,"您的显卡似乎不支持所需的全屏模式。\n换用窗口模式吗?","Purple^Moon Present",MB_YESNO | MB_ICONEXCLAMATION)==IDYES)
{
fullScreen = f; //换用窗口模式
}
else
{
//退出
MessageBox (NULL,"程序将从此退出。","错误",MB_OK|MB_ICONSTOP);
return f; // Return f
}
}
}
if (fullScreen)
{
MessageBox (NULL, "全屏状态准备中", "信息", MB_OK);
dwExStyle = WS_EX_APPWINDOW;//扩展风格
dwStyle = WS_POPUP;//基础风格
}
else
{
MessageBox (NULL, "窗口状态准备中", "信息", MB_OK);
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;//扩展风格
dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_BORDER | WS_SYSMENU;//基础风格
}
//调整窗口大小
if (!AdjustWindowRectEx(&WindowRect, dwStyle, f, dwExStyle))
MessageBox (NULL, "调整窗口大小失败", "消息", MB_OK);
if (!hInstance)
MessageBox (NULL, "不能获得进程句柄", "消息", MB_OK);
MessageBox (NULL, "准备的窗口标题是", title, MB_OK);
if (!(hWnd = CreateWindowEx(dwExStyle,//扩展风格
"MyClass",//类名
title,//标题
dwStyle |//基础风格
WS_CLIPSIBLINGS |//基础风格
WS_CLIPCHILDREN,//基础风格
0, 0,//窗口位置
WindowRect.right - WindowRect.left,//窗口宽
WindowRect.bottom - WindowRect.top,//窗口高
NULL,//无父窗口
NULL,//无菜单
hInstance,//指定进程
NULL)))//无附加信息
{
WndDestroy();//销毁并复位
MessageBox(NULL,"创建窗口失败。","错误", MB_OK|MB_ICONEXCLAMATION);
return f;//失败
}
MessageBox (NULL, "窗体终于创建成功了!OTL", "成功了", MB_OK);
return f;
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),//pfd的大小
1,//版本
PFD_DRAW_TO_WINDOW |//直接绘制到窗口
PFD_SUPPORT_OPENGL |//支持OpenGL
PFD_DOUBLEBUFFER,//双缓冲
PFD_TYPE_RGBA,//RGBA格式
0,//色深
0, 0, 0, 0, 0, 0,//忽略的颜色
0,//无Alpha缓冲区
0,//忽略平移位
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
pfd.cColorBits = bits;//设定色深
if (!(hDC=GetDC(hWnd)))//获取设备上下文
{
WndDestroy();//重置销毁
MessageBox(NULL,"无法创建OpenGL渲染设备。","错误",MB_OK|MB_ICONEXCLAMATION);
return f;
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))//指定像素模式
{
WndDestroy();//重置销毁
MessageBox(NULL,"无法找到合适的像素模式","错误",MB_OK|MB_ICONEXCLAMATION);
return f;
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd))//使用像素模式
{
WndDestroy();//重置销毁
MessageBox(NULL,"无法设置像素格式。","错误",MB_OK|MB_ICONEXCLAMATION);
return f;
}
if (!(hRC=wglCreateContext(hDC)))//创建OpenGL设备
{
WndDestroy();//重置销毁
MessageBox(NULL,"无法创建OpenGL渲染设备。","错误",MB_OK|MB_ICONEXCLAMATION);
return f;
}
if(!wglMakeCurrent(hDC,hRC))//激活OpenGL设备
{
WndDestroy();//重置销毁
MessageBox(NULL,"无法激活OpenGL渲染设备。","错误",MB_OK|MB_ICONEXCLAMATION);
return f;
}
ShowWindow(hWnd,SW_SHOW);//显示窗口
SetForegroundWindow(hWnd);
SetFocus(hWnd);
xInitGL ();
xResizeFrame(width, height);
return t;
}
Error when creating a window...
The following function is based on Nehe's CreateGLWindow function.
The message box reported error after CreateWindowEx was called. But I can't find what's the matter.
I have checked the window rect, the window style and extend window style, yet the function didn't work anyway.
Note: Lots of comment and string are in Chinese, but I can't believe that's the reason... >_<
I can't see anything wrong with your code. I would try removing the OpenGL parts and seeing if the window is created then.
Quote:Original post by Dave Hunt
Make sure your window procedure is return 0 on WM_CREATE.
Yes, 0 is returned and it still doesn't work. T_T
The Error Code is 6: ERROR_INVALID_HANDLE.
It seems that a Handle passed to the function is invalid. Then I checked the handles. And then I found that if I set the hIcon of the Window Structure to NULL the Error Code becomes 0:ERROR_SUCCESS, yet the the CreateWindowEx STILL FAILES!!!
What's the deal???!!! I'm completely puzzled.
[Edited by - purple_moon on April 5, 2006 7:31:55 AM]
It seems that a Handle passed to the function is invalid. Then I checked the handles. And then I found that if I set the hIcon of the Window Structure to NULL the Error Code becomes 0:ERROR_SUCCESS, yet the the CreateWindowEx STILL FAILES!!!
What's the deal???!!! I'm completely puzzled.
[Edited by - purple_moon on April 5, 2006 7:31:55 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement