Sign in to follow this  
purple_moon

Error when creating a window...

Recommended Posts

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... >_<
bl WndCreate(char* title, int width, int height, int bits, bl ifFullScreen)
{
  GLuint    PixelFormat;      // Holds The Results After Searching For A Match
  WNDCLASS  wc;//&#31383;&#21475;&#31867;
  DWORD    dwExStyle;//&#31383;&#21475;&#30340;&#25193;&#23637;&#39118;&#26684;
  DWORD    dwStyle;//&#31383;&#21475;&#39118;&#26684;
  RECT    WindowRect;//&#31383;&#21475;&#22823;&#23567;
  DEVMODE dmScreenSettings; 
	//&#35774;&#23450;&#31383;&#21475;&#22823;&#23567;
  WindowRect.left = (long)0;
  WindowRect.right = (long)width;
  WindowRect.top = (long)0;
  WindowRect.bottom = (long)height;

  fullScreen=ifFullScreen;//&#35774;&#23450;&#20840;&#23616;&#30340;&#20840;&#23631;&#25351;&#31034;&#21464;&#37327;

  hInstance      = GetModuleHandle (NULL);//&#35774;&#23450;&#36827;&#31243;&#21477;&#26564;
  wc.style      = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;//&#35774;&#23450;&#31383;&#21475;&#39118;&#26684;
  wc.lpfnWndProc    = (WNDPROC) WndProc;//&#35774;&#23450;&#28040;&#24687;&#22788;&#29702;&#20989;&#25968;
  wc.cbClsExtra    = 0;//&#26080;&#31867;&#38468;&#21152;&#20449;&#24687;
  wc.cbWndExtra    = 0;//&#26080;&#31383;&#21475;&#38468;&#21152;&#20449;&#24687;
  wc.hInstance    = hInstance;//&#35774;&#23450;&#31867;&#20013;&#30340;&#36827;&#31243;&#21477;&#26564;
  wc.hIcon      = LoadIcon(NULL, IDI_WINLOGO);//&#20351;&#29992;&#40664;&#35748;&#22270;&#26631;
  wc.hCursor      = LoadCursor(NULL, IDC_ARROW);//&#20351;&#29992;&#40664;&#35748;&#40736;&#26631;&#25351;&#38024;
  wc.hbrBackground  = NULL;//&#26080;&#32972;&#26223;&#30011;&#31508;
  wc.lpszMenuName    = NULL;//&#27809;&#26377;&#33756;&#21333;
  wc.lpszClassName  = "MyClass";//&#35774;&#23450;&#31867;&#21517;&#31216;

  if (!RegisterClass(&wc))//&#27880;&#20876;&#31383;&#21475;&#31867;
  {
    MessageBox(NULL,"&#27880;&#20876;&#31383;&#21475;&#31867;&#22833;&#36133;&#12290;","&#38169;&#35823;",MB_OK|MB_ICONEXCLAMATION);
    return f;
  }
  
  if (fullScreen)//&#31243;&#24207;&#35831;&#27714;&#20840;&#23631;&#29366;&#24577;&#65292;&#35774;&#27861;&#25913;&#21464;&#26174;&#31034;&#35774;&#32622;
  {
		MessageBox (NULL, "&#20840;&#23631;&#29366;&#24577;&#20934;&#22791;&#20013;", "&#20449;&#24687;", MB_OK);
    memset(&dmScreenSettings,0,sizeof(dmScreenSettings));//&#28165;&#31354;&#21464;&#37327;
    dmScreenSettings.dmSize=sizeof(dmScreenSettings);
    dmScreenSettings.dmPelsWidth  = width;//&#23485;
    dmScreenSettings.dmPelsHeight  = height;//&#39640;
    dmScreenSettings.dmBitsPerPel  = bits;//BPP&#65307;
    dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

    //&#23581;&#35797;&#25913;&#21464;&#26174;&#31034;&#35774;&#32622;
    if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
    {
      //&#26080;&#27861;&#20351;&#29992;&#20840;&#23631;&#27169;&#24335;
      if (MessageBox (NULL,"&#24744;&#30340;&#26174;&#21345;&#20284;&#20046;&#19981;&#25903;&#25345;&#25152;&#38656;&#30340;&#20840;&#23631;&#27169;&#24335;&#12290;\n&#25442;&#29992;&#31383;&#21475;&#27169;&#24335;&#21527;&#65311;","Purple^Moon Present",MB_YESNO | MB_ICONEXCLAMATION)==IDYES)
      {
        fullScreen = f;    //&#25442;&#29992;&#31383;&#21475;&#27169;&#24335;
      }
      else
      {
        //&#36864;&#20986;
        MessageBox (NULL,"&#31243;&#24207;&#23558;&#20174;&#27492;&#36864;&#20986;&#12290;","&#38169;&#35823;",MB_OK|MB_ICONSTOP);
        return f;                  // Return f
      }
    }
  }

  if (fullScreen) 
  {
		MessageBox (NULL, "&#20840;&#23631;&#29366;&#24577;&#20934;&#22791;&#20013;", "&#20449;&#24687;", MB_OK);
    dwExStyle = WS_EX_APPWINDOW;//&#25193;&#23637;&#39118;&#26684;
    dwStyle = WS_POPUP;//&#22522;&#30784;&#39118;&#26684;    
  }
  else
  {
		MessageBox (NULL, "&#31383;&#21475;&#29366;&#24577;&#20934;&#22791;&#20013;", "&#20449;&#24687;", MB_OK);
    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;//&#25193;&#23637;&#39118;&#26684;
    dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_BORDER | WS_SYSMENU;//&#22522;&#30784;&#39118;&#26684;
  }

	//&#35843;&#25972;&#31383;&#21475;&#22823;&#23567;
	if (!AdjustWindowRectEx(&WindowRect, dwStyle, f, dwExStyle))
		MessageBox (NULL, "&#35843;&#25972;&#31383;&#21475;&#22823;&#23567;&#22833;&#36133;", "&#28040;&#24687;", MB_OK);
	if (!hInstance)
		MessageBox (NULL, "&#19981;&#33021;&#33719;&#24471;&#36827;&#31243;&#21477;&#26564;", "&#28040;&#24687;", MB_OK);
	MessageBox (NULL, "&#20934;&#22791;&#30340;&#31383;&#21475;&#26631;&#39064;&#26159;", title, MB_OK);
  if (!(hWnd = CreateWindowEx(dwExStyle,//&#25193;&#23637;&#39118;&#26684;
                "MyClass",//&#31867;&#21517;
                title,//&#26631;&#39064;
                dwStyle |//&#22522;&#30784;&#39118;&#26684;
                WS_CLIPSIBLINGS |//&#22522;&#30784;&#39118;&#26684;
                WS_CLIPCHILDREN,//&#22522;&#30784;&#39118;&#26684;
                0, 0,//&#31383;&#21475;&#20301;&#32622;
                WindowRect.right - WindowRect.left,//&#31383;&#21475;&#23485;
                WindowRect.bottom - WindowRect.top,//&#31383;&#21475;&#39640;
                NULL,//&#26080;&#29238;&#31383;&#21475;
                NULL,//&#26080;&#33756;&#21333;
                hInstance,//&#25351;&#23450;&#36827;&#31243;
                NULL)))//&#26080;&#38468;&#21152;&#20449;&#24687;
  {
    WndDestroy();//&#38144;&#27585;&#24182;&#22797;&#20301;
    MessageBox(NULL,"&#21019;&#24314;&#31383;&#21475;&#22833;&#36133;&#12290;","&#38169;&#35823;", MB_OK|MB_ICONEXCLAMATION);
    return f;//&#22833;&#36133;
  }
	MessageBox (NULL, "&#31383;&#20307;&#32456;&#20110;&#21019;&#24314;&#25104;&#21151;&#20102;&#65281;OTL", "&#25104;&#21151;&#20102;", MB_OK);
	return f;

  static  PIXELFORMATDESCRIPTOR pfd=
  {
    sizeof(PIXELFORMATDESCRIPTOR),//pfd&#30340;&#22823;&#23567;
    1,//&#29256;&#26412;
    PFD_DRAW_TO_WINDOW |//&#30452;&#25509;&#32472;&#21046;&#21040;&#31383;&#21475;
    PFD_SUPPORT_OPENGL |//&#25903;&#25345;OpenGL
    PFD_DOUBLEBUFFER,//&#21452;&#32531;&#20914;
    PFD_TYPE_RGBA,//RGBA&#26684;&#24335;
    0,//&#33394;&#28145;
    0, 0, 0, 0, 0, 0,//&#24573;&#30053;&#30340;&#39068;&#33394;
    0,//&#26080;Alpha&#32531;&#20914;&#21306;
    0,//&#24573;&#30053;&#24179;&#31227;&#20301;
    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;//&#35774;&#23450;&#33394;&#28145;
  
  if (!(hDC=GetDC(hWnd)))//&#33719;&#21462;&#35774;&#22791;&#19978;&#19979;&#25991;
  {
    WndDestroy();//&#37325;&#32622;&#38144;&#27585;
    MessageBox(NULL,"&#26080;&#27861;&#21019;&#24314;OpenGL&#28210;&#26579;&#35774;&#22791;&#12290;","&#38169;&#35823;",MB_OK|MB_ICONEXCLAMATION);
    return f;
  }

  if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))//&#25351;&#23450;&#20687;&#32032;&#27169;&#24335;
  {
    WndDestroy();//&#37325;&#32622;&#38144;&#27585;
    MessageBox(NULL,"&#26080;&#27861;&#25214;&#21040;&#21512;&#36866;&#30340;&#20687;&#32032;&#27169;&#24335;","&#38169;&#35823;",MB_OK|MB_ICONEXCLAMATION);
    return f;
  }

  if(!SetPixelFormat(hDC,PixelFormat,&pfd))//&#20351;&#29992;&#20687;&#32032;&#27169;&#24335;
  {
    WndDestroy();//&#37325;&#32622;&#38144;&#27585;
    MessageBox(NULL,"&#26080;&#27861;&#35774;&#32622;&#20687;&#32032;&#26684;&#24335;&#12290;","&#38169;&#35823;",MB_OK|MB_ICONEXCLAMATION);
    return f;
  }

  if (!(hRC=wglCreateContext(hDC)))//&#21019;&#24314;OpenGL&#35774;&#22791;
  {
    WndDestroy();//&#37325;&#32622;&#38144;&#27585;
    MessageBox(NULL,"&#26080;&#27861;&#21019;&#24314;OpenGL&#28210;&#26579;&#35774;&#22791;&#12290;","&#38169;&#35823;",MB_OK|MB_ICONEXCLAMATION);
    return f;
  }

  if(!wglMakeCurrent(hDC,hRC))//&#28608;&#27963;OpenGL&#35774;&#22791;
  {
    WndDestroy();//&#37325;&#32622;&#38144;&#27585;
    MessageBox(NULL,"&#26080;&#27861;&#28608;&#27963;OpenGL&#28210;&#26579;&#35774;&#22791;&#12290;","&#38169;&#35823;",MB_OK|MB_ICONEXCLAMATION);
    return f;
  }

  ShowWindow(hWnd,SW_SHOW);//&#26174;&#31034;&#31383;&#21475;
  SetForegroundWindow(hWnd);
  SetFocus(hWnd);
	xInitGL ();
  xResizeFrame(width, height);


  return t;
}


Share this post


Link to post
Share on other sites
I can't see anything wrong with your code. I would try removing the OpenGL parts and seeing if the window is created then.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this