Linguagem C - Função BeginPaint, SetMapMode, SetWindowExtEx, SetViewportExtEx, SelectObject, ExtCreatePen, BeginPath, MoveToEx, LineTo, ..

main.html
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

HMENU hMenu;

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

void addMenus(HWND);

static int r = 0, g = 77, b = 77;

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, PSTR szCL, int iCS) {
   static TCHAR szAppName[] = TEXT("EndJoin") ;
   HWND hwnd;
   MSG msg;
   WNDCLASS wndclass;
   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(GRAY_BRUSH);
   wndclass.lpszMenuName = NULL;
   wndclass.lpszClassName = szAppName;
   
   if(!RegisterClass(&wndclass)) {
      MessageBox(
         NULL,
         TEXT("This program requires Windows NT!"),
         szAppName,
         MB_ICONERROR
      );
      return 0;
   }
   hwnd = CreateWindow(
      szAppName, TEXT("Linguagem C"), 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;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
   /* Configuração específica para canetas geométricas(PS_GEOMETRIC) */
   static int iEnd[] = {
      PS_ENDCAP_ROUND,
      PS_ENDCAP_SQUARE,
      PS_ENDCAP_FLAT
   };
   static int iJoin[] = {
      PS_JOIN_ROUND,
      PS_JOIN_BEVEL,
      PS_JOIN_MITER
   };
   /* Configuração específica para canetas geométricas(PS_GEOMETRIC) */
   
   static int cxClient, cyClient;
   HDC hdc;
   int i;
   LOGBRUSH lb; //lb contém os attributos do pincel
   PAINTSTRUCT ps;
   switch(msg) {
      case WM_SIZE:
         cxClient = LOWORD(lP);//Guarda o valor da largura em cxClient
         cyClient = HIWORD(lP);//Guarda o valor da altura em cyClient
         return 0;

      case WM_CREATE:
         addMenus(hwnd);
         return 0;
         
      case WM_COMMAND:
         if(LOWORD(wP)==5) {
            struct tm *ptr;
            time_t lt;
            lt = time(NULL);
            ptr = localtime(&lt);
            MessageBox(NULL, asctime(ptr), "Data e Hora:", MB_OK);
         }
         if(LOWORD(wP)==8) {
            int cxScreen, cyScreen;
            cxScreen = GetSystemMetrics(0);
            cyScreen = GetSystemMetrics(1);
            char charX[500], charY[500];
            itoa(cxScreen, charX, 10);
            itoa(cyScreen, charY, 10);

            char ttl[60];//total

            sprintf(ttl,"Horizontal: %spx - Vertical: %spx",charX,charY);

            MessageBox(NULL, ttl, "Tamanha da Tela: ", MB_OK);
         }
         if(LOWORD(wP)==11) {
            exit(0);
         }
         return 0;
         
      case WM_PAINT:
         hdc = BeginPaint(hwnd, &ps);
         SetMapMode(hdc, MM_ANISOTROPIC);
         SetWindowExtEx(hdc, 100, 100, NULL);
         SetViewportExtEx(hdc, cxClient, cyClient, NULL);
         lb.lbStyle = BS_SOLID; //Estilo do pincel
         lb.lbColor = RGB(r, g, b); //Cor do desenho
         lb.lbHatch = 0;
         for(i = 0; i < 3; i++) {
            SelectObject(
               hdc,
               ExtCreatePen( //Cria a caneta em modo geométrico
                  PS_SOLID | PS_GEOMETRIC | iEnd[i] | iJoin[i],
                  10,
                  &lb,
                  0,
                  NULL
               )
            );//Função SelectObject
            BeginPath(hdc);
            MoveToEx(//Posição inicial da linha de contorno
               hdc,
               10+30*i,
               25,
               NULL
            );       //Posição inicial da linha de contorno
            LineTo(hdc,20 + 30 * i,75);//Configura a linha de contorno
            LineTo(hdc,30 + 30 * i, 0);//Configura a linha de contorno
            EndPath(hdc);
            StrokePath(hdc);
            DeleteObject(
               SelectObject(hdc, GetStockObject(BLACK_PEN))
            );
            MoveToEx(hdc,10 + 30 * i,25,NULL);//Posição inicial da linha
            LineTo(hdc,20 + 30 * i,75);//Configura a linha(normal)
            LineTo(hdc,30 + 30 * i, 0);//Configura a linha(normal)
         } //Fecha o laço for
         EndPaint(hwnd, &ps);
         return 0; //WM_PAINT
      
      case WM_DESTROY:
         PostQuitMessage(0);
         return 0; //WM_DESTROY
   } //switch(msg)
   return DefWindowProc(hwnd, msg, wP, lP);
}

void addMenus(HWND hwnd) {
    hMenu = CreateMenu();//Menu Principal

    HMENU hFileMenu = CreateMenu();//SubMenu

    AppendMenu(hMenu,MF_POPUP,(UINT_PTR) hFileMenu,"Opções");
    AppendMenu(hFileMenu, MF_STRING, 5, "Data e Hora");
    AppendMenu(hFileMenu, MF_STRING, 8, "Medidas da tela..");
    AppendMenu(hFileMenu, MF_STRING, 11, "Sair");

    SetMenu(hwnd, hMenu);
}

Comentários