Manejando el color de fondo de una ventana (WinApi)

Iniciado por Jhonjhon_123, Febrero 23, 2010, 06:42:44 PM

Tema anterior - Siguiente tema

0 Miembros y 2 Visitantes están viendo este tema.

Febrero 23, 2010, 06:42:44 PM Ultima modificación: Febrero 08, 2014, 06:16:34 PM por Expermicid
Código: c
#include <windows.h>
#include <stdio.h>

#define ID_BUTTON 1
#define ID_EDIT_1 2
#define ID_EDIT_2 3
#define ID_EDIT_3 4

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
WORD Comando;

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "B. Color changer (?)",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           240,                 /* The programs width */
           160,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    ShowWindow (hwnd, nCmdShow);

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndEdit,hwndEdit1,hwndEdit2;
    HFONT hFont;
    HDC hdc;
    PAINTSTRUCT ps;
    HBRUSH BackgroundBrush;
    RECT client_rect;
    static TCHAR text[4], text1[4], text2[4];

    switch (message)
    {
        case WM_CREATE:
            hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
              30, 25, 50, 20, hwnd, (HMENU) ID_EDIT_1,NULL, NULL);
            hwndEdit1 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
              90, 25, 50, 20, hwnd, (HMENU) ID_EDIT_2,NULL, NULL);
            hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
              150, 25, 50, 20, hwnd, (HMENU) ID_EDIT_3,NULL, NULL);
            break;

        case WM_PAINT:
        {
            hdc = BeginPaint(hwnd, &ps);
            int len = GetWindowTextLength(hwndEdit)+1;
            int len1 = GetWindowTextLength(hwndEdit1)+1;
            int len2 = GetWindowTextLength(hwndEdit2)+1;
            GetWindowText(hwndEdit,text,len);
            GetWindowText(hwndEdit1,text1,len1);
            GetWindowText(hwndEdit2,text2,len2);
            GetClientRect(hwnd, &client_rect);
            BackgroundBrush = CreateSolidBrush(RGB(atoi(text),atoi(text1),atoi(text2))); //green background
            FillRect(hdc, &client_rect, BackgroundBrush);
            hFont = CreateFont(
                20, 10, 0, 0,FW_NORMAL, FALSE, FALSE, FALSE,
                ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                DEFAULT_QUALITY,DEFAULT_PITCH|FF_ROMAN,"Verdana");
            SelectObject(hdc, hFont);
            SetTextColor(hdc,RGB(130,130,130));
            SetBkColor(hdc,RGB(atoi(text),atoi(text1),atoi(text2)));
            TextOut(hdc,13,70,"Inserta el color RGB",20);
            DeleteObject(hFont);
            break;
        }

        case WM_COMMAND:
        {
            InvalidateRect(hwnd, NULL, TRUE);
            Comando = LOWORD(wParam);
            break;
        }

        case WM_DESTROY:
            EndPaint(hwnd, &ps);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


Código "interesante":

Código: c
        case WM_PAINT:
        {
            hdc = BeginPaint(hwnd, &ps);
            int len = GetWindowTextLength(hwndEdit)+1;
            int len1 = GetWindowTextLength(hwndEdit1)+1;
            int len2 = GetWindowTextLength(hwndEdit2)+1;
            GetWindowText(hwndEdit,text,len);
            GetWindowText(hwndEdit,text1,len1);
            GetWindowText(hwndEdit,text2,len2);
            GetClientRect(hwnd, &client_rect);
            BackgroundBrush = CreateSolidBrush(RGB(atoi(text),atoi(text1),atoi(text2))); //green background
            FillRect(hdc, &client_rect, BackgroundBrush);
            hFont = CreateFont(
                20, 10, 0, 0,FW_NORMAL, FALSE, FALSE, FALSE,
                ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                DEFAULT_QUALITY,DEFAULT_PITCH|FF_ROMAN,"Verdana");
            SelectObject(hdc, hFont);
            SetTextColor(hdc,RGB(130,130,130));
            SetBkColor(hdc,RGB(atoi(text),atoi(text1),atoi(text2)));
            TextOut(hdc,13,70,"Inserta el color RGB",20);
            DeleteObject(hFont);
            break;
        }

        case WM_COMMAND:
        {
            InvalidateRect(hwnd, NULL, TRUE);
            Comando = LOWORD(wParam);
            break;
        }


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)
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