简体   繁体   English

C ++使用DrawText重绘

[英]C++ redraw with DrawText

Currently i'm drawing the text from a textbox in my window. 目前,我正在从窗口中的文本框中绘制文本。 I successfully get the text i need to draw and it draws the text. 我成功获取了需要绘制的文本,并且绘制了文本。 It's ok. 没关系。

Here's the problem: when i write something else in my input box and draw the text again (via button push), the new text is drawn right on top of the previous text as to be expected. 这是问题所在:当我在输入框中输入其他内容并再次绘制文本时(通过按钮按下),新文本将在预期的前一个文本的正上方绘制。

I'm new to all of this and i can't find a way to clear the previous text before drawing the new text. 我是所有这一切的新手,在绘制新文本之前我找不到找到清除先前文本的方法。

Here's my code: 这是我的代码:

void DrawMyText(HWND hwnd) {

    int iTextLength = GetWindowTextLength(hDrawInput) + 1;
    char cDrawText[1000] = "";
    HDC wdc = GetWindowDC(hwnd);    
    RECT canvas;
    canvas.left   = 168;
    canvas.top    = 108;
    canvas.right  = 500;
    canvas.bottom = 500;

    GetWindowText(hDrawInput, cDrawText, iTextLength);

    SetTextColor(wdc, 0x00FF0066);
    SetBkMode(wdc,TRANSPARENT);
    DrawText(wdc, cDrawText, -1, &canvas, DT_LEFT);

    DeleteDC(wdc);
}

Any tips on how to do this? 有关如何执行此操作的任何提示? I will gladly provide any additional information if needed. 如果需要,我将很乐意提供任何其他信息。 Thanks in advance! 提前致谢!

DrawText is more like a spray can - it paints over top whatever is already there. DrawText更像是一个喷壶-它可以将已经存在的所有内容绘制在顶部。 I'd recommend switching to SetWindowText. 我建议切换到SetWindowText。 The difference is DrawText is more like a canvas rendering call, and doesn't consider much about the type of control it's drawing to. 区别在于DrawText更像是一个画布渲染调用,并且对其绘制到的控件类型没有太多考虑。 SetWindowText is an explicit "set the text of this window to this specific value", and is specific to text-based controls. SetWindowText是显式的“将此窗口的文本设置为此特定值”,并且特定于基于文本的控件。 More to the point, it will replace the current text with the new text value. 更重要的是,它将用新的文本值替换当前文本。

If you absolutely must do it with DrawText (ie you prefer a canvas approach as above), then you'll have to manually clear the text area yourself with something like InvalidateRect() (using the RECT of the text area). 如果您绝对必须使用DrawText来做到这一点(即您更喜欢上面的画布方法),则必须使用InvalidateRect()之类的东西(使用文本区域的RECT)手动清除文本区域。 Or, by drawing a rectangle equal in size to the text area and with the same color as the background. 或者,通过绘制一个与文本区域大小相等且颜色与背景相同的矩形。 Let me know if that's not enough detail. 让我知道是否不够详细。

If some other window covers and then uncovers the place where you drew the text it will be gone! 如果其他窗口遮住了,然后又发现了绘制文本的位置,它将消失! The illusion that windows can sit on top of each other is destroyed! 窗户可以彼此叠放的错觉被摧毁了! Windows provides a scheme to overcome this problem but you are not cooperating with the scheme. Windows提供了解决此问题的方案,但是您不配合该方案。

Paint your text only in response to the WM_PAINT message, using the BeginPaint and EndPaint API calls. 使用BeginPaint和EndPaint API调用,仅在响应WM_PAINT消息时才绘制文本。 On the button click just call InvalidateRect, which asks Windows to send you a WM_PAINT. 在按钮上单击,只需调用InvalidateRect,它要求Windows向您发送WM_PAINT。 BeginPaint erases your window just before you paint. BeginPaint会在绘制之前擦除您的窗口。 So by putting your painting code in the right place - the WM_PAINT handler - you solve two problems: It erases any old text, and it repaints whenever your window is uncovered. 因此,通过将绘画代码放在正确的位置(WM_PAINT处理程序),您可以解决两个问题:擦除所有旧文本,并在发现窗口时重新绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM