arcanosam

BUILDING NATIVE WINDOWS APPLICATIONS VS2015 COMMUNITY

Oct 16th, 2015
236
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * This code belongs to Bonafide Ideas blog
  3. * source: http://www.angelhernandezm.com/building-native-windows-applications-with-clion-gcc-and-cygwin/
  4. *
  5. * I just fix some casts and the lambda instruction.
  6. * I'm using Visual Studio 2015 Community
  7. * I was bit curious to see running in VS...
  8. *
  9. */
  10.  
  11. #include <windows.h>
  12.  
  13. HINSTANCE  hInst;
  14. const char g_szClassName[] = "bonafideideasWindowClass";
  15.  
  16. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  17.     LPSTR lpCmdLine, int nCmdShow) {
  18.     MSG Msg;
  19.     HWND hwnd;
  20.     WNDCLASSEX wc;
  21.  
  22.     wc.lpszMenuName = NULL;
  23.     wc.hInstance = hInstance;
  24.     wc.lpszClassName = (LPCWSTR) g_szClassName;
  25.     wc.cbSize = sizeof(WNDCLASSEX);
  26.     wc.cbClsExtra = wc.cbWndExtra = 0;
  27.     wc.style = CS_HREDRAW | CS_VREDRAW;
  28.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  29.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  30.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  31.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  32.     wc.lpfnWndProc = [](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) -> LRESULT {
  33.         HDC hdc;
  34.         RECT rect;
  35.         // TEXTMETRIC tm;
  36.         PAINTSTRUCT ps;
  37.  
  38.         switch (msg) {
  39.         case WM_CLOSE:
  40.             if (MessageBox(NULL, (LPCWSTR)"Are you sure you want to quit?", (LPCWSTR)"Confirmation", MB_ICONQUESTION | MB_YESNO) == IDYES)
  41.                 DestroyWindow(hwnd);
  42.             break;
  43.  
  44.         case WM_DESTROY:
  45.             PostQuitMessage(0);
  46.             break;
  47.  
  48.         case WM_PAINT:
  49.             hdc = BeginPaint(hwnd, &ps);
  50.             GetClientRect(hwnd, &rect);
  51.             DrawText(hdc, TEXT("Native Windows Development with CygWin and CLion."),
  52.                 -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  53.             EndPaint(hwnd, &ps);
  54.             return 0;
  55.  
  56.         default:
  57.             return DefWindowProc(hwnd, msg, wParam, lParam);
  58.         }
  59.  
  60.         return 0;
  61.     };
  62.  
  63.  
  64.     if (!RegisterClassEx(&wc)) {
  65.         MessageBox(NULL, (LPCWSTR)"Window Registration Failed", (LPCWSTR)"Error", MB_ICONEXCLAMATION | MB_OK);
  66.         return 0;
  67.     }
  68.  
  69.     hwnd = CreateWindowEx(
  70.         WS_EX_CLIENTEDGE,
  71.         (LPCWSTR)g_szClassName,
  72.         (LPCWSTR)"Simplest Windows Native App built with CLion",
  73.         WS_OVERLAPPEDWINDOW,
  74.         CW_USEDEFAULT, CW_USEDEFAULT, 500, 250,
  75.         NULL, NULL, hInstance, NULL);
  76.  
  77.     if (hwnd == NULL) {
  78.         MessageBox(NULL, (LPCWSTR)"Window Creation Failed", (LPCWSTR)"Error", MB_ICONEXCLAMATION | MB_OK);
  79.         return 0;
  80.     }
  81.  
  82.     ShowWindow(hwnd, nCmdShow);
  83.     UpdateWindow(hwnd);
  84.  
  85.     while (GetMessage(&Msg, NULL, 0, 0) > 0) {
  86.         TranslateMessage(&Msg);
  87.         DispatchMessage(&Msg);
  88.     }
  89.     return Msg.wParam;
  90. }
RAW Paste Data