本文介绍了使用GDI在矩形上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:超级菜鸟警报,请耐心等待. :D

我才刚刚开始学习Win32 API并开始使用GDI等,已经有点难以理解了.

这是我的代码:

Warning: super noob alert so bear with me. :D

I''ve only just begun learning the Win32 API and started using the GDI, etc and already I''m having some difficulty understanding.

This is my code:

#include <windows.h>

char ClassName[] = "myWindowClass";
HBRUSH hOrange = CreateSolidBrush(RGB(255,180,0));
HBRUSH hRed = CreateSolidBrush(RGB(255,0,0));
HBRUSH hYellow = CreateSolidBrush(RGB(255,255,0));

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE previnstance, LPSTR lpcmdline, int ncmdshow)
{
    WNDCLASSEX wc;
    MSG msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = hYellow;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = ClassName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (RegisterClassEx(&wc) == 0)
    {
        MessageBox(NULL,
                   "Windows class failed to register",
                   "Error",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    HWND MainWindow;
    MainWindow = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        ClassName,
        "Rectangles",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        320,
        540,
        NULL,
        NULL,
        hInstance,
        NULL);

    if (MainWindow == NULL)
    {
        MessageBox(NULL,
                   "Failed to create window";,
                   "Error",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(MainWindow, ncmdshow);
    UpdateWindow(MainWindow);

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


LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT msg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    RECT secondbackground;
    RECT titlebox;
    HDC hdc;

    switch(msg)
    {
    case WM_PAINT:
        hdc = GetDC(hwnd);
        SetRect(&secondbackground, 3, 3, 297, 495);
        FillRect(hdc, &secondbackground, hOrange);
        SetRect(&titlebox, 20, 20, 278,45);
        FillRect(hdc,&titlebox, hYellow);
        ReleaseDC(hwnd, hdc);
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


我的问题是,当我绘制第二个矩形时,我想在第一个矩形上绘制它,所以我认为这只是绘制它们的顺序,但是很明显,我在运行该矩形时会误认为是屏幕上第二个黄色矩形奇怪地闪闪发光.如果有人想启发我以正确的方式绘制矩形,我将不胜感激.顺便说一句,如果有任何其他小错误或其他任何东西,这几乎是我的第一个WinApi程序,因此,将不胜感激. :)


My issue is that when i draw that second rectangle, i want to draw it on top of the first, so i figured it was just the order in which they were drawn, but clearly i''m mistaken cause when i run this, the second yellow rectangle is oddly strobing on the screen. If someone would like to enlighten me on the correct way to draw rectangles, i would greatly appreciate it. Btw, if there are any other little mistakes or anything, this is pretty much my first WinApi program, so any knowledge would be greatly appreciated. :)

推荐答案



这篇关于使用GDI在矩形上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 15:30
查看更多