如何在win32项目窗口中绘制文本 [英] how to draw text in win32 project window

查看:202
本文介绍了如何在win32项目窗口中绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何在win32项目的窗口中绘制文本,我有一些研究,我发现我可以使用DrawText()函数,但问题是你只能在WM_PAINT中使用它,但是我想在一个完全不同的函数中使用它,在函数内部说像void something(){然后调用drawText()},所以任何人都可以告诉我如何做或者有更好的方法来完成任务。谢谢

解决方案

您可以随时使用Drawtext()或TextOut(),只需要设备上下文:

< pre lang =c ++> void PrintHello(HWND hwnd)
{
HDC dc = GetDc(hwnd);
RECT rc;
GetClientRect(hwnd,& rc);
DrawText(dc, Hello!, - 1 ,& rc,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
ReleaseDC(hwnd,dc);
}



然后,如果你试图移动窗口,改变它的大小,缩小到图标然后恢复,拖动另一个窗口,等等你会看到你的文字不再存在。

这是因为当你以任何方式修改或改变窗口图形区域时,系统会要求你重绘感兴趣的部分(以后你会理解的是部分)通过WM_PAINT消息。当应用程序收到此消息时,它必须重新绘制该区域。

处理它的有效方法是创建一个绘图函数,使所有绘制和从WM_PAINT处理程序调用它。然后强制WM_PAINT调用RedrawWindow()或UpdateWindow()。



应用程序必须处理WM_PAINT的正确方法是:

1。调用GetUpdateRect()函数以检查是否有任何要更新的区域。如果没有定义区域,则该函数应该简单地返回什么都不做。

2.如果存在要重绘的区域,则调用BeginPaint()以获得再次具有要绘制的区域和窗口的设备上下文的PAINTSTRUCT 。

3.更新图形区域

4.调用EndPaint()

5.升级结束

在代码中:

  void  UpdateGraph(HWND hwnd,HDC dc)
{
RECT rc;
GetClientRect(hwnd,& rc);
DrawText(dc, Hello!, - 1 ,& rc,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
switch (uMsg )
{
case WM_PAINT:
{
RECT rToPaint;
if (!GetUpdateRect(hwnd,& rToPaint,FALSE))
break ; // 没有要更新的地区,请留下程序
PAINTSTRUCT ps;
BeginPaint(hwnd,& ps);

// 做图形绘画
UpdateGraph(hwnd,ps .hdc);

EndPaint(hwnd,& ps);

break ; // 程序结束
}
...
}
}





为什么要打印一个矩形区域?为了使图形刷新更快,在图形功能降低的旧设备上,限制更新区域(减少图形操作)非常有效。现在,当图形由GPU管理而不再由CPU管理时,当我们拥有高分辨率的复杂和大图时,这仍然有效。

如何管理部分绘图?通常,您创建一个独立的位图并将其加载到内存设备上下文中。使用BitBlt()在WM_PAINT处理程序中准备好复制屏幕上的内存上下文时,使所有图形都在后者上工作。要强制发布 WM_PAINT 消息,您可以调用 UpdateWindow() RedrawWindow( )

在这种情况下,如果存在更新区域,您可以设法仅将指定的图像部分复制到屏幕设备上,从而缩短更新时间。


hi, anyone can tell me how to draw text in a win32 project's window, i had a little of research about which i found that i can use DrawText() function but the problem is that you only can use it in WM_PAINT, but i wanted to use it in a totally different function, says inside a function like void something(){ then call drawText() }, so can anybody tell me how to do this or is there a better way which can do the task. thanks

解决方案

You can use Drawtext(), or TextOut(), in any moment, you just need the device context:

void PrintHello(HWND hwnd)
{
  HDC dc = GetDc(hwnd);
  RECT rc;
  GetClientRect(hwnd, &rc);
  DrawText(dc, "Hello!", -1, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  ReleaseDC(hwnd, dc);
}


Then if you try to move the window, change its size, reduce to icon then revert, drag another window over it, etc. you'll see that your text is no more there.
This is because when you modify or alter in any way the window graphic area the system asks you to redraw the interested part (yes part you'll understand later) by means of the WM_PAINT message. When an application receive this message it must repaint the area.
The effective way to handle it is to create a drawing function that make all painting and call it from the WM_PAINT handler. Then force a WM_PAINT calling RedrawWindow() or UpdateWindow().

The correct way an application must handle a WM_PAINT is:
1. Call GetUpdateRect() function to check if there is any area to update. If no area is defined the function should simply return doing nothing.
2. If exist areas to redraw call BeginPaint() to obtain a PAINTSTRUCT that has again the area to paint and a device context of the window.
3. Updates the graphic area
4. Call EndPaint()
5. End of upgrade
In code:

void UpdateGraph(HWND hwnd, HDC dc)
{
  RECT rc;
  GetClientRect(hwnd, &rc);
  DrawText(dc, "Hello!", -1, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  switch(uMsg)
  {
    case WM_PAINT:
    {
      RECT rToPaint;
      if (!GetUpdateRect(hwnd, &rToPaint, FALSE))
          break;    //No regions to update, leave procedure
      PAINTSTRUCT ps;
      BeginPaint(hwnd, &ps);

      //Do graphical paint
      UpdateGraph(hwnd, ps.hdc);

      EndPaint(hwnd, &ps);

      break;        //End of procedure
    }
    ...
  }
}



Why do we get a rectangle of area to print? To make graphic refresh faster, on old devices where the graphic capabilities were reduced it was very effective to limit the updating area (reducing graph actions). Nowadays when graph are managed by GPU's and no more by the CPU, this is still effective when we have complex and big graphs with high-res.
How can you manage partial drawing? Normally you create an independent bitmap and load it in a memory device context. Make all your graphic work on the latter, than when you are ready copy the memory context on the screen, using, BitBlt(), inside the WM_PAINT handler. To force posting of a WM_PAINT message you can call UpdateWindow() or RedrawWindow().
In this case if an update region exist you can manage to copy only the specified part of image onto the screen device reducing update time.


这篇关于如何在win32项目窗口中绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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