Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
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
Posted
Comments
[no name] 5-Jul-15 14:25pm    
http://winapi.freetechsecrets.com/win32/WIN32GetDC.htm
Member 11593571 5-Jul-15 15:22pm    
what's with the link
[no name] 5-Jul-15 15:46pm    
If you actually read it, it would answer your "question"

1 solution

You can use Drawtext(), or TextOut(), in any moment, you just need the device context:
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);
}

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:
C++
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.
 
Share this answer
 
v6
Comments
Member 11593571 5-Jul-15 15:54pm    
i did the way you said above but still no result, isn't there any other better way to show a small piece of text in window
Frankie-C 5-Jul-15 16:12pm    
The sample works. What means no result? You didn't got the text on the window?
This is the way it have to be done, there no other *simpler* ways.
Member 11593571 5-Jul-15 18:30pm    
yeah you are right i didn't got any result means no text no where in window, and i can't do it the way you did, i mean you called UpdateGraph() function in wm_paint handler but i can't do that cause there are lots of other stuff in my function where drawtext is, i mean the function that calls DrawText()
Mohibur Rashid 5-Jul-15 21:08pm    
The suggested code must have to work. And the text will be written in the center of your window. You better debug through your code and make sure you are getting proper HDC and HWND of the example.
Member 11593571 6-Jul-15 12:48pm    
my dear brother guess you didn't understand me completely, let me explain it a little clearly, there is a textbox in my hWnd window, what i want it, is to when the user types something and pressed enter it should get the text and draw it in the window somewhere or something like that. and thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900