---
title: "Byta färg i API"
type: "forum-thread"
url: "https://www.webforum.nu/amne/c-cpp/77440-byta-färg-i-api"
topic: "C/C++"
topic_url: "https://www.webforum.nu/amne/c-cpp"
author: "CoyPu"
published: "2003-05-19T19:01:20.000Z"
updated: "2003-05-22T10:25:54.000Z"
replies: 2
views: 225
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/c-cpp/77440-byta-färg-i-api"
---

# Byta färg i API

## #1 — CoyPu, 2003-05-19T19:01Z

Jag skulle kunna vilja byta färg på både text och bakgrund genom att välja i en meny!

```
#include <windows.h>
#include <stdio.h>
#include <string.h>

#define IDM_MENU1 1
#define BGblack   202
#define BGwhite   201
#define TEXTblack 102
#define TEXTwhite 101

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

HWND hwndMain;
HINSTANCE hInst;

char uttext [50];
char temp[10];
char str[45];
int xPos;
int yPos;
HDC hdc;
WNDCLASS wc;

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;

HMENU MinMeny;

    // Register the Windows class
    if (!hPrevInstance)
    {
        wc.style = 0;
        wc.lpfnWndProc = (WNDPROC) MainWndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;

        wc.hIcon = LoadIcon(hInstance,
            MAKEINTRESOURCE(1));
        wc.hCursor = LoadCursor(hInstance,
            MAKEINTRESOURCE(1));
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName =  "MainMenu";
        wc.lpszClassName = "myMainWndClass";

        if (!RegisterClass(&wc))
            return FALSE;
    }

    hInst = hInstance;  // save instance handle
    MinMeny = LoadMenu(hInst, MAKEINTRESOURCE(IDM_MENU1)) ;

    // Create the main window.

    hwndMain = CreateWindow(
        "myMainWndClass",     // class name
        "Christer Corneliusson",           // window name
    WS_OVERLAPPEDWINDOW |   // overlapped window
        WS_HSCROLL |        // horizontal scroll bar
        WS_VSCROLL,         // vertical scroll bar
    CW_USEDEFAULT,          // default horizontal position

    CW_USEDEFAULT,          // default vertical position
    CW_USEDEFAULT,          // default width
    CW_USEDEFAULT,          // default height
    (HWND) NULL,            // no parent or owner window
    MinMeny,//(HMENU) NULL,           // class menu used
    hInstance,              // instance handle
    NULL);                  // no window creation data

    // If the main window cannot be created, terminate
    // the application.

    if (!hwndMain)
        return FALSE;

    // Show the window and paint its contents.

    ShowWindow(hwndMain, nCmdShow);
    UpdateWindow(hwndMain);

    // Start the message loop.

    while (GetMessage(&msg, (HWND) NULL, 0, 0))

    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Return the exit code to Windows.

    return msg.wParam;
}

LRESULT CALLBACK MainWndProc(
    HWND hwnd,        // handle of window
    UINT uMsg,        // message identifier
    WPARAM wParam,    // first message parameter
    LPARAM lParam)    // second message parameter
{

    switch (uMsg)
    {

        /*
        case WM_CREATE:
            // Initialize the window.
            break;
        */

        /*
        case WM_PAINT:
            // Paint the window's client area.
            break;
        */

        /*
        case WM_SIZE:
            // Set the size and position of the window.

            break;
        */
        case WM_COMMAND:

            if (wParam == BGblack)

            if (wParam == BGwhite)
            if (wParam == TEXTblack)
            if (wParam == TEXTwhite)
        		break;

        case WM_RBUTTONUP:

        		break;

        case WM_LBUTTONUP:

            xPos = LOWORD(lParam);    /* horizontal position of cursor */
				yPos = HIWORD(lParam);    /* vertical position of cursor   */

            strcpy(uttext, "Kordinaten du klickade på är: ");
            itoa(xPos, temp, 10);
            strcat(uttext, temp);
            strcat(uttext, ",");
            itoa(yPos, temp, 10);
            strcat(uttext, temp);

            MessageBox(0, uttext, /*szAppName*/"", MB_OK);

            break;

        case WM_CHAR:
        		if ((char)wParam =='q' ||(char)wParam =='Q' )
            {
            	MessageBox(0, "HEJ DÅ!", /*szAppName*/"", MB_OK);
               PostQuitMessage(0);
            }

           	hdc = GetDC(hwnd);
            sprintf(str, "du skrev: %c", (char) wParam );
            TextOut(hdc, xPos, yPos, str, strlen(str));
            ReleaseDC(hwnd, hdc);
            break;

        case WM_CLOSE:

        // Create the message box. If the user clicks
        // the Yes button, destroy the main window.

            if (MessageBox(0, "Vill du verkligen avsluta ?", /*szAppName*/"",
                MB_YESNO) == IDYES)
                DestroyWindow(hwndMain);
            break;

        case WM_DESTROY:
            // Quit when destroying the main window
            PostQuitMessage(0);
            break;

        //
        // Process other messages.
        //

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}
```

tack på förhand

Permalänk: https://www.webforum.nu/p/77440

## #2 — niko, 2003-05-19T19:22Z

Är osäker på om du med "bakgrund" menar hela bakgrunden eller bara själva textbakgrunden. Om du menar det första så är nog [SetBkColor](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pantdraw_433m.asp) och [SetTextColor](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_8bsi.asp) två bra funktioner.

/red: Snurrade nog till det lite angående bakgrundsfärgen. Vill du sätta bakgrunden till svart kan du tex göra så här:

```
RECT r;
hdc = GetDC(hwnd);
GetClientRect(hwnd, &r); 
FillRect(hdc, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));
```

.. Och WHITE_BRUSH för vit.

Permalänk: https://www.webforum.nu/p/1056234

## #3 — CoyPu, 2003-05-22T10:25Z

tnx alot! det funka!

Permalänk: https://www.webforum.nu/p/1058343

---

Tråden på webben: https://www.webforum.nu/amne/c-cpp/77440-byta-färg-i-api
