Jump to content
  • Advertisement
Sign in to follow this  
CodaKiller

How to DrawText to a HBITMAP?

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I draw text to a DIBSection? Here is what I got so far but it just draws to the screen:
			BITMAPINFO bmi;
			BYTE *pBits;
			HBITMAP hBitmap;

			bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
			bmi.bmiHeader.biWidth = 300;
			bmi.bmiHeader.biHeight = 300;
			bmi.bmiHeader.biPlanes = 1;
			bmi.bmiHeader.biBitCount = 32;
			bmi.bmiHeader.biCompression = BI_RGB;
			bmi.bmiHeader.biSizeImage = 0;
			bmi.bmiHeader.biXPelsPerMeter = 0;
			bmi.bmiHeader.biYPelsPerMeter = 0;
			bmi.bmiHeader.biClrUsed = 0;
			bmi.bmiHeader.biClrImportant = 0;
			hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (VOID**)&pBits, NULL, 0); 

			RECT rect;
			rect.left = 0;
			rect.top = 0;
			rect.bottom = 100;
			rect.right = 100;
			HDC hDC = GetDC(hWnd);
			HFONT NewFont = CreateFont( 25, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial" );
			HBRUSH NewBrush = CreateSolidBrush(RGB(255, 0, 0));
			SelectObject(hDC, NewFont);
			SelectObject(hDC, NewBrush);
			DrawText(hDC, "See?", 4, &rect, DT_LEFT | DT_WORDBREAK);
			DeleteObject(NewBrush);
			DeleteObject(NewFont);
			ReleaseDC(hWnd, hDC);


[Edited by - CodaKiller on December 1, 2008 8:15:30 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by CodaKiller
Quote:
Original post by Anon Mike
You need to select the bitmap into the dc.


how?
After your GetDC() call:
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBitmap);

Then before ReleaseDC():
SelectObject(hDC, hBmpOld);

Or, use CreateCompatibleDC(NULL) to create a DC then select the bitmap into it at creation time.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by CodaKiller
Quote:
Original post by Anon Mike
You need to select the bitmap into the dc.


how?
After your GetDC() call:
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBitmap);

Then before ReleaseDC():
SelectObject(hDC, hBmpOld);

Or, use CreateCompatibleDC(NULL) to create a DC then select the bitmap into it at creation time.


Here is what I'm doing and I think it works but I don't know since I still need to copy the data to a texture and display it:


HDC hDC = CreateCompatibleDC(NULL);
DWORD* pSrcData = 0;
BITMAPINFO bmi = { sizeof( BITMAPINFOHEADER ), 256, 256, 1, 32, BI_RGB, 0, 0, 0, 0, 0};
HBITMAP hTempBmp = CreateDIBSection( hDC, &bmi, DIB_RGB_COLORS, (void**)&pSrcData, NULL, 0 );
RECT rect;
rect.left = 0;
rect.top = 0;
rect.bottom = 100;
rect.right = 100;
HFONT NewFont = CreateFont( 25, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial" );
HBRUSH NewBrush = CreateSolidBrush(RGB(255, 0, 0));
SelectObject(hDC, NewFont);
SelectObject(hDC, NewBrush);
DrawText(hDC, "See?", 4, &rect, DT_LEFT | DT_WORDBREAK);
DeleteObject(NewBrush);
DeleteObject(NewFont);
ReleaseDC(NULL, hDC);






Thank you very much, now all I need to know is how to get the raw data from it?

EDIT: I think I figured it out, I think it updates pSrcData after I draw to it am I right?

Share this post


Link to post
Share on other sites
Quote:
Original post by CodaKiller
Thank you very much, now all I need to know is how to get the raw data from it?
It's in pSrcData after you've drawn to the bitmap, and after you've called GdiFlush().

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by CodaKiller
Thank you very much, now all I need to know is how to get the raw data from it?
It's in pSrcData after you've drawn to the bitmap, and after you've called GdiFlush().


Though I read it and I kinda understand it I still am not quite sure what GdiFlush exactly?

Share this post


Link to post
Share on other sites
Quote:
Original post by CodaKiller
Though I read it and I kinda understand it I still am not quite sure what GdiFlush exactly?
GDI is free to queue up several draw calls before actually doing any drawing, which can be better for performance. On Vista, all (?) GDI calls go through Direct3D surfaces, and locking a surface is fairly expensive - so it would be better for performance for GDI to lock the surface, draw 10 things, then unlock it instead of locking and unlocking for each individual draw command.
GdiFlush() just tells the GDI to render everything it has queued up now, so that the bitmap contains the data you expect.

The default batch size is probably 1 anyway, but it's always best not to assume these sorts of things.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by CodaKiller
Though I read it and I kinda understand it I still am not quite sure what GdiFlush exactly?
GDI is free to queue up several draw calls before actually doing any drawing, which can be better for performance. On Vista, all (?) GDI calls go through Direct3D surfaces, and locking a surface is fairly expensive - so it would be better for performance for GDI to lock the surface, draw 10 things, then unlock it instead of locking and unlocking for each individual draw command.
GdiFlush() just tells the GDI to render everything it has queued up now, so that the bitmap contains the data you expect.

The default batch size is probably 1 anyway, but it's always best not to assume these sorts of things.


You sir are a very smart man! Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!