944,120 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7417
  • C++ RSS
Jul 22nd, 2005
0

?A Simple Win32 GUI Introduction Snippet?

Expand Post »
Hello,

I am just messin around with this "A Simple Win32 GUI Introduction" snippet I found on this site. I am able to paste it in to a simple project in my Visual C++ application and run it. Basically it is just a blank window. It says you can play around with it, and I did figure out how to change the Title. But what else can we do with this? I have a simple console application that I wrote that calculates two resistors in parallel.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int R1, R2;
  7. float Rpar;
  8. cout << "Enter the value for R1: " ;
  9. cin >> R1;
  10. cout << "Enter the value for R2: ";
  11. cin >> R2;
  12. Rpar = (R1 * R2) / (R1 + R2);
  13. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  14. system ("pause");
  15. return 0;
  16.  
  17. }

Here is the Snippet

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <windows.h> /* Windows header */
  3.  
  4. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  5. char szClassName[ ] = "WindowsApp"; /* Class ID */
  6.  
  7. int WINAPI
  8. WinMain (HINSTANCE hThisInstance,
  9. HINSTANCE hPrevInstance,
  10. LPSTR lpszArgument,
  11. int nFunsterStil)
  12.  
  13. {
  14. HWND hwnd;
  15. MSG messages;
  16. WNDCLASSEX wincl;
  17.  
  18. wincl.hInstance = hThisInstance;
  19. wincl.lpszClassName = szClassName;
  20. wincl.lpfnWndProc = WindowProcedure; /* See end of code */
  21. wincl.style = CS_DBLCLKS;
  22. wincl.cbSize = sizeof (WNDCLASSEX);
  23. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  24. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  25. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  26. wincl.lpszMenuName = NULL;
  27. wincl.cbClsExtra = 0;
  28. wincl.cbWndExtra = 0;
  29. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Default windows background colour */
  30.  
  31. if (!RegisterClassEx (&wincl))
  32. return 0;
  33.  
  34. hwnd = CreateWindowEx (
  35. 0,
  36. szClassName, /* Classname */
  37. "Windows App", /* Title Text */
  38. WS_OVERLAPPEDWINDOW,
  39. CW_USEDEFAULT, /* Default x... */
  40. CW_USEDEFAULT, /* ...and default y position of window */
  41. 640, /* The programs width... */
  42. 480, /* ...and height in pixels */
  43. HWND_DESKTOP,
  44. NULL,
  45. hThisInstance,
  46. NULL
  47. );
  48.  
  49. /* Make the window visible on the screen */
  50. ShowWindow (hwnd, nFunsterStil);
  51. while (GetMessage (&messages, NULL, 0, 0))
  52. {
  53. TranslateMessage(&messages);
  54. DispatchMessage(&messages);
  55. }
  56.  
  57. /* Return: wParam from a quit message usually = 0 */
  58. return messages.wParam;
  59. }
  60.  
  61. LRESULT CALLBACK
  62. WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  63. { /* Called when window processes a message */
  64. switch (message)
  65. {
  66. case WM_DESTROY: /* Destoy message: called if you press the "x" in the top right corner */
  67. PostQuitMessage (0); /* Send a message to quit */
  68. break;
  69. default:
  70. return DefWindowProc (hwnd, message, wParam, lParam);
  71. }
  72. return 0;
  73. }


Can this be incorporated into this window, and not disappear after hitting any key?

Just playin around. Oh Yes! How do you change the background color?

any help would be appreciated

ThanX BandM
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bandm is offline Offline
19 posts
since Jun 2005
Jul 22nd, 2005
0

Re: ?A Simple Win32 GUI Introduction Snippet?

Just a hint, on your parallel resistor calculator declare both R1 and R2 as a float or something like:

R1 = 1
R2 = 2

will give you a parallel resistance of 0

BTW, the Windows snippet you have is the one that comes up with DevCpp when you create a Windows Application. You need to add EditBoxes for data entry and output, also a button to click to do your calculations. The easiest way to do all of this is with a form builder in visual studio. The problem there is, that your total code is now scattered across several goofy sounding files.

Plotzki schmotzki!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 23rd, 2005
0

Re: ?A Simple Win32 GUI Introduction Snippet?

A very good tutorial about Windows GUI programming for the beginner is at:
http://www.functionx.com/win32/
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

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: c doubts
Next Thread in C++ Forum Timeline: Subclassed Editbox Control: Do I need to create 2 subclasses for two edit controls?





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


Follow us on Twitter


© 2011 DaniWeb® LLC