User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,380 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,683 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Jul 13th, 2008
Views: 1,477
Put the following code in a C++ Header File.
The following function is to make it way more easier to draw text onto the client area of a window.

Now the only thing you need to do is include the C++ Header file containing this function into any C++ Source file and use the following code to dynamically write text onto the screen:
DoText(hwnd, 70, 50, "This is my text!!!");

70 and 50 are the values of the X and Y coordinates of the position to drawm the text in.

Tell me if this way is obsolete, returns errors, or in any other way is not a good thing to use.
Tell me if this helps.
cplusplus Syntax
  1. void DoText(HWND hwnd, int x, int y, LPSTR text)
  2. {
  3. HDC hdc;
  4. PAINTSTRUCT ps;
  5. hdc = BeginPaint(hwnd, &ps);
  6. TextOut(hdc, x, y, text, strlen(text));
  7. EndPaint(hwnd, &ps);
  8. }
Comments (Newest First)
killdude69 | Newbie Poster | Jul 15th, 2008
Well, thank you WilliamHemsWorth for your contribution to my snippet. I did not know the options, I just thought it might help newcomers write out text.
williamhemsworth | Posting Pro in Training | Jul 14th, 2008
No thanks.. that way is very in-efficient, and doesn't give you the options of text size, font, colour and many other styles. Also, say you want to write 100 lines using that function, you would have constructed 200 un-needed variables and made 200 un-needed function calls.

What I would use is something more like:

  1. static COLORREF oldTextRGB = 0, oldBackRGB = 0xFFFFFF;
  2. void Write(HDC hdc, int x, int y, char *text,
  3. /* Optional params */ COLORREF txtRGB = oldTextRGB,
  4. COLORREF backRGB = oldBackRGB, HFONT font = NULL) {
  5.  
  6. if (font) SelectObject(hdc, font);
  7. if (txtRGB != oldTextRGB) {
  8. SetTextColor(hdc, txtRGB);
  9. oldTextRGB = txtRGB;
  10. }
  11. if (backRGB != oldBackRGB) {
  12. SetBkColor(hdc, backRGB);
  13. oldBackRGB = backRGB;
  14. }
  15.  
  16. TextOut(hdc, x, y, text, strlen(text));
  17. }

Even though this may be slightly slower in some cases, it offers many more options and is worth it in the end. But to be honest I think I would just do it all manually to save time and problems
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC