WinAPI.如何重绘没有背景的窗口? [英] WinAPI. How to redraw window which has no background?

查看:38
本文介绍了WinAPI.如何重绘没有背景的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 WNDCLASSEX 结构,其中包含以下数据:

Hi I have WNDCLASSEX structure which has this data:

m_wndClass.cbSize = sizeof(WNDCLASSEX);
m_wndClass.style = CS_NOCLOSE;
m_wndClass.lpfnWndProc = WndProc;
m_wndClass.cbClsExtra = 0;
m_wndClass.cbWndExtra = 0;
m_wndClass.hInstance = GetModuleHandle(NULL);
m_wndClass.hIcon = NULL;
m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wndClass.hbrBackground = NULL;
m_wndClass.lpszMenuName = NULL;
m_wndClass.lpszClassName = Checkbox::CHECKBOX_CLASS.c_str(); 
m_wndClass.hIconSm = NULL;

我需要有没有背景的窗口,因为我需要在父窗口上绘制文本并且文本可以是任何颜色.

I need to have window without background, because I need to draw text on parent window and text may be any color.

绘图工作正常,绘图代码:

Drawing works fine, code for drawing:

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC dc = BeginPaint(window, &ps);

    if (!classInfo->m_text.empty())
    {
        HDC wdc = GetDC(window);

        SetBkMode(wdc,TRANSPARENT);
        DrawText(wdc, classInfo->m_text.c_str(), -1, &classInfo->m_textRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOCLIP);
        ReleaseDC(window, wdc); 
    }

    EndPaint(window, &ps);
    break;
}

但是我有方法更改标签文本:

However I have method to change text of label:

void Checkbox::SetText(const String& text)
{
    m_text = text;
    SetTextRectSize();   //calculates size of RECT 
    if (m_window != NULL)
        InvalidateRect(m_window, NULL, TRUE);
}

创建带有标签的窗口后,我看到了标签,但是如果我想更改其上的文本,它不会更改(我需要手动调整窗口大小,然后更改).但是我以前有彩色背景的时候没有这个问题,例如我的窗口类有这个:

After I create window with label I see label, however if I want to change text on it, it doesn't change (I need to manualy resize window and it changes after that). However I hadn't have this problem at the time when I used to have colored background, for example my window class had this:

m_wndClass.hbrBackground = HBRUSH(COLOR_3DFACE+1);

我想问一下,如何更新没有背景的窗口.

I want to ask, how to update window which, has no background.

我尝试了一些东西

FillRect(dc, &rect, (HBRUSH)GetStockObject(NULL_BRUSH));

还试图改变窗口过程:

case WM_CTLCOLORSTATIC:
{
    HDC hdc = (HDC) wp; 
    SetBkMode (hdc, TRANSPARENT);
    return (LRESULT)GetStockObject(NULL_BRUSH);
}

结果是我在之前绘制了新文本,将 text 设置为 some long text 后,标签的一部分被损坏了! 看到这个 但在调整主窗口大小后,我的标签变得可读.

And the result is that I draw new text on previous, after setting text into some longer text part of label becomes corupted! see this but after resizing the main window my label becomes readable.

推荐答案

您的代码没有为 DrawText() 设置设备上下文的文本前景色,尽管默认值为黑色.请参阅 SetTextColor().

Your code doesn't set the device context's text foreground color for DrawText(), although the default is black. See SetTextColor().

SetBkMode(..., TRANSPARENT) 只是为 DrawText() 矩形设置背景颜色/模式,而不是整个窗口.

SetBkMode(..., TRANSPARENT) just sets the background color/mode for the DrawText() rect, not the entire window.

这篇关于WinAPI.如何重绘没有背景的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆