How to calculate width and height of different font size ,font style and no of characters ?
-
Hi i want to crate a rectangle based on the font size, font style,and number of charactes.So How can i decide width and height of different font size,font style & number of characters.
-
Hi i want to crate a rectangle based on the font size, font style,and number of charactes.So How can i decide width and height of different font size,font style & number of characters.
You can get the font information from CWnd member GetFont(http://msdn.microsoft.com/en-us/library/cb4schcy(VS.80).aspx) And GetLogFont of the CFont will help get the information about the font. http://msdn.microsoft.com/en-us/library/zhcs623h(v=VS.71).aspx
Величие не Бога может быть недооценена.
-
Hi i want to crate a rectangle based on the font size, font style,and number of charactes.So How can i decide width and height of different font size,font style & number of characters.
This function could calculate the text dimensions :) :
void GetTextSize(CSize& cResultSize, // Receiving size of the text rectangle
CDC* pcDC, // Surface context
CFont* pcFont, // Font for the context
const CString& cszText) // Text to measure
{
if (pcDC && pcFont) {
CGdiObject* pcOldFont = pcDC->SelectObject(pcFont);
cResultSize = pcDC->GetTextExtent(cszText); // dimensions of the text
// at the given surface
// with the given font
pcDC->SelectObject(pcOldFont);
}
}virtual void BeHappy() = 0;
modified on Friday, April 2, 2010 2:12 AM
-
This function could calculate the text dimensions :) :
void GetTextSize(CSize& cResultSize, // Receiving size of the text rectangle
CDC* pcDC, // Surface context
CFont* pcFont, // Font for the context
const CString& cszText) // Text to measure
{
if (pcDC && pcFont) {
CGdiObject* pcOldFont = pcDC->SelectObject(pcFont);
cResultSize = pcDC->GetTextExtent(cszText); // dimensions of the text
// at the given surface
// with the given font
pcDC->SelectObject(pcOldFont);
}
}virtual void BeHappy() = 0;
modified on Friday, April 2, 2010 2:12 AM
-
It could be something like this :) :
/*virtual*/ void CYourView::OnDraw(CDC* pDC)
{
if (pDC) {
CString cszText(_T("Hello World !"));
// Calculate the text rectangle from the start point(100, 100)
// at this surface with the actual set font
CRect cTextRect(CPoint(100, 100),
pDC->GetTextExtent(cszText));
pDC->FillSolidRect(cTextRect, RGB(0, 200, 0)); // Text background
pDC->DrawText(cszText, cTextRect, DT_LEFT | DT_TOP);
}
}virtual void BeHappy() = 0;
-
It could be something like this :) :
/*virtual*/ void CYourView::OnDraw(CDC* pDC)
{
if (pDC) {
CString cszText(_T("Hello World !"));
// Calculate the text rectangle from the start point(100, 100)
// at this surface with the actual set font
CRect cTextRect(CPoint(100, 100),
pDC->GetTextExtent(cszText));
pDC->FillSolidRect(cTextRect, RGB(0, 200, 0)); // Text background
pDC->DrawText(cszText, cTextRect, DT_LEFT | DT_TOP);
}
}virtual void BeHappy() = 0;
actually i want create a Button based on fontsize fontstyle ,no of characters and font type.I dont know what actul text or characters. only i have a options like. No of characters : 50 Font Size : 16 Font Style : Regular/Bold/Italic Font Type : Times New Roman or Arial or Courier How width and height can be decided for the above ? so i can create a Button by assigning width and height easily .
-
actually i want create a Button based on fontsize fontstyle ,no of characters and font type.I dont know what actul text or characters. only i have a options like. No of characters : 50 Font Size : 16 Font Style : Regular/Bold/Italic Font Type : Times New Roman or Arial or Courier How width and height can be decided for the above ? so i can create a Button by assigning width and height easily .
Try it :) :
#define GAP 20
CSize GetButtonDimensions(int iCharCount,
int iFontSize,
const CString& cszFontName)
{
CSize cResultSize(0, 0);CFont cFont;
if (cFont.CreatePointFont(iFontSize * 10, cszFontname)) {
CDC cDC;
if (cDC.CreateCompatibleDC(NULL)) {
CGdiObject* pcOldFont = cDC.SelectObject(cFont);TEXTMETRIC tm = {0}; cDC.GetTextMetrics(&tm); cResultSize.cy = tm.tmHeight + 2 \* GAP; cResultSize.cx = tm.tmMaxCharWidth \* iCharCount + 2 \* GAP; cDC.SelectObject(pcOldFont); } cFont.DeleteObject();
}
return cResultSize;
}virtual void BeHappy() = 0;
-
Try it :) :
#define GAP 20
CSize GetButtonDimensions(int iCharCount,
int iFontSize,
const CString& cszFontName)
{
CSize cResultSize(0, 0);CFont cFont;
if (cFont.CreatePointFont(iFontSize * 10, cszFontname)) {
CDC cDC;
if (cDC.CreateCompatibleDC(NULL)) {
CGdiObject* pcOldFont = cDC.SelectObject(cFont);TEXTMETRIC tm = {0}; cDC.GetTextMetrics(&tm); cResultSize.cy = tm.tmHeight + 2 \* GAP; cResultSize.cx = tm.tmMaxCharWidth \* iCharCount + 2 \* GAP; cDC.SelectObject(pcOldFont); } cFont.DeleteObject();
}
return cResultSize;
}virtual void BeHappy() = 0;
-
i am getting error : [CGdiObject* pcOldFont = cDC.SelectObject(cFont);] 'initializing' : cannot convert from 'HGDIOBJ' to 'CGdiObject *'
Sorry,
..cDC.SelectObject(&cFont);
:)virtual void BeHappy() = 0;
-
Sorry,
..cDC.SelectObject(&cFont);
:)virtual void BeHappy() = 0;