User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,505 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,767 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Dec 18th, 2004
Views: 12,177
An introduction to the Win32 GUI. The code creates a simple window that you can play with. Many parameters can be changed if you like! The unusual thing about windows apps is that rather than directly handling inputs (ie the position of the mouse / what control has focus ect...) you process window messages. Whenever something happens, it sends a message to the window making a message queue. DispatchMessage() calls WindowProcedure (which if you look is assigned to be the default message handler for the class name ("WindowsApp") of which the window belongs. The good thing is that as all messages are of the same structure it is easy to get information about what happened (lParam/wParam) and take the correct action (switch statement). Enjoy!
cplusplus Syntax | 4 stars
  1. #include <windows.h> /* Windows header */
  2.  
  3. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  4. char szClassName[ ] = "WindowsApp"; /* Class ID */
  5.  
  6. int WINAPI
  7. WinMain (HINSTANCE hThisInstance,
  8. HINSTANCE hPrevInstance,
  9. LPSTR lpszArgument,
  10. int nFunsterStil)
  11.  
  12. {
  13. HWND hwnd;
  14. MSG messages;
  15. WNDCLASSEX wincl;
  16.  
  17. wincl.hInstance = hThisInstance;
  18. wincl.lpszClassName = szClassName;
  19. wincl.lpfnWndProc = WindowProcedure; /* See end of code */
  20. wincl.style = CS_DBLCLKS;
  21. wincl.cbSize = sizeof (WNDCLASSEX);
  22. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  23. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  24. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  25. wincl.lpszMenuName = NULL;
  26. wincl.cbClsExtra = 0;
  27. wincl.cbWndExtra = 0;
  28. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Default windows background colour */
  29.  
  30. if (!RegisterClassEx (&wincl))
  31. return 0;
  32.  
  33. hwnd = CreateWindowEx (
  34. 0,
  35. szClassName, /* Classname */
  36. "Windows App", /* Title Text */
  37. WS_OVERLAPPEDWINDOW,
  38. CW_USEDEFAULT, /* Default x... */
  39. CW_USEDEFAULT, /* ...and default y position of window */
  40. 640, /* The programs width... */
  41. 480, /* ...and height in pixels */
  42. HWND_DESKTOP,
  43. NULL,
  44. hThisInstance,
  45. NULL
  46. );
  47.  
  48. /* Make the window visible on the screen */
  49. ShowWindow (hwnd, nFunsterStil);
  50. while (GetMessage (&messages, NULL, 0, 0))
  51. {
  52. TranslateMessage(&messages);
  53. DispatchMessage(&messages);
  54. }
  55.  
  56. /* Return: wParam from a quit message usually = 0 */
  57. return messages.wParam;
  58. }
  59.  
  60. LRESULT CALLBACK
  61. WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  62. { /* Called when window processes a message */
  63. switch (message)
  64. {
  65. case WM_DESTROY: /* Destoy message: called if you press the "x" in the top right corner */
  66. PostQuitMessage (0); /* Send a message to quit */
  67. break;
  68. default:
  69. return DefWindowProc (hwnd, message, wParam, lParam);
  70. }
  71. return 0;
  72. }
Comments (Newest First)
bumsfeld | Posting Shark | Aug 8th, 2005
I think FunsterStil is Danish for WindowStyle.
Dae | Newbie Poster | Jun 12th, 2005
nFunsterStil is just a local variable name they used down at 'ShowWindow (hwnd, nFunsterStil);', meaning upon calling the fuction you could use that as an arguement spot... but the name itself doesnt mean anything apprently.

Good way for them to tell who is using their code though, just search google for that variable name they've come up with and put as a temple on their program.
1o0oBhP | Posting Pro in Training | Mar 5th, 2005
DevC++ using mingw32 compiler. I think its the other way round as most MS users I have seen have got all sorts of auto-generated things to adjust the code! The brush issue should work as COLOR_BACKGROUND is independant of the compiler and is defined in the Win32 API reference, unless VC++ modifies it somewhere else. WindowProcedure is going to need a cast most of the time (it is c++ after all!) but this compiles ok for me. This is a modified DevC++ template sample. DevC++ can also handle not defining WindowProcedure and having the defininition / implementation all in one go provided it is before main (where it is first used). Some compilers I have seen REQUIRE you to define a function first, wierd no?
Tight_Coder_Ex | Junior Poster | Feb 7th, 2005
I am curious as to which compiler you used to create the executable. In know with VC++, not typecasting WindowProcedure will cause and error.

Secondly, why MS$ has done this I'll never know, but not adding 1 to the desired background color will yeild the wrong color. I must use HBRUSH (COLOR_BACKGROUND + 1) with VC++.

Maybe the headers you use have the background colors adjusted already.
1o0oBhP | Posting Pro in Training | Dec 20th, 2004
DevC++ Template gives you it!!! dunno what it does but i will find out...
vegaseat | Kickbutt Moderator | Dec 20th, 2004
nFunsterStil? The folks at Dev C++ use it too! Wonder what that means?
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC