send
Grupo de Telegram
play_arrow
Este sitio utiliza cookies propias y de terceros. Si continúa navegando consideramos que acepta el uso de cookies. OK Más Información.

Manejando el color de fondo de una ventana (WinApi)

  • 0 Respuestas
  • 2252 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Jhonjhon_123

  • *
  • Underc0der
  • Mensajes: 150
  • Actividad:
    0%
  • Reputación 0
    • Ver Perfil
    • Jhonjhon_123's Software In Developed
« en: Febrero 23, 2010, 06:42:44 pm »
Código: C
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. #define ID_BUTTON 1
  5. #define ID_EDIT_1 2
  6. #define ID_EDIT_2 3
  7. #define ID_EDIT_3 4
  8.  
  9. /*  Declare Windows procedure  */
  10. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  11. WORD Comando;
  12.  
  13. /*  Make the class name into a global variable  */
  14. char szClassName[ ] = "CodeBlocksWindowsApp";
  15.  
  16. int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
  17. {
  18.     HWND hwnd;               /* This is the handle for our window */
  19.     MSG messages;            /* Here messages to the application are saved */
  20.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  21.  
  22.     /* The Window structure */
  23.     wincl.hInstance = hThisInstance;
  24.     wincl.lpszClassName = szClassName;
  25.     wincl.lpfnWndProc = WindowProcedure;
  26.     wincl.style = CS_DBLCLKS;
  27.     wincl.cbSize = sizeof (WNDCLASSEX);
  28.  
  29.     /* Use default icon and mouse-pointer */
  30.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  31.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  32.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  33.     wincl.lpszMenuName = NULL;
  34.     wincl.cbClsExtra = 0;
  35.     wincl.cbWndExtra = 0;
  36.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  37.  
  38.     if (!RegisterClassEx (&wincl))
  39.         return 0;
  40.  
  41.     hwnd = CreateWindowEx (
  42.            0,                   /* Extended possibilites for variation */
  43.            szClassName,         /* Classname */
  44.            "B. Color changer (?)",       /* Title Text */
  45.            WS_OVERLAPPEDWINDOW, /* default window */
  46.            CW_USEDEFAULT,       /* Windows decides the position */
  47.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  48.            240,                 /* The programs width */
  49.            160,                 /* and height in pixels */
  50.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  51.            NULL,                /* No menu */
  52.            hThisInstance,       /* Program Instance handler */
  53.            NULL                 /* No Window Creation data */
  54.            );
  55.  
  56.     ShowWindow (hwnd, nCmdShow);
  57.  
  58.     while (GetMessage (&messages, NULL, 0, 0))
  59.     {
  60.         TranslateMessage(&messages);
  61.         DispatchMessage(&messages);
  62.     }
  63.     return messages.wParam;
  64. }
  65.  
  66.  
  67. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  68. {
  69.     static HWND hwndEdit,hwndEdit1,hwndEdit2;
  70.     HFONT hFont;
  71.     HDC hdc;
  72.     PAINTSTRUCT ps;
  73.     HBRUSH BackgroundBrush;
  74.     RECT client_rect;
  75.     static TCHAR text[4], text1[4], text2[4];
  76.  
  77.     switch (message)
  78.     {
  79.         case WM_CREATE:
  80.             hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
  81.               30, 25, 50, 20, hwnd, (HMENU) ID_EDIT_1,NULL, NULL);
  82.             hwndEdit1 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
  83.               90, 25, 50, 20, hwnd, (HMENU) ID_EDIT_2,NULL, NULL);
  84.             hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
  85.               150, 25, 50, 20, hwnd, (HMENU) ID_EDIT_3,NULL, NULL);
  86.             break;
  87.  
  88.         case WM_PAINT:
  89.         {
  90.             hdc = BeginPaint(hwnd, &ps);
  91.             int len = GetWindowTextLength(hwndEdit)+1;
  92.             int len1 = GetWindowTextLength(hwndEdit1)+1;
  93.             int len2 = GetWindowTextLength(hwndEdit2)+1;
  94.             GetWindowText(hwndEdit,text,len);
  95.             GetWindowText(hwndEdit1,text1,len1);
  96.             GetWindowText(hwndEdit2,text2,len2);
  97.             GetClientRect(hwnd, &client_rect);
  98.             BackgroundBrush = CreateSolidBrush(RGB(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text1),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text2))); //green background
  99.             FillRect(hdc, &client_rect, BackgroundBrush);
  100.             hFont = CreateFont(
  101.                 20, 10, 0, 0,FW_NORMAL, FALSE, FALSE, FALSE,
  102.                 ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
  103.                 DEFAULT_QUALITY,DEFAULT_PITCH|FF_ROMAN,"Verdana");
  104.             SelectObject(hdc, hFont);
  105.             SetTextColor(hdc,RGB(130,130,130));
  106.             SetBkColor(hdc,RGB(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text1),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text2)));
  107.             TextOut(hdc,13,70,"Inserta el color RGB",20);
  108.             DeleteObject(hFont);
  109.             break;
  110.         }
  111.  
  112.         case WM_COMMAND:
  113.         {
  114.             InvalidateRect(hwnd, NULL, TRUE);
  115.             Comando = LOWORD(wParam);
  116.             break;
  117.         }
  118.  
  119.         case WM_DESTROY:
  120.             EndPaint(hwnd, &ps);
  121.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  122.             break;
  123.  
  124.         default:                      /* for messages that we don't deal with */
  125.             return DefWindowProc (hwnd, message, wParam, lParam);
  126.     }
  127.  
  128.     return 0;
  129. }

Código "interesante":

Código: C
  1.         case WM_PAINT:
  2.         {
  3.             hdc = BeginPaint(hwnd, &ps);
  4.             int len = GetWindowTextLength(hwndEdit)+1;
  5.             int len1 = GetWindowTextLength(hwndEdit1)+1;
  6.             int len2 = GetWindowTextLength(hwndEdit2)+1;
  7.             GetWindowText(hwndEdit,text,len);
  8.             GetWindowText(hwndEdit,text1,len1);
  9.             GetWindowText(hwndEdit,text2,len2);
  10.             GetClientRect(hwnd, &client_rect);
  11.             BackgroundBrush = CreateSolidBrush(RGB(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text1),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text2))); //green background
  12.             FillRect(hdc, &client_rect, BackgroundBrush);
  13.             hFont = CreateFont(
  14.                 20, 10, 0, 0,FW_NORMAL, FALSE, FALSE, FALSE,
  15.                 ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
  16.                 DEFAULT_QUALITY,DEFAULT_PITCH|FF_ROMAN,"Verdana");
  17.             SelectObject(hdc, hFont);
  18.             SetTextColor(hdc,RGB(130,130,130));
  19.             SetBkColor(hdc,RGB(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text1),You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(text2)));
  20.             TextOut(hdc,13,70,"Inserta el color RGB",20);
  21.             DeleteObject(hFont);
  22.             break;
  23.         }
  24.  
  25.         case WM_COMMAND:
  26.         {
  27.             InvalidateRect(hwnd, NULL, TRUE);
  28.             Comando = LOWORD(wParam);
  29.             break;
  30.         }

Simple, tienes tres cajas de texto en el que introduces un valor RGB a cada una y el color de fondo se cambia solo por arte de magia! x)
« Última modificación: Febrero 08, 2014, 06:16:34 pm por Expermicid »
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login

 

¿Te gustó el post? COMPARTILO!