Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,203 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,440 people online right now. Registration is fast and FREE... Join Now!





Simple Window Template

You can use this as a template for your Win32 Programs.

Submitted By: Mrafcho001
Actions:
Rating:
Views: 4,996

Language: C++

Last Modified: December 17, 2005
Instructions: Copy and Paste

Snippet


  1. #include <windows.h>
  2.  
  3.  
  4. // Step 4: the Window Procedure
  5. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  6. {
  7.      switch(msg)
  8.      {
  9.      case WM_CREATE:
  10.  
  11.           break;
  12.           case WM_CLOSE:
  13.                DestroyWindow(hwnd);
  14.           break;
  15.           case WM_DESTROY:
  16.                PostQuitMessage(0);
  17.           break;
  18.           default:
  19.                return DefWindowProc(hwnd, msg, wParam, lParam);
  20.      }
  21.      return 0;
  22. }
  23.  
  24. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  25.                          LPSTR lpCmdLine, int nCmdShow)
  26. {
  27.      WNDCLASSEX wc;
  28.      HWND hwnd;
  29.      MSG Msg;
  30.      static char appName[] = "Your Application";
  31.  
  32.      //Step 1: Registering the Window Class
  33.      wc.cbSize           = sizeof(WNDCLASSEX);
  34.      wc.style           = CS_HREDRAW | CS_VREDRAW;
  35.      wc.lpfnWndProc      = WndProc;
  36.      wc.cbClsExtra      = 0;
  37.      wc.cbWndExtra      = 0;
  38.      wc.hInstance      = hInstance;
  39.      wc.hIcon           = LoadIcon(NULL, IDI_APPLICATION);
  40.      wc.hCursor           = LoadCursor(NULL, IDC_ARROW);
  41.      wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.      wc.lpszMenuName  = NULL;
  43.      wc.lpszClassName = appName;
  44.      wc.hIconSm           = LoadIcon(NULL, IDI_APPLICATION);
  45.  
  46.      if(!RegisterClassEx(&wc))
  47.      {
  48.           MessageBox(NULL, "Window Registration Failed!", "Error!",
  49.                MB_ICONERROR | MB_OK);
  50.           return 0;
  51.      }
  52.  
  53.      // Step 2: Creating the Window
  54.      hwnd = CreateWindowEx(
  55.           WS_EX_CLIENTEDGE,
  56.           appName,
  57.           "Your Window Title",
  58.           WS_OVERLAPPEDWINDOW,
  59.           CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
  60.           NULL, NULL, hInstance, NULL);
  61.  
  62.      if(hwnd == NULL)
  63.      {
  64.           MessageBox(NULL, "Window Creation Failed!", "Error",
  65.                MB_ICONERROR | MB_OK);
  66.           return 0;
  67.      }
  68.  
  69.      ShowWindow(hwnd, nCmdShow);
  70.      UpdateWindow(hwnd);
  71.  
  72.      // Step 3: The Message Loop
  73.      while(GetMessage(&Msg, NULL, 0, 0) > 0)
  74.      {
  75.           TranslateMessage(&Msg);
  76.           DispatchMessage(&Msg);
  77.      }
  78.      return int(Msg.wParam);
  79. }

Copy & Paste


Comments


Pat234 2008-11-05 22:57:07

instead of stealing from the tutorial (without proper credit :( ) link to original: http://www.winprog.org/tutorial/simple_window.html


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month