How to get textwidth base on font height and font name? - c++

From this post,
Let's say I want to calculate the textwidth in point of "Meet".
I use the following code:
HFONT hFont, hOldFont;
double fontheight = 12; <--font height in point
HDC hDC = GetDC(0);
// get a 12-point font and select it into the DC
int currentY = MulDiv((int)fontheight, GetDeviceCaps(hDC, LOGPIXELSY), 72);
hFont = CreateFont(-currentY, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0,
0, 0, 0, 0, L"Arial"));
hOldFont = SelectFont(hDC, hFont);
TEXTMETRIC textMetric;
GetTextMetrics(hDC, &textMetric); <--this function return char height=18/charwidth=7
double fontwidth = fontheight/textMetric.tmHeight* textMetric.tmAveCharWidth; //rescale font width base on font height
CString t = L"Meet";
fontwidth =(double) (fontwidth* t.GetLength()); <-fontwidth*4.
Where am I going wrong, because based on the actual image it is quite small?

Related

MFC, Drawing text in memory context (printing)

I got stuck with a problem - I need to create a bitmap in memory, draw some text i it and save it as a BMP file and then print out the bitmap with physical printer. I can do this drawing in the dialog window context - it works fine. But when I try to do the same drawing in printer context the text doesn't appear. I really can't figure out why it is so. Please, help me guys. Thanks in advance. Here is the code:
void CMy2Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CPrintDialog pd(false);
if (pd.DoModal()==IDOK)
{
CDC PrintDC;
HDC hdc = pd.CreatePrinterDC();
PrintDC.Attach(hdc);
DOCINFO infStru;
::ZeroMemory (&infStru, sizeof (DOCINFO));
CString title="Print test";
infStru.cbSize = sizeof (DOCINFO);
infStru.lpszDocName=title;
infStru.lpszOutput=NULL;
PrintDC.StartDoc(&infStru);
PrintDC.StartPage();
{
CRect r, r2;
CBitmap memBMP, * pOldBitmap;
CPaintDC dc(this);
CDC memDC, *pDC = &memDC;
CFont font, * pOldFont;
int width = 2000;
int height = 1500;
int textwidth = 300;
int textheight = 150;
int oldMapMode = 0;
int oldbkmode = 0;
int i, j;
LOGFONT logFont, lf;
COLORREF oldTextColor;
memset(&logFont, 0, sizeof(logFont));
logFont.lfHeight = 16;
logFont.lfWidth = 0;
logFont.lfEscapement = 0;
logFont.lfOrientation = 0;
logFont.lfWeight = FW_NORMAL;
logFont.lfItalic = FALSE;
logFont.lfUnderline = FALSE;
logFont.lfStrikeOut = 0;
logFont.lfCharSet = ANSI_CHARSET;
logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logFont.lfQuality = DEFAULT_QUALITY;
logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
strcpy(logFont.lfFaceName, "Arial");
if(memDC.CreateCompatibleDC(&PrintDC)) {
if (memBMP.CreateCompatibleBitmap(&PrintDC, width, height)) {
pOldBitmap = pDC->SelectObject(&memBMP);
pDC->FillSolidRect(0, 0, width, height, RGB(200, 200, 200));
oldTextColor = pDC->SetTextColor(RGB(255,0,0));
oldMapMode = pDC->SetMapMode(MM_LOMETRIC);
oldbkmode = pDC->SetBkMode(TRANSPARENT);
lf = logFont;
lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(pDC->GetSafeHdc(), LOGPIXELSY), 72);
//lf.lfHeight = 100;
font.CreateFontIndirect(&lf);
pOldFont = pDC->SelectObject(&font);
r.left = 10;
r.top = 10;
r.right = r.left + textwidth;
r.bottom = r.top + textheight;
r.top *= -1;
r.bottom *= -1;
pDC->MoveTo(r.left, r.top);
pDC->LineTo(r.right, r.top);
pDC->LineTo(r.right, r.bottom);
pDC->LineTo(r.left, r.bottom);
pDC->LineTo(r.left, r.top);
pDC->DrawText("qwerty", &r, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
pDC->SetMapMode(oldMapMode);
pDC->SetTextColor(oldTextColor);
pDC->SetBkMode(oldbkmode);
PrintDC.BitBlt(10, 10, width, height, pDC, 0, 0, SRCCOPY);
pDC->SelectObject(pOldBitmap);
pDC->SelectObject(pOldFont);
font.DeleteObject();
memBMP.DeleteObject();
pDC->DeleteDC();
}
}
}
PrintDC.EndPage();
PrintDC.EndDoc();
PrintDC.Detach();
DeleteDC(hdc);
}
}
If SetMapMode(MM_LOMETRIC) is used on memory DC, then memory DC has to be drawn upside down when using BitBlt to copy to printer/display DC. The width/height will have to be adjusted as well. Just use the default map mode (MM_TEXT). Use SetMapMode(MM_LOMETRIC) when you are drawing on printer DC and you want that specific measurement units.
void CMy2Dlg::OnButton1()
{
//create the bitmap
int w = 600, h = 400;
CClientDC dc(this);
CBitmap bmp;
CDC memdc;
memdc.CreateCompatibleDC(&dc);
bmp.CreateCompatibleBitmap(&dc, w, h);
auto oldbmp = memdc.SelectObject(bmp);
//draw on bitmap
memdc.FillSolidRect(0, 0, w, h, RGB(200, 200, 200));
memdc.SetTextColor(RGB(255, 0, 0));
CRect rc(0, 0, w, h);
memdc.DrawText(L"qwerty", &rc, 0);
dc.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);//optional: draw the bitmap on dialog
CPrintDialog pd(false);
if(pd.DoModal() == IDOK)
{
CDC PrintDC;
HDC hdc = pd.GetPrinterDC();
PrintDC.Attach(hdc);
DOCINFO docinfo = { sizeof(docinfo) };
docinfo.lpszDocName = L"Print test";
PrintDC.StartDoc(&docinfo);
PrintDC.StartPage();
PrintDC.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);
PrintDC.EndPage();
PrintDC.EndDoc();
}
dc.SelectObject(oldbmp);
}

How to draw RGB pixel data from memory with GDI in C++

I have a pointer to RGB data (640x480x3 bytes) that I want to draw into a window using BitBlt or something else equally fast. How do I convert the RGB data into something usable with BitBlt (for example).
Here is what I tried so far (without success)
unsigned char *buf = theVI->getPixels(0);
int size = theVI->getSize(0);
int h = theVI->getHeight(0);
int w = theVI->getWidth(0);
HDC dc = GetDC(hwnd);
HDC dcMem = CreateCompatibleDC(dc);
HBITMAP bmp = CreateBitmap(w, h, 1, 24, buf);
SelectObject(dcMem, bmp);
BitBlt(dc, 0, 0, w, h, dcMem, 0, 0, SRCCOPY);
Thanks
UPDATE: this is the working code...
HDC dc = GetDC(hwnd);
BITMAPINFO info;
ZeroMemory(&info, sizeof(BITMAPINFO));
info.bmiHeader.biBitCount = 24;
info.bmiHeader.biWidth = w;
info.bmiHeader.biHeight = h;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biSizeImage = size;
info.bmiHeader.biCompression = BI_RGB;
StretchDIBits(dc, 0, 0, w, h, 0, 0, w, h, buf, &info, DIB_RGB_COLORS, SRCCOPY);
ReleaseDC(hwnd, dc);
You can render your bytes ( that are so called DIB device independent bitmap ) to HDC using StretchDIBits API function.
And yet check DIB article in MSDN

Why drawing on memory bitmap doesn't work as expected?

I'm trying to draw on memory bitmap and have weird results. This is a code I have:
HDC hdcScreen = GetDC(hWnd);
HDC hdcMemory = CreateCompatibleDC(hdcScreen);
HBITMAP hMemoryBitmap = CreateCompatibleBitmap(hdcScreen, w, h);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hMemoryBitmap);
Graphics *memoryGraphics = Graphics::FromHDC(hdcMemory);
SolidBrush brush(Color(255, 255, 0, 0));
memoryGraphics->FillRectangle(&brush, 0, 0, w, h);
POINT dcOffset = { 0, 0 };
SIZE size = { w, h };
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 255;
bf.AlphaFormat = AC_SRC_ALPHA;
UpdateLayeredWindow(hWnd, hdcScreen, &dcOffset, &size, hdcMemory, &dcOffset, 0, &bf, ULW_ALPHA);
delete memoryGraphics;
ReleaseDC(hWnd, hdcScreen);
SelectObject(hdcMemory, hBitmapOld);
DeleteDC(hdcMemory);
DeleteObject(hMemoryBitmap);
And it gives me semitransparent! Why? I clearly specified Color(255, 255, 0, 0) in brush?

How to draw text without window

I'm trying to display text on the screen, on top of everything, not clickable, without having any window. The idea is to be able to show notifications. I'm somewhat close from what I want, but a really weird problem just showed. This is the code:
#include <Windows.h>
int main(void){
HDC hdc = ::GetDC(0);
RECT rect;
SetTextColor(hdc, RGB(0, 0, 255));
SetBkMode(hdc, TRANSPARENT);
SetBkColor(hdc, RGB(0, 0, 0));
auto hFont = CreateFont(40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, L"Verdana");
auto hTmp = (HFONT)SelectObject(hdc, hFont);
rect.left = 40;
rect.top = 10;
while (true){
DrawText(hdc, L"THIS IS A TEXT", -1, &rect, DT_SINGLELINE | DT_NOCLIP);
Sleep(1);
}
DeleteObject(SelectObject(hdc, hTmp));
::ReleaseDC(0, hdc);
return 0;
}
and this is what happens when I change the text settings from red to blue and size 80 to 40:
For some reason I can still see the old text, after rerunning the program, which tells me that I misunderstood something. Is there a better, cleaner way to do this?
EDIT: I checked windows notifications and that's NOT a solution. Imagine you are playing a full screen game, and want to know if an email arrives. Another essential thing is that it cannot be clickable, so that clicks by mistake don't minimize your game. How annoying is that skype popup that minimizes your application when you get a call?
You have bypassed all of the windows/client controls and so the system has no idea that this area needs to be cleared. You need to tell it manually, especially since you are not using the windows message notification mechanism.
Before drawing to it, you want to invalidate that part of the screen and tell windows to redraw it:
::InvalidateRect (0, &rect, false); // Redraw without erasing. If doesn't help, try true
::UpdateWindow (0);
while (true)...
I ended up running into a lot of problems, while trying to make this work. If someone ends up visiting this page looking for an answer to the same problems I ran into, I hope you have an easier time than I had. This is the code that worked for me:
#include <Windows.h>
INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpCmdLine, INT nCmdShow)
{
// Define and initialize variables
HDC hdc;
HDC hdcMem;
HBITMAP hbmMem;
HANDLE hOld;
RECT rect;
SIZE sz;
int win_width = 0;
int win_height = 0;
int font_size = 20;
int location_x = 40;
int location_y = 40;
int border = font_size / 4;
int duration = 10000; // In miliseconds. The notification will always stay up more time
wchar_t* font_face = L"Consolas";
wchar_t message[100];
// Save command-line arguments to message; They are showed by the notification
MultiByteToWideChar(0, 0,
lpCmdLine,
strlen(lpCmdLine),
message,
100
);
message[strlen(lpCmdLine)] = L'\0';
// Acquire screen
hdc = ::GetDC(0);
//Create necessary font
HFONT hFont = CreateFont(font_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, font_face);
HFONT hTmp = (HFONT)SelectObject(hdc, hFont);
// Calculate size of the text
GetTextExtentPoint32(hdc, message, wcslen(message), &sz);
win_width = sz.cx;
win_height = sz.cy;
rect = { 0, 0, sz.cx, sz.cx };
// Create an off-screen DC for double-buffering
hdcMem = CreateCompatibleDC(hdc);
hbmMem = CreateCompatibleBitmap(hdc, win_width + 2 * border, win_height + 2 * border);
// Configure off-screen DC
SetBkMode(hdcMem, OPAQUE);
SetTextColor(hdcMem, RGB(125, 125, 255));
SetBkColor(hdcMem, RGB(0, 0, 0));
SelectObject(hdcMem, hFont);
hOld = SelectObject(hdcMem, hbmMem);
// Draw loop
for (int i = 0; i < duration; i++)
{
// Draw into hdcMem
DrawText(hdcMem, message, -1, &rect, DT_SINGLELINE);
// Transfer the off-screen DC to the screen
BitBlt(hdc, location_x, location_y, win_width + 2 * border, win_height + 2 * border, hdcMem, -5, -5, SRCCOPY);
// Don't eat all the cpu!
Sleep(1);
}
// Delete notification right after time expires
::InvalidateRect(0, &rect, false);
::UpdateWindow(0);
// Free-up the off-screen DC
SelectObject(hdcMem, hOld);
DeleteObject(hbmMem);
DeleteDC(hdcMem);
// Release created objects
DeleteObject(SelectObject(hdc, hTmp));
::ReleaseDC(0, hdc);
return 0;
}
It can still be improved, a lot. The only thing that appears is the rectangle with the notification.
The arguments that you pass to the program will be shown as a message. Everything related with hdcMem was implemented to avoid flickering. I can't yet change the background of the bigger rectangle.

Finding width of text

I am setting the font for a control like this:
HDC hdc = GetDC(NULL);
int lfHeight = -MulDiv(szFont, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
HFONT font = CreateFont(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Font.c_str());
SendMessage(hwnd,WM_SETFONT,(WPARAM)font,0);
The control is a static. How would I find the width of the text in the static for a given string?
Use GetTextExtentPoint32. You'll need to select the font into the DC first.
CDC::GetTextExtent() and CDC::GetOutputTextExtent() should help.