Ham62

WinAPI Resizing Font.c

Jul 8th, 2019
810
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2.  
  3. LRESULT CALLBACK WindowProc(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
  4. {
  5.     static HWND text;
  6.     static HFONT hfont;
  7.    
  8.     switch (msg) {
  9.     case WM_SIZE:
  10.         {            
  11.             RECT rc;
  12.             GetClientRect(window, &rc);
  13.  
  14.             MoveWindow(text, 0, 0, rc.right, rc.bottom, TRUE);
  15.  
  16.             HDC hDC = GetDC(window);
  17.             hfont = CreateFont(
  18.                     -MulDiv(rc.bottom / 2, GetDeviceCaps(hDC, LOGPIXELSY), 72),
  19.                     -MulDiv(rc.bottom / 25, GetDeviceCaps(hDC, LOGPIXELSY), 72),
  20.                     0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
  21.                     OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  22.                     DEFAULT_PITCH, "Verdana");
  23.  
  24.             ReleaseDC(window, hDC);
  25.            
  26.             SendMessage(text, WM_SETFONT, (WPARAM)hfont, TRUE);
  27.             DeleteObject(hfont);
  28.            
  29.         } break;
  30.     case WM_CREATE:
  31.         {
  32.             RECT rc;
  33.             CREATESTRUCT cs = *((CREATESTRUCT *) lparam);
  34.             GetClientRect(window, &rc);
  35.            
  36.             text = CreateWindow(
  37.                     "STATIC",
  38.                     "THIS IS A MEMORY LEAK TEST",
  39.                     (WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_CENTER),
  40.                     0, 0, rc.right, rc.bottom,
  41.                     window, 0, cs.hInstance, 0);
  42.         } break;
  43.     case WM_DESTROY:
  44.         {
  45.             PostQuitMessage(0);
  46.         } break;
  47.     }
  48.  
  49.     return DefWindowProc(window, msg, wparam, lparam);
  50. }
  51.  
  52. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CmdLine, int nCmdLine)
  53. {
  54.     const char *CLASS_NAME = "Font Test";
  55.     WNDCLASS wc = {0};
  56.  
  57.     wc.style = CS_HREDRAW | CS_VREDRAW;
  58.     wc.lpfnWndProc = WindowProc;
  59.     wc.hInstance = hInstance;
  60.     wc.hbrBackground = CreateSolidBrush(RGB(0xF0, 0xF0, 0xF0));
  61.     wc.lpszClassName = CLASS_NAME;
  62.  
  63.     RegisterClass(&wc);
  64.    
  65.     HWND window = CreateWindow(
  66.                 CLASS_NAME,
  67.                 "Font Test",
  68.                 WS_OVERLAPPEDWINDOW,
  69.                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  70.                 0, 0, hInstance, 0);
  71.  
  72.     if (!window)
  73.         return 0;
  74.  
  75.     ShowWindow(window, nCmdLine);
  76.    
  77.     MSG msg;
  78.     while (GetMessage(&msg, NULL, 0, 0)) {
  79.         TranslateMessage(&msg);
  80.         DispatchMessage(&msg);
  81.     }
  82.    
  83.     return 0;
  84. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×