MFC edit control strange behaviour - mfc

I ran into an awkward behaviour of CEdit when setting it's font: for a certain font size, letters like 'g' or 'j' have the bottom part missing, regardless of CEdit's rect height. Here are two examples:
CFont *ctrlFont = new CFont();
ctrlFont ->CreatePointFont(80, "Arial Black");
CEdit m_editName;
m_editName.SetFont(ctrlFont);
with this result:
but for
ctrlFont ->CreatePointFont(100, "Arial Black");
everything is fine
As you can observe, the CEdit's rect height is larger than the text's height in both cases. The parent control is a CDialog; the font is set on ::OnInitDialog and CEdit's size is set with SetWindowPos method on ::OnShowWindow. What could cause this, and how should i handle it?
Edit: i've tried #rrirower 's suggestion, and now i'm confussed; adding the CEdit's CDC to CFont's initialization changed the text's mask alot (you may not see it from the beggining, but i have other edit's with the old font on the same page and there's a big difference):
ctrlFont1->CreatePointFont(80, "Arial Black", m_editName.GetDC());

Call CreateFont() with all parameters
font.CreateFont(
12, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial"))); // lpszFacename

Related

How can we increase or decrease the size of dialog box so that it can also change the size of all the button and menu also in MFC?

void CMFCApplication6Dlg::OnFontIncreasefont()
{
/*LPCTSTR text = _T("Hello World");
SetDlgItemTextW(IDD_MFCAPPLICATION6_DIALOG,text);*/
Correct rect;
GetClientRect(&rect);
x = rect.Height();
int y = rect.Width();
cout << x << endl;
cout << y << endl;
SetWindowPos(NULL, 200, 300, x + 150, y + 10, SWP_NOMOVE);
CFont font;
font.CreateFont(
fon + 5, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFacename
fon = fon + 5;
GetDlgItem(IDC_STATIC1)->SetFont(&font);
GetDlgItem(IDC_STATIC2)->SetFont(&font);
GetDlgItem(IDOK)->SetFont(&font);
GetDlgItem(IDC_EDIT2)->SetFont(&font);
}
I want to change the diagonal size in such way that all the button and menu size should change on the same time.
I have tried above code but it will take all the id of menu or button manually , but what i need is that that should be done automatically.
I have tried above code but it will take all the id of menu or button manually , but what i need is that that should be done automatically.
You can automate that with a simple loop:
font.DeleteObject();
font.CreateFont(...);
for (CWnd* wnd = GetWindow(GW_CHILD); wnd != NULL; wnd = wnd->GetWindow(GW_HWNDNEXT))
{
wnd->SetFont(&font);
}

MFC how to change font when I move the window?

Basically, I am trying to display the output and then update the font of the display when the window is moved. I am trying to use onMove() method to determine if window is moved. However, my problem is somehow, when my program is initialized, it calls the onMove() method instantly. It does not even wait for the window to be moved. Therefore, the font is already changed before the window is moved. What I am trying to do is that, change the font of the display after the window is moved. Honestly, I don't know if there is a way to call onMove() after the window is initialized. In any case, my program will invoke the onMove(). I don't think this is going to be my solution. Does anybody have any other ideas how could I achieve that? Here is a sample execution of What I am trying to do and when I run the program, the test output is already displayed with new font and color. I would like to do that after the window is moved.
void CMainFrame::OnMove()
{
CFrameWnd::OnMove(x, y);
CDC *dc;
dc = GetDC();
CRect rect;
dc->SetTextColor(RGB(255, 128, 0));
CBrush brush;
CFont oFont;
oFont.CreateFont(25, 0, 0, 0, 400, false, false,
0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, _T("Times New Roman"));
dc->SetBkMode(TRANSPARENT);
dc->SelectObject(oFont);
dc->FillRect(&rect, &brush);
Invalidate();
UpdateWindow();
dc->TextOutW(0, 200, _T("TESTTTT"));
}

Replicate DOS console font (CP437) in Win32 app

I am converting an old Console app to Win32, and want to replicate the font from the console. The existing codebase forces me to work in C/C++. I'm trying to use CreateFont and CreateFontIndirect to construct an equivalent.
The console font settings are:
I think I understand that raster fonts are not TTF and not directly supported, thanks to this post, How to use DOS font in WinForms application, and arbiter's answer.
I want to construct a fixed font that matches the 12 pixel height and 8 pixel width.
Here's some of the code I've tried so far.
HDC hdc = GetDC(w_child);
// "A 12-point font is 16 pixels tall." -- https://msdn.microsoft.com/en-us/library/windows/desktop/ff684173(v=vs.85).aspx
// "An n-point font is 4/3*n pixels tall"?
// I want 12 pixels tall, so 9-point, right?
int PointSize = 9;
int nHeight = -MulDiv(PointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(w_child, hdc);
HFONT hf = CreateFont(
-12, //nHeight, // Logical height
0, //nHeight * 2/3, // Logical avg character width
0, // Angle of escapement (0)
0, // Baseline angle (0)
FW_DONTCARE, // Weight (0)
FALSE, // Italic (0)
FALSE, // Underline (0)
FALSE, // Strikeout (0)
ANSI_CHARSET, // Character set identifier ??
OUT_DEFAULT_PRECIS, // Output precision
CLIP_DEFAULT_PRECIS, // Clip precision (0)
DEFAULT_QUALITY, // Output quality
FIXED_PITCH, // Pitch and family
"Lucida Console" // Pointer to typeface name string
//"Terminal"
//"Courier New"
);
*/
// Getting stock font, creating an indirect as a logical modification,
// seems to work better.
// ANSI_FIXED_FONT, with lf.lfHeight = 10, results in something clear and
// readable, but a little too large.
// And changing lfHeight seems to have no impact.
HFONT hf = (HFONT)GetStockObject(ANSI_FIXED_FONT);
// SYSTEM_FIXED_FONT at lfHeight = 10 is way too big
HFONT hf = (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
// DEVICE_DEFAULT_FONT at lfHeight = 8 is way too big.
HFONT hf = (HFONT)GetStockObject(DEVICE_DEFAULT_FONT);
LOGFONT lf;
GetObject(hf, sizeof(LOGFONT), &lf);
lf.lfHeight = 12;
HFONT nf = CreateFontIndirect(&lf);
SendMessage(w_child, WM_SETFONT, (WPARAM)nf, TRUE);
Seems like success in my situation:
CreateFont(12, 8, 0, 0, 0, FALSE, 0, 0, OEM_CHARSET, OUT_RASTER_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH, L"System");

How to clear drawing area for ActiveX OCX control?

I have some trouble drawing an ActiveX control. In the screenshot below you see the control after a resize in the VB6 IDE. The control's outline from before the resize is still shown on the left-hand side of the control:
Here is the code that draws a black ellipsis with a red Z:
void CzFileIoXCtrl::OnDraw(CDC* pdc,
const CRect& rcBounds,
const CRect& rcInvalid)
{
if (!pdc)
{
return;
}
pdc->SetBkMode(TRANSPARENT);
pdc->SelectObject(CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH)));
pdc->Ellipse(rcBounds.left, rcBounds.top,
rcBounds.left + rcBounds.Width(),
rcBounds.top + rcBounds.Height());
HFONT font = CreateFont(int(rcBounds.Height() * 0.7),
int(rcBounds.Width() * 0.5),
0, 0, FW_BLACK, FALSE, FALSE, FALSE,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
FF_DECORATIVE, NULL);
pdc->SelectObject(font);
pdc->SetTextColor(RGB(255, 0, 0));
DRAWTEXTPARAMS params = { sizeof(DRAWTEXTPARAMS), 1, 0, 0, 1 };
RECT bounds = rcBounds;
CString z(L"Z");
pdc->DrawTextEx(z, &bounds, DT_CENTER | DT_VCENTER | DT_SINGLELINE, &params);
}
How can I clear the drawing area?
I managed to reproduce this in the vb form editor. It looks like the problem comes because you do not draw anything outside the ellipse. So, you can draw a rectangle in the entire area like this before drawing anything in OnDraw().
pdc->FillRect( rcBounds, &CBrush(TranslateColor( AmbientBackColor() )) );
I tested this and is working fine.

Set font size of CLinkCtrl

Is it possible to set the font size of a CLinkCtrl? I tried the following code, but it does nothing!
EDIT: Oops, forgot to include my attempt:
CFont* aboutFont=nullptr;
BOOL AboutDlg::OnInitDialog(){
SpecialDlg::OnInitDialog();
if(aboutFont==nullptr){
aboutFont=new CFont();
aboutFont->CreateFont(
20, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
L"Arial");
}
((CLinkCtrl*)GetDlgItem(ID_WEBSITE_LINK))->SetFont(aboutFont,true);
return true;
}
I dont know what's wrong with your code or dialog resources. But I have tried setting the font of a CLinkCtrl (even without using variable or typecasting), and succeeded.
I created font using CFont::CreatePointFont.
You first check the resource ID, also check if you create font with other approaches.