webForumDet fria alternativet

Byta färg i API

C/C++ur C/C++

2 svar · 203 visningar · startad av CoyPu

CoyPuMedlem sedan apr. 200363 inlägg
#1

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

nikoMedlem sedan juni 20022 599 inlägg
#2

Ä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 och SetTextColor 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.

CoyPuMedlem sedan apr. 200363 inlägg
#3

tnx alot! det funka!

135 ms totalt · 3 externa anrop · v20260731065814-full.29ac60f6
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
132 ms — hämta tråd, inlägg och bilagor (db)