943,962 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8149
  • C RSS
Feb 25th, 2006
1

Problem Creating Window

Expand Post »
First, I prepared the class blow :
  1. class UserDefinedWindow
  2. {
  3. public:
  4. WNDCLASSEX WindowClass;
  5.  
  6. DWORD dwExStyle;
  7. LPCTSTR lpClassName;
  8. LPCTSTR lpWindowName;
  9. DWORD dwStyle;
  10. int x;
  11. int y;
  12. int nWidth;
  13. int nHeight;
  14. HWND hWndParent;
  15. HMENU hMenu;
  16. HINSTANCE hProgInst;
  17. LPVOID lpParam;
  18. char ClassName[MAX_LOADSTRING];
  19. char Caption[MAX_LOADSTRING];
  20. HWND hWnd;
  21.  
  22. UserDefinedWindow()
  23. {
  24. dwExStyle = 0; //These are default parameters for CreateWindowEx()
  25. lpClassName = "ClassName";
  26. lpWindowName = "Caption";
  27. dwStyle = WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE;
  28. x = 100;
  29. y = 100;
  30. nWidth = 100;
  31. nHeight = 100;
  32. hWndParent = HWND_DESKTOP;
  33. hMenu = NULL;
  34. hProgInst = hInstance;
  35. lpParam = NULL;
  36.  
  37. WindowClass.cbSize = (UINT) sizeof(WNDCLASSEX); //Defaults for WNDCLASSEX
  38. WindowClass.style = (UINT) CS_DBLCLKS;
  39. WindowClass.lpfnWndProc = (WNDPROC) WindowProcedure;
  40. WindowClass.cbClsExtra = (INT) 0;
  41. WindowClass.cbWndExtra = (INT) 0;
  42. WindowClass.hInstance = (HINSTANCE) hProgInst;
  43. WindowClass.hIcon = (HICON) LoadIcon (NULL, IDI_APPLICATION);
  44. WindowClass.hCursor = (HCURSOR) LoadCursor (NULL, IDC_ARROW);
  45. WindowClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  46. WindowClass.lpszMenuName = (LPCTSTR) NULL;
  47. WindowClass.lpszClassName = (LPCTSTR) ClassName;
  48. WindowClass.hIconSm = (HICON) LoadIcon (NULL, IDI_APPLICATION);
  49. }
  50. void Create(void)
  51. {
  52. if (!RegisterClassEx(&WindowClass)) MessageBox(NULL, "Error registering.", "Error", MB_OK);
  53. hWnd = CreateWindowEx( (DWORD) dwExStyle,
  54. (LPCTSTR) lpClassName,
  55. (LPCTSTR) lpWindowName,
  56. (DWORD) dwStyle,
  57. (int) x,
  58. (int) y,
  59. (int) nWidth,
  60. (int) nHeight,
  61. (HWND) hWndParent,
  62. (HMENU) hMenu,
  63. (HINSTANCE) hProgInst,
  64. (LPVOID) lpParam);
  65. }
  66. };

And the main() block of the program is...
  1. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
  2. {
  3. hInstance = hInst;
  4. UserDefinedWindow MainWnd;
  5. MainWnd.Create();
  6.  
  7. while (GetMessage(&Msg, NULL, 0, 0))
  8. {
  9. TranslateMessage(&Msg);
  10. DispatchMessage(&Msg);
  11. }
  12. return (INT) Msg.wParam;
  13. }

And finally the problem is, nothing appears. Program seems to be running, but no window appears on the screen. There is no errors or even warnings given.

Can you give an idea why the window doesn't pop up?

Notes
-----
-Compiler is Visual Studio 2005.
-I didn't use ShowWindow(), because WS_VISIBLE already exists in dwStyle.
-hInstance is global defined variable.
Similar Threads
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Feb 26th, 2006
0

Re: Problem Creating Window

This part returns NULL.
  1. hWnd = CreateWindowEx( (DWORD) dwExStyle,
  2. (LPCTSTR) lpClassName,
  3. (LPCTSTR) lpWindowName,
  4. (DWORD) dwStyle,
  5. (int) x,
  6. (int) y,
  7. (int) nWidth,
  8. (int) nHeight,
  9. (HWND) hWndParent,
  10. (HMENU) hMenu,
  11. (HINSTANCE) hProgInst,
  12. (LPVOID) lpParam);
  13. if ( hWnd == NULL )
  14. {
  15. MessageBox(NULL, "CreateWindow Returned NULL", "Error", MB_OK );
  16. return;
  17. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 26th, 2006
0

Re: Problem Creating Window

Thanks for your help.
It OK now!

  1. VOID ErrorTest(LPSTR lpzsErrorText);
  2. VOID ErrorTest(INT nText);
  3. class UserDefinedWindow
  4. {
  5. public:
  6. WNDCLASSEX WindowClass;
  7.  
  8. DWORD dwExStyle;
  9. //LPCTSTR lpClassName;
  10. //LPCTSTR lpWindowName;
  11. char lpClassName[MAX_LOADSTRING];
  12. char lpWindowName[MAX_LOADSTRING];
  13. DWORD dwStyle;
  14. int x;
  15. int y;
  16. int nWidth;
  17. int nHeight;
  18. HWND hWndParent;
  19. HMENU hMenu;
  20. HINSTANCE hProgInst;
  21. LPVOID lpParam;
  22. HWND hWnd;
  23.  
  24. UserDefinedWindow()
  25. {
  26. dwExStyle = 0; //These are default parameters for CreateWindowEx()
  27. strcpy(lpClassName, "ClassName");
  28. strcpy(lpWindowName, "Caption");
  29. dwStyle = WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE;
  30. x = 100;
  31. y = 100;
  32. nWidth = 200;
  33. nHeight = 200;
  34. hWndParent = HWND_DESKTOP;
  35. hMenu = NULL;
  36. hProgInst = hInstance;
  37. lpParam = NULL;
  38.  
  39. WindowClass.cbSize = (UINT) sizeof(WNDCLASSEX); //Defaults for WNDCLASSEX
  40. WindowClass.style = (UINT) CS_DBLCLKS;
  41. WindowClass.lpfnWndProc = (WNDPROC) WindowProcedure;
  42. WindowClass.cbClsExtra = (INT) 0;
  43. WindowClass.cbWndExtra = (INT) 0;
  44. WindowClass.hInstance = (HINSTANCE) hProgInst;
  45. WindowClass.hIcon = (HICON) LoadIcon (NULL, IDI_APPLICATION);
  46. WindowClass.hCursor = (HCURSOR) LoadCursor (NULL, IDC_ARROW);
  47. WindowClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  48. WindowClass.lpszMenuName = (LPCTSTR) NULL;
  49. WindowClass.lpszClassName = (LPCTSTR) lpClassName;
  50. WindowClass.hIconSm = (HICON) LoadIcon (NULL, IDI_APPLICATION);
  51. }
  52. void Create(void)
  53. {
  54. if (!RegisterClassEx(&WindowClass)) MessageBox(NULL, "Error registering.", "Error", MB_OK);
  55. hWnd = CreateWindowEx( (DWORD) dwExStyle,
  56. (LPCTSTR) lpClassName,
  57. (LPCTSTR) lpWindowName,
  58. (DWORD) dwStyle,
  59. (int) x,
  60. (int) y,
  61. (int) nWidth,
  62. (int) nHeight,
  63. (HWND) hWndParent,
  64. (HMENU) hMenu,
  65. (HINSTANCE) hProgInst,
  66. (LPVOID) lpParam);
  67. if (hWnd == NULL)
  68. {
  69. //1407 Cannot find window class. ERROR_CANNOT_FIND_WND_CLASS
  70. DWORD ErrCode = GetLastError();
  71. MessageBox(NULL, "Error creating.", "Error", MB_OK);
  72. ErrorTest(ErrCode);
  73. }
  74. }
  75. };
  76. VOID ErrorTest(LPSTR lpzsErrorText)
  77. {
  78. if (MessageBox(NULL, lpzsErrorText, "Error", MB_ICONERROR | MB_OKCANCEL)==IDCANCEL);//PostQuitMessage(0);
  79. }
  80. VOID ErrorTest(INT nText)
  81. {
  82. CHAR lpzsErrorText[32];
  83. _itoa(nText, lpzsErrorText, 10);
  84. if (MessageBox(NULL, lpzsErrorText, "Error", MB_ICONERROR | MB_OKCANCEL)==IDCANCEL);//PostQuitMessage(0);
  85. }

ClassName was defined as the name of the class, but in the WNDCLASSEX structure, lpClassName was used.
So, because of not matching the class names in WNDCLASSEX and CreateWindowEx(), the system returned the error :
Error Code :1407
Meaning :Cannot find window class.
Constant :ERROR_CANNOT_FIND_WND_CLASS

Now the code is error-free.
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Dec 28th, 2008
0

Re: Problem Creating Window

Peace...
Can anyone explain that more, step by step.
EZO
Reputation Points: 2
Solved Threads: 0
Newbie Poster
EZO is offline Offline
6 posts
since Dec 2008
Dec 28th, 2008
0

Re: Problem Creating Window

Click to Expand / Collapse  Quote originally posted by EZO ...
Peace...
Can anyone explain that more, step by step.
First, you read the date that conversation was initiated.
Second, you start a new thread, making sure you have done your part to follow the rules of posting.
Third, you link or point to the old conversation, if it is relevant to what you are going to ask.
Fourth, you ask in a clear and intelligent way, what your question is, and how you would like to obtain help.
Fifth, you wait patiently, for someone to be gracious enough to take the time to respond.
Sixth, you show gratitude, and mark the post solved if your problem has been resolved or question has been answered satisfactory.

[Disclaimer] Steps are only a guide to understanding, and not absolute
nor authoritative in nature. Differences in personality may or may not
lengthen or shorten the measure of the stride.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 30th, 2008
0

Re: Problem Creating Window

Thaks for help, I'll fix that....
EZO
Reputation Points: 2
Solved Threads: 0
Newbie Poster
EZO is offline Offline
6 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Algorithm for EBCDIC to ASCII Conversion
Next Thread in C Forum Timeline: hi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC