Advertisement
HEX0x29A

StatusBar - WinApi

Aug 16th, 2020
5,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma comment(linker,"/MERGE:.rdata=.text")
  2. #pragma comment(linker,"/MERGE:.data=.text")
  3. #pragma comment(linker,"/SECTION:.text,ERW,512")
  4. #pragma comment(linker,"/Od")
  5. #pragma comment(linker,"/CLRUNMANAGEDCODECHECK:NO")
  6. #pragma comment(linker,"/MANIFEST:NO")
  7. #pragma comment(linker,"/MANIFESTUAC:NO")
  8. #pragma comment(linker,"/GS-")
  9. #pragma comment(linker,"/PDBPATH:NONE")
  10. #pragma comment(linker,"/DYNAMICBASE:NO")
  11. #pragma comment(linker,"/ENTRY:WinMain")
  12. #pragma comment(lib, "Comctl32.lib")
  13. #include <windows.h>
  14. #include <commctrl.h>
  15.  
  16. /* This is where all the input to the window goes to */
  17. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
  18.     switch (Message) {
  19.  
  20.         /* Upon destruction, tell the main thread to stop */
  21.     case WM_DESTROY: {
  22.         PostQuitMessage(0);
  23.         break;
  24.     }
  25.  
  26.                    /* All other messages (a lot of them) are processed using default procedures */
  27.     default:
  28.         return DefWindowProc(hwnd, Message, wParam, lParam);
  29.     }
  30.     return 0;
  31. }
  32.  
  33. // Description:
  34. //   Creates a status bar and divides it into the specified number of parts.
  35. // Parameters:
  36. //   hwndParent - parent window for the status bar.
  37. //   idStatus - child window identifier of the status bar.
  38. //   hinst - handle to the application instance.
  39. //   cParts - number of parts into which to divide the status bar.
  40. // Returns:
  41. //   The handle to the status bar.
  42. //
  43. HWND DoCreateStatusBar(HWND hwndParent, int idStatus, HINSTANCE
  44.     hinst, int cParts)
  45. {
  46.     HWND hwndStatus;
  47.     RECT rcClient;
  48.     HLOCAL hloc;
  49.     PINT paParts;
  50.     int i, nWidth;
  51.  
  52.     // Ensure that the common control DLL is loaded.
  53.     InitCommonControls();
  54.  
  55.     // Create the status bar.
  56.     hwndStatus = CreateWindowEx(
  57.         0,                       // no extended styles
  58.         STATUSCLASSNAME,         // name of status bar class
  59.         (PCTSTR)NULL,           // no text when first created
  60.         SBARS_SIZEGRIP |         // includes a sizing grip
  61.         WS_CHILD | WS_VISIBLE,   // creates a visible child window
  62.         0, 0, 0, 0,              // ignores size and position
  63.         hwndParent,              // handle to parent window
  64.         (HMENU)idStatus,       // child window identifier
  65.         hinst,                   // handle to application instance
  66.         NULL);                   // no window creation data
  67.  
  68.     // Get the coordinates of the parent window's client area.
  69.     GetClientRect(hwndParent, &rcClient);
  70.  
  71.     // Allocate an array for holding the right edge coordinates.
  72.     hloc = LocalAlloc(LHND, sizeof(int) * cParts);
  73.     paParts = (PINT)LocalLock(hloc);
  74.  
  75.     // Calculate the right edge coordinate for each part, and
  76.     // copy the coordinates to the array.
  77.     nWidth = rcClient.right / cParts;
  78.     int rightEdge = nWidth;
  79.     for (i = 0; i < cParts; i++) {
  80.         paParts[i] = rightEdge;
  81.         rightEdge += nWidth;
  82.     }
  83.  
  84.     // Tell the status bar to create the window parts.
  85.     SendMessage(hwndStatus, SB_SETPARTS, (WPARAM)cParts, (LPARAM)
  86.         paParts);
  87.  
  88.     // Free the array, and return.
  89.     LocalUnlock(hloc);
  90.     LocalFree(hloc);
  91.     return hwndStatus;
  92. }
  93.  
  94. /* The 'main' function of Win32 GUI programs: this is where execution starts */
  95. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  96.     WNDCLASSEX wc; /* A properties struct of our window */
  97.     HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
  98.     MSG msg; /* A temporary location for all messages */
  99.  
  100.     /* zero out the struct and set the stuff we want to modify */
  101.     memset(&wc, 0, sizeof(wc));
  102.     wc.cbSize = sizeof(WNDCLASSEX);
  103.     wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
  104.     wc.hInstance = hInstance;
  105.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  106.  
  107.     /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
  108.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  109.     wc.lpszClassName = "WindowClass";
  110.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
  111.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
  112.  
  113.     if (!RegisterClassEx(&wc)) {
  114.         MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  115.         return 0;
  116.     }
  117.  
  118.     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", "Caption", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
  119.         CW_USEDEFAULT, /* x */
  120.         CW_USEDEFAULT, /* y */
  121.         400, /* width */
  122.         200, /* height */
  123.         NULL, NULL, hInstance, NULL);
  124.  
  125.     if (hwnd == NULL) {
  126.         MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  127.         return 0;
  128.     }
  129.     DoCreateStatusBar(hwnd, NULL, hInstance, 4);
  130.     /*
  131.         This is the heart of our program where all input is processed and
  132.         sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
  133.         this loop will not produce unreasonably high CPU usage
  134.     */
  135.     while (GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
  136.         TranslateMessage(&msg); /* Translate key codes to chars if present */
  137.         DispatchMessage(&msg); /* Send it to WndProc */
  138.     }
  139.     return msg.wParam;
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement