Aumentar e diminuir o tamanho da janela(hwnd) ao clicar nos botões hwndLarger e hwndSmaller - Código revisado em 27/08/2022

main.html
/**
Código revisado em 27/08/2022
*/

#include <windows.h>

#define ID_SMALLER 1
#define ID_LARGER  2
#define BTN_WIDTH  (8 * cxChar)
#define BTN_HEIGHT (4 * cyChar)
#define TITULO_DA_JANELA "Aumentar e diminuir o tamanho da janela"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, PSTR szC, int iCS) {
   static TCHAR szAppName[] = TEXT("OwnDraw");
   MSG msg;
   HWND hwnd;
   WNDCLASS wndclass;

   hInst = hI;

   wndclass.style         = CS_HREDRAW | CS_VREDRAW;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hI;
   wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
   wndclass.lpszMenuName = szAppName ;
   wndclass.lpszClassName = szAppName ;
   if(!RegisterClass(&wndclass)) {
      MessageBox(
         NULL,
         TEXT("This program requires Windows NT!"),
         szAppName,
         MB_ICONERROR
      );
      return 0;
   }

   hwnd = CreateWindow(
      szAppName,TEXT(TITULO_DA_JANELA),WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
      NULL, NULL, hI, NULL
   );
   ShowWindow(hwnd, iCS);
   UpdateWindow(hwnd);
   while(GetMessage(&msg, NULL, 0, 0)) {
      TranslateMessage(&msg);
      DispatchMessage (&msg);
   }
   return msg.wParam;
}

void Triangle(HDC hdc, POINT pt[]) {
   SelectObject(hdc, GetStockObject(BLACK_BRUSH));
   Polygon(hdc, pt, 3);
   SelectObject(hdc, GetStockObject(WHITE_BRUSH));
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
   static HWND hwndSmaller, hwndLarger;
   static int  cxClient, cyClient, cxChar, cyChar;
   int         cx, cy;
   LPDRAWITEMSTRUCT pdis;
   POINT       pt[3];
   RECT        rc;

   switch(msg) {
      case WM_CREATE:
         cxChar = LOWORD(GetDialogBaseUnits());
         cyChar = HIWORD(GetDialogBaseUnits());
         hwndSmaller = CreateWindow(
            TEXT("button"),TEXT(""),WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
            0,0, BTN_WIDTH, BTN_HEIGHT,
            hwnd, (HMENU) ID_SMALLER, hInst, NULL
         );

         hwndLarger = CreateWindow(
            TEXT("button"),TEXT(""),WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
            0, 0, BTN_WIDTH, BTN_HEIGHT,
            hwnd, (HMENU) ID_LARGER, hInst, NULL
         );
         return 0;
      case WM_SIZE:
         cxClient = LOWORD(lP);//Gravar a largura em relação a hwnd
         cyClient = HIWORD(lP);//Gravar a altura em relação a hwnd

         MoveWindow(
            hwndSmaller,//Identificador do primeiro botão..
            cxClient / 2 - 3 * BTN_WIDTH / 2,
            cyClient / 2 - BTN_HEIGHT / 2,
            BTN_WIDTH, BTN_HEIGHT, TRUE
         );//Essa função MoveWindow move o objeto(hwndSmaller) na tela
         MoveWindow(
            hwndLarger,//Identificador do segundo botão..
            cxClient / 2 + BTN_WIDTH / 2,
            cyClient / 2 - BTN_HEIGHT / 2,
            BTN_WIDTH, BTN_HEIGHT, TRUE
         );//Essa função MoveWindow move o objeto(hwndLarger) na tela
         return 0;
      case WM_COMMAND:
         GetWindowRect(hwnd, &rc);
         /**Make the window 10% smaller or larger*/
         switch(wP) {
            case ID_SMALLER://Diminui o tamanho da janela(hwnd)
               rc.left   += cxClient/20;
               rc.right  -= cxClient/20;
               rc.top    += cyClient/20;
               rc.bottom -= cyClient/20;
               break;
            case ID_LARGER://Aumenta o tamanho da janela(hwnd)
               rc.left   -= cxClient/20;
               rc.right  += cxClient/20;
               rc.top    -= cyClient/20;
               rc.bottom += cyClient/20;
               break;
         }
         MoveWindow(
            hwnd,//Define o tamanho e a posição da janela(hwnd)
            rc.left,
            rc.top,
            rc.right - rc.left,
            rc.bottom - rc.top,
            TRUE
         );
         return 0;//case WM_COMMAND
      case WM_DRAWITEM:
         pdis = (LPDRAWITEMSTRUCT) lP;
         //Fill area with white and frame it black
         FillRect(
            pdis->hDC,
            &pdis->rcItem,
            (HBRUSH) GetStockObject(WHITE_BRUSH)
         );
         FrameRect(
            pdis->hDC,
            &pdis->rcItem,
            (HBRUSH) GetStockObject(BLACK_BRUSH)
         );
         //Draw inward and outward black triangles
         cx = pdis->rcItem.right - pdis->rcItem.left;
         cy = pdis->rcItem.bottom - pdis->rcItem.top;
         switch(pdis->CtlID) {
            case ID_SMALLER://Desenha no primeiro botão..
               pt[0].x = 3*cx/8; pt[0].y = 1*cy/8;
               pt[1].x = 5*cx/8; pt[1].y = 1*cy/8;
               pt[2].x = 4*cx/8; pt[2].y = 3*cy/8;
               Triangle(pdis->hDC, pt);
               pt[0].x = 7 * cx / 8;  pt[0].y = 3 * cy / 8;
               pt[1].x = 7 * cx / 8;  pt[1].y = 5 * cy / 8;
               pt[2].x = 5 * cx / 8 ;  pt[2].y = 4 * cy / 8;
               Triangle(pdis->hDC, pt);
               pt[0].x = 5 * cx / 8;  pt[0].y = 7 * cy / 8;
               pt[1].x = 3 * cx / 8;  pt[1].y = 7 * cy / 8;
               pt[2].x = 4 * cx / 8;  pt[2].y = 5 * cy / 8;
               Triangle(pdis->hDC, pt);
               pt[0].x = 1 * cx / 8;  pt[0].y = 5 * cy / 8;
               pt[1].x = 1 * cx / 8;  pt[1].y = 3 * cy / 8;
               pt[2].x = 3 * cx / 8;  pt[2].y = 4 * cy / 8;
               Triangle(pdis->hDC, pt);
               break ;
            case ID_LARGER://Desenha no segundo botão..
               pt[0].x = 5 * cx / 8;  pt[0].y = 3 * cy / 8;
               pt[1].x = 3 * cx / 8;  pt[1].y = 3 * cy / 8;
               pt[2].x = 4 * cx / 8;  pt[2].y = 1 * cy / 8;
               Triangle(pdis->hDC, pt);
               pt[0].x = 5 * cx / 8;  pt[0].y = 5 * cy / 8;
               pt[1].x = 5 * cx / 8;  pt[1].y = 3 * cy / 8;
               pt[2].x = 7 * cx / 8;  pt[2].y = 4 * cy / 8;
               Triangle(pdis->hDC, pt);
               pt[0].x = 3 * cx / 8;  pt[0].y = 5 * cy / 8;
               pt[1].x = 5 * cx / 8;  pt[1].y = 5 * cy / 8;
               pt[2].x = 4 * cx / 8;  pt[2].y = 7 * cy / 8;
               Triangle(pdis->hDC, pt);
               pt[0].x = 3 * cx / 8;  pt[0].y = 3 * cy / 8;
               pt[1].x = 3 * cx / 8;  pt[1].y = 5 * cy / 8;
               pt[2].x = 1 * cx / 8;  pt[2].y = 4 * cy / 8;
               Triangle(pdis->hDC, pt);
               break;
         }
         /**Invert the rectangle if the button is selected*/
         if(pdis->itemState & ODS_SELECTED) {
            InvertRect(pdis->hDC, &pdis->rcItem);
         }
         /**Draw a focus rectangle if the button has the focus*/
         if(pdis->itemState & ODS_FOCUS) {
            pdis->rcItem.left   += cx / 16;
            pdis->rcItem.top    += cy / 16;
            pdis->rcItem.right  -= cx / 16;
            pdis->rcItem.bottom -= cy / 16;
            DrawFocusRect(pdis->hDC, &pdis->rcItem);
         }
         return 0;//case WM_DRAWITEM
      case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
   }//switch(msg)
   return DefWindowProc(hwnd, msg, wP, lP);
}

Comentários