Jump to content
  • Advertisement
Sign in to follow this  
Jouei

Win32 text on a bitmap

This topic is 4160 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

Hello i was writing a custom control and i ran into a slight problem and cant seem to fix it i draw a bitmap with win32 api for the buttons face. and i try and put text on it but the text just over writes the bitmap can someone give em an example of how one would go about drawing it properly heres the rough of what i got.

static LRESULT CustButton_OnPaint(CButtonProp *ccp, WPARAM wParam, LPARAM lParam)
{
    HDC          hdc;
    PAINTSTRUCT  ps;
    HANDLE       hOldFont;
    TCHAR        szText[200];
    RECT         rect;
    SIZE		 sz;
    int			 x, y;

    // Get a device context for this window
    hdc = BeginPaint(ccp->hwnd, &ps);

    // create a device context for the skin
    HDC dcSkin = CreateCompatibleDC(hdc);

    // select the skin bitmap
    HBITMAP hOldBmp = (HBITMAP)SelectObject(dcSkin, ccp->Up);
    BitBlt(hdc, 0,0,213,36, dcSkin, 0,0, SRCCOPY);

    ReleaseDC(ccp->hwnd,dcSkin);
    DeleteDC(dcSkin);
    // Set the font we are going to use
    hOldFont = SelectObject(hdc, ccp->hFont);
	
    // Find the text to draw
    GetWindowText(ccp->hwnd, szText, sizeof(szText));
   
    // Work out where to draw
    GetClientRect(ccp->hwnd, &rect);

	// Find out how big the text will be
	GetTextExtentPoint32(hdc, szText, lstrlen(szText), &sz);

	// Center the text
	x = (rect.right  - sz.cx) / 2;
	y = (rect.bottom - sz.cy) / 2;
    
    // Draw the text
    ExtTextOut(hdc, x, y, ETO_OPAQUE, &rect, szText, lstrlen(szText), 0);
    BitBlt(hdc, 0,0,213,36, Font, 0,0, SRCCOPY);
    // Restore the old font when we have finished
    SelectObject(Font, hOldFont);

    // Release the device context
    EndPaint(ccp->hwnd, &ps);

    return 0;
}


Any thought as you can probaly tell im not to good with the win32 api for drawing and such. Regards Jouei.

Share this post


Link to post
Share on other sites
Advertisement
I have code that did exactly what you wanted. Its on a PC with a dead hard-drive (primary yes!) so if I can recover it I will post it here. Might be a few days but if you don't have it solved by Friday or I haven't posted you can send me a PM.

Share this post


Link to post
Share on other sites
Ok thank you very much. i still havent quite figured it out but i am still working away at it.

Share this post


Link to post
Share on other sites
Well i got it more or less working so heres the code in case anyone is woundering what i did.



static LRESULT CustButton_OnPaint(CButtonProp *ccp, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HANDLE hOldFont;
TCHAR szText[200];
RECT rect;
SIZE sz;
int x, y;

// Get a device context for this window
hdc = BeginPaint(ccp->hwnd, &ps);

// create a device context for the skin
HDC dcSkin = CreateCompatibleDC(hdc);

// select the skin bitmap
HBITMAP hOldBmp = (HBITMAP)SelectObject(dcSkin, ccp->Up);
BitBlt(hdc, 0,0,213,36, dcSkin, 0,0, SRCCOPY);

SetTextColor(hdc,RGB(255,0,0));
SetBkMode(hdc, TRANSPARENT);
SelectObject(hdc, ccp->hFont);
TextOut(hdc,0,0,szText,strlen(szText));

// Release the device context
EndPaint(ccp->hwnd, &ps);

return 0;
}




Regards Jouei.

Share this post


Link to post
Share on other sites
I wrote the code almost 2 years ago but that looks somewhat familiar. I have to order another piece (north bridge fan died apparently). So if you still need more help let me know in PM.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!