BOOL CGLWindow::CreateGLWindow(LPCTSTR lpszClassName, LPCTSTR lpszTitle,
UINT nWidth/* = 640*/, UINT nHeight/* = 480*/, UINT nBpp/* = 16*/,
BOOL bFullscreen/* = FALSE*/)
{
// TODO: Create the OpenGL window
GLuint nPixelFormat; // Window's pixel format
WNDCLASS wndClass; // Window class structure
DWORD dwStyle, dwExStyle; // Window's styles and extended styles
RECT wndRect; // Window's rectangle
wndRect.left = 0; // Left side
wndRect.right = nWidth; // Right side (width)
wndRect.top = 0; // Top side
wndRect.bottom = nHeight; // Bottom side (height)
// Copy each variable to its corresponding class member
m_nWidth = nWidth; // Window width
m_nHeight = nHeight; // Window height
m_nBpp = nBpp; // Window bpp
m_bFullscreen = bFullscreen; // Copy fullscreen flag
// Setup and register window class
m_hInstance = GetModuleHandle(NULL); // Get handle to application's instance
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Class style
wndClass.lpfnWndProc = (WNDPROC) glWndProc; // Wnd. proc. function
wndClass.cbClsExtra = 0; // No extra class data
wndClass.cbWndExtra = 0; // No extra window data
wndClass.hInstance = m_hInstance; // Set app. instance handle
wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Set default icon
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Set default cursor
wndClass.hbrBackground = NULL; // No background (OGL does not require it)
wndClass.lpszMenuName = NULL; // No menu
wndClass.lpszClassName = lpszClassName; // Window's class name
if (! RegisterClass(&wndClass)) // Register the window class (from 'wndClass' object)
{
// Display error message and return error
MessageBox(NULL, "Error registering window class in 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
return FALSE;
}
// Check for fullscreen mode
if (bFullscreen) // Go fullscreen
{
DEVMODE dmScreen; // Screen settings
memset(&dmScreen, 0, sizeof(dmScreen)); // Make sure memory is cleared
// Setup members of 'dmScreen'
dmScreen.dmSize = sizeof(dmScreen); // Set memory size
dmScreen.dmPelsWidth = nWidth; // Set width
dmScreen.dmPelsHeight = nHeight; // Set height
dmScreen.dmBitsPerPel = nBpp; // Set bits-per-pixel
dmScreen.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; // Set flags for use
// Try to switch to desired fullscreen mode
if (ChangeDisplaySettings(&dmScreen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
if (MessageBox(NULL, "Fullscreen mode could not initialize.\nWould you like to use windowed mode?\n",
"Error", MB_YESNO | MB_ICONQUESTION) == IDYES) // Prompt for windowed mode
{
m_bFullscreen = FALSE; // Switch to windowed mode
}
else // Exit the application (fullscreen could not switch and user did not want windowed mode)
return FALSE; // Return on error
}
}
// Check for fullscreen mode
if (m_bFullscreen) // Are we in fullscreen mode?
{
dwExStyle = WS_EX_APPWINDOW; // Set window ext. style
dwStyle = WS_POPUP; // Set window style
ShowCursor(FALSE); // Hide the cursor
}
else // Windowed mode
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Set window ext. style
dwStyle = WS_OVERLAPPEDWINDOW; // Set window style
}
AdjustWindowRectEx(&wndRect, dwStyle, FALSE, dwExStyle); // Adjust window's rect.
// Attempt to create the window itself using 'CreateWindowEx'
if (! (m_hWnd = CreateWindowEx(dwExStyle, // Window's extended style
lpszClassName, // Window's class name
lpszTitle, // Window's title
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle, // Set window style
0, 0, // Window position (top-left corner)
wndRect.right - wndRect.left, // Adjusted window width
wndRect.bottom - wndRect.top, // Adjusted window height
NULL, // No parent window
NULL, // No menu
m_hInstance, // Application instance handle
NULL))) // Don't pass anything to 'WM_CREATE' message
{
// Kill the window, display error message, and return an error
char szError[256];
sprintf(szError, "Last Error: %d", GetLastError());
KillWindow(); // Kill the window (and anything else that was already initialized)
MessageBox(NULL, "Error in 'CreateWindowEx' in function 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
MessageBox(NULL, szError, NULL, NULL);
return FALSE;
}
// Setup a pixel format descriptor
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // Set size of 'pfd' object
1, // Version number
PFD_DRAW_TO_WINDOW | // Format must support window
PFD_SUPPORT_OPENGL | // Format must support OpenGL
PFD_DOUBLEBUFFER | // Format must support double-buffering
PFD_TYPE_RGBA, // Request RGBA format
nBpp, // Select specified color depth (bpp)
0, 0, 0, 0, 0, 0, // Ignore the color bits
0, // No alpha buffer
0, // Shift bit ignored
0, // No accumulation buffer
0, 0, 0, 0, // Accumulation bits ignored
16, // 16-bit depth (Z) buffer
0, 0, // No stencil or auxiliary buffer
PFD_MAIN_PLANE, // Main drawing layer
0, // Reserved (ignore)
0, 0, 0 // Layer masks ignored
};
// Attempt to get device context for window
if (! (m_hDC = GetDC(m_hWnd)))
{
// Kill the window, display error message, and return an error
KillWindow(); // Kill the window
MessageBox(NULL, "Error creating Device Context in function 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
return FALSE;
}
// Attempt to select the pixel format (from 'pfd' object)
if (! (nPixelFormat = ChoosePixelFormat(m_hDC, &pfd)))
{
// Kill the window, display error message, and return an error
KillWindow(); // Kill the window
MessageBox(NULL, "Error in 'ChoosePixelFormat' in function 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
return FALSE;
}
// Attempt to set the pixel format for the screen
if (! SetPixelFormat(m_hDC, nPixelFormat, &pfd))
{
// Kill the window, display error message, and return an error
KillWindow(); // Kill the window
MessageBox(NULL, "Error in 'SetPixelFormat' in function 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
return FALSE;
}
// Attempt to create an OpenGL rendering context
if (! (m_hRC = wglCreateContext(m_hDC)))
{
// Kill the window, display error message, and return an error
KillWindow(); // Kill the window
MessageBox(NULL, "Error creating GLRC in function 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
return FALSE;
}
// Attempt to activate the rc
if (! wglMakeCurrent(m_hDC, m_hRC))
{
// Kill the window, display error message, and return an error
KillWindow(); // Kill the window
MessageBox(NULL, "Error activating 'm_hRC' in function 'CGLWindow::CreateGLWindow'...\n", NULL, NULL);
return FALSE;
}
ShowWindow(m_hWnd, SW_SHOW); // Show the window
SetForegroundWindow(m_hWnd); // Set window as foreground window
SetFocus(m_hWnd); // Set focus to this window
ResizeScene(m_nWidth, m_nHeight); // Resize the GL scene to the window
// Attempt to initialize OpenGL
if (! InitGL())
{
// Kill the window, display error message, and return an error
KillWindow(); // Kill the window
MessageBox(NULL, "Error initializing OpenGL in function 'CGLWindow::InitGL'...\n", NULL, NULL);
return FALSE;
}
return TRUE; // Successful; return okay
}
🔒 Locked
'CreateWindowEx' causes error when called from a class function?
Advertisement
Original Post
Hey again. I am posting regarding the same project I was working on yesterday, when I made a post. I am creating an OpenGL framework that I can use for my OpenGL projects, if you didn't know. Anyhow, I am creating a window like this in a class: The call to 'CreateWindowEx' fails, and displays "Last Error: 87". I used the 'Error Lookup' tool, and it says "The parameter is incorrect." Why is this happening? I have never had a problem creating a window before, and it only does this when called from a class function. It has nothing to do with any of the other functions that are called in 'CreateGLWindow'. It successfully registers the window class, and everything between 'RegisterClass' and 'CreateWindowEx' succeeds. Can anyone tell me why this is happening? I am using MSVC++ 6.0 on Win98.
When you call the function, are you sure you are passing a valid class name and a valid title?
Some windows styles and extended styles might be incompatible with each other. That be my first course of investigation: looking up the styles that you use in the documentation to see if there are any conflicts. I know WS_POPUP has some restrictions; don't know if WS_EX_APPWINDOW is one of them.
Well, I don't think that's the problem. And I just tried altering it, and it's the same thing. I used the NeHe base code as an example for my code. And I'm not using 'WS_POPUP' because I am first testing it in windowed mode. I only specify the 'WS_POPUP' style when I go fullscreen (as does NeHe).
At the first glance I can see you're using WNDCLASS for CreateWindowEx (), what does that mean ?.[smile]
This should cause an unhandled exception...
This should cause an unhandled exception...
That was a result of my own stupidity! =P LoL, I never thought about that. I had declared it to take a 'CGLWindow' pointer so it could access the members of the window class (since the function is static, it can't do it by itself). But now, when I create the function definition, I just do it in my 'WinMain' file, so that I can access the 'g_pMainWnd' object.
Thank you all for your suggestions/solution! =)
Thank you all for your suggestions/solution! =)
Did you try to redeclare wndClass to be WNDCLASSEX ?.
If so, put wndClass.hIconSm = NULL to fully initialize wndClass, it should be no problem then.
Also glWndProc is declared incorrectly as Mastaba stated.
Good luck.
Edit: Well, :-p congrats.
If so, put wndClass.hIconSm = NULL to fully initialize wndClass, it should be no problem then.
Also glWndProc is declared incorrectly as Mastaba stated.
Good luck.
Edit: Well, :-p congrats.
@Skeleton_V@T
WNDCLASSEX doesn't have anything to do with CreateWindowEx(). You can use both types of window classes on both types of windows.
... and you forgot to say to set the size of the structure to sizeof(WNDCLASSEX)! (WNDCLASS doesn't have a size variable)
WNDCLASSEX doesn't have anything to do with CreateWindowEx(). You can use both types of window classes on both types of windows.
... and you forgot to say to set the size of the structure to sizeof(WNDCLASSEX)! (WNDCLASS doesn't have a size variable)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement