vovan333

HEOL WROLD IN WINDOWS API, OVER 100 LINES OF CODE

Aug 5th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GT_HelloWorldWin32.cpp
  2. // compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
  3.  
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <tchar.h>
  8.  
  9. // Global variables
  10.  
  11. // The main window class name.
  12. static TCHAR szWindowClass[] = _T("win32app");
  13.  
  14. // The string that appears in the application's title bar.
  15. static TCHAR szTitle[] = _T("Win32 Guided Tour Application");
  16.  
  17. HINSTANCE hInst;
  18.  
  19. // Forward declarations of functions included in this code module:
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21.  
  22. int WINAPI WinMain(HINSTANCE hInstance,
  23.                    HINSTANCE hPrevInstance,
  24.                    LPSTR lpCmdLine,
  25.                    int nCmdShow)
  26. {
  27.     WNDCLASSEX wcex;
  28.  
  29.     wcex.cbSize = sizeof(WNDCLASSEX);
  30.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  31.     wcex.lpfnWndProc    = WndProc;
  32.     wcex.cbClsExtra     = 0;
  33.     wcex.cbWndExtra     = 0;
  34.     wcex.hInstance      = hInstance;
  35.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  36.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  37.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  38.     wcex.lpszMenuName   = NULL;
  39.     wcex.lpszClassName  = szWindowClass;
  40.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  41.  
  42.     if (!RegisterClassEx(&wcex))
  43.     {
  44.         MessageBox(NULL,
  45.             _T("Call to RegisterClassEx failed!"),
  46.             _T("Win32 Guided Tour"),
  47.             NULL);
  48.  
  49.         return 1;
  50.     }
  51.  
  52.     hInst = hInstance; // Store instance handle in our global variable
  53.  
  54.     // The parameters to CreateWindow explained:
  55.     // szWindowClass: the name of the application
  56.     // szTitle: the text that appears in the title bar
  57.     // WS_OVERLAPPEDWINDOW: the type of window to create
  58.     // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
  59.     // 500, 100: initial size (width, length)
  60.     // NULL: the parent of this window
  61.     // NULL: this application does not have a menu bar
  62.     // hInstance: the first parameter from WinMain
  63.     // NULL: not used in this application
  64.     HWND hWnd = CreateWindow(
  65.         szWindowClass,
  66.         szTitle,
  67.         WS_OVERLAPPEDWINDOW,
  68.         CW_USEDEFAULT, CW_USEDEFAULT,
  69.         500, 100,
  70.         NULL,
  71.         NULL,
  72.         hInstance,
  73.         NULL
  74.     );
  75.  
  76.     if (!hWnd)
  77.     {
  78.         MessageBox(NULL,
  79.             _T("Call to CreateWindow failed!"),
  80.             _T("Win32 Guided Tour"),
  81.             NULL);
  82.  
  83.         return 1;
  84.     }
  85.  
  86.     // The parameters to ShowWindow explained:
  87.     // hWnd: the value returned from CreateWindow
  88.     // nCmdShow: the fourth parameter from WinMain
  89.     ShowWindow(hWnd,
  90.         nCmdShow);
  91.     UpdateWindow(hWnd);
  92.  
  93.     // Main message loop:
  94.     MSG msg;
  95.     while (GetMessage(&msg, NULL, 0, 0))
  96.     {
  97.         TranslateMessage(&msg);
  98.         DispatchMessage(&msg);
  99.     }
  100.  
  101.     return (int) msg.wParam;
  102. }
  103.  
  104. //
  105. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  106. //
  107. //  PURPOSE:  Processes messages for the main window.
  108. //
  109. //  WM_PAINT    - Paint the main window
  110. //  WM_DESTROY  - post a quit message and return
  111. //
  112. //
  113. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  114. {
  115.     PAINTSTRUCT ps;
  116.     HDC hdc;
  117.     TCHAR greeting[] = _T("Hello, World!");
  118.  
  119.     switch (message)
  120.     {
  121.     case WM_PAINT:
  122.         hdc = BeginPaint(hWnd, &ps);
  123.  
  124.         // Here your application is laid out.
  125.         // For this introduction, we just print out "Hello, World!"
  126.         // in the top left corner.
  127.         TextOut(hdc,
  128.             5, 5,
  129.             greeting, _tcslen(greeting));
  130.         // End application-specific layout section.
  131.  
  132.         EndPaint(hWnd, &ps);
  133.         break;
  134.     case WM_DESTROY:
  135.         PostQuitMessage(0);
  136.         break;
  137.     default:
  138.         return DefWindowProc(hWnd, message, wParam, lParam);
  139.         break;
  140.     }
  141.  
  142.     return 0;
  143. }
Add Comment
Please, Sign In to add comment