How to change the "FONT" for CEdit? - visual-c++

How to adjust the "font" for the following:(solved)
fontSize 40
BOLD
Italicized
Thanks
The following fails:(now works)
CFont *myFont = new CFont();
myFont->CreateFont( 40, 0, 0, 0, FW_HEAVY, true, false,
0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FIXED_PITCH|FF_MODERN, _T("Courier New") );
CEdit *ed1 = new CEdit();
ed1->Create(WS_VISIBLE | WS_BORDER,CRect(200,100,500,140),this,16);
ed1->SetFont(myFont);

Be sure not to delete myFont as long as your edit control is still around.

Here what i did, and it run for me :
CFont m_Font;
CEdit m_EditBox
CClientDC dc(this);
int nFontSize = 40;
int nHeight = -((dc.GetDeviceCaps(LOGPIXELSY) * nFontSize) / 72);
m_Font.CreateFont(nHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("Courier New"));
// Create edit box
if (m_EditBox.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN | ES_READONLY, CRect(0, 0, 100, 100), this, 0x102) == FALSE) return FALSE;
// Set font
m_EditBox.SetFont(&m_Font, TRUE);

Related

Printing text and bitmap using MFC

I need to print some text (using font specified), than print a bitmap, using MFC. I can draw text on bitmap, than print this bitmap, using code below - but I need to print text, and than print bitmap in the bottom. The bitmap must be loaded from file.
CFont j1;
j1.CreateFont(
120, // nHeight
120, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
RUSSIAN_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial"); // lpszFacename // lpszFacename
int w = 600, h = 400;
CClientDC dc(this);
CBitmap bmp;
CDC memdc;
memdc.CreateCompatibleDC(&dc);
bmp.CreateCompatibleBitmap(&dc, w, h);
if (!bmp.Attach(::LoadImage(
::GetModuleHandle(NULL), "D:\\UPM\\BMP\\Login.bmp", IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE))) {
AfxMessageBox(_T("Error loading bitmap!")); return;
}
BITMAP bm;
bmp.GetBitmap(&bm);
auto oldbmp = memdc.SelectObject(bmp);
CFont* pOldFont = memdc.SelectObject(&j1);
//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("qwerty\nrtrtrt\nttttt", &rc, DT_WORDBREAK | DT_EXPANDTABS | DT_CENTER);
///pDC->DrawText(dpu, strRect, DT_WORDBREAK | DT_EXPANDTABS | DT_CENTER);
//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 = "Print test";
PrintDC.StartDoc(&docinfo);
PrintDC.StartPage();
PrintDC.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);
PrintDC.EndPage();
PrintDC.EndDoc();
}
dc.SelectObject(oldbmp);
CClientDC dc(this);
dc.DrawText(...);
...
PrintDC.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);
You are drawing text on display's "client DC", then drawing a bitmap on a "printer DC".
Use instead a paint function for everything, and a paint_bitmap function to make things easier.
void paint_bitmap(CDC& dc, CBitmap &bmp, CRect rc)
{
CDC memdc;
memdc.CreateCompatibleDC(&dc);
auto oldbmp = memdc.SelectObject(&bmp);
dc.BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &memdc, 0, 0, SRCCOPY);
memdc.SelectObject(oldbmp);
}
void paint(CDC& dc)
{
CBitmap bmp;
if (!bmp.Attach(...))...
dc.DrawText(L"text", &rc, ...);
rc.OffsetRect(0, 200);
paint_bitmap(dc, bmp, rc);
}
...
CPrintDialog pd(false);
if (pd.DoModal() == IDOK)
{
HDC hdc = pd.GetPrinterDC();
if (hdc)
{
...
dc.StartPage();
paint(dc); //<- use this for printer or display
dc.EndPage();
}
To print directly to default printer:
CPrintDialog pd(false);
if (pd.GetDefaults() && pd.m_pd.hDC)
{
CDC dc;
dc.Attach(pd.m_pd.hDC);
DOCINFO docinfo = { sizeof(docinfo) };
docinfo.lpszDocName = L"Print test";
dc.StartDoc(&docinfo);
dc.StartPage();
paint(dc);
dc.EndPage();
dc.EndDoc();
}
else
{
MessageBox(L"no default printer...");
}
or if you want a particular printer which you are sure is there, use
HDC hdc = CreateDC(L"WINSPOOL", L"Microsoft Print to PDF", NULL, NULL);
if (hdc)
{
CDC dc;
dc.Attach(hdc);
...
//DeleteDC(hdc); CDC will take care of delete
}

How to change the font style of the text of a common control like button, label, etc. in c++ using GDI+?

I am trying to change the font style of a button using GDI+ but I don't know how to do it.
My button -
HWND btn = CreateWindow(L"BUTTON", L"My button", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 50, 100, 300, hWnd, NULL, NULL, NULL);
I have initialized GDI+ -
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
And also Shutdown it -
GdiplusShutdown(gdiplusToken);
Any help please
Common controls don't use GDI+. You need use LOGFONT.
HWND TextBox;
TextBox = CreateWindowW(WC_EDIT, L"", WS_BORDER | WS_VISIBLE | WS_CHILD, 330, 130, 300, 100, hWnd, NULL, NULL, NULL);
LOGFONT logfont;
ZeroMemory(&logfont, sizeof(LOGFONT));
logfont.lfCharSet = DEFAULT_CHARSET; //Font
logfont.lfHeight = 20; //Font Height
logfont.lfWidth = 23; //Font Width
logfont.lfWeight = 300; //Font Weight
//If want
logfont.lfItalic = true; // Italic (Boolean)
logfont.lfUnderline = true; // Underline (Boolean)
HFONT hFont = CreateFontIndirect(&logfont);
SendMessage(TextBox, WM_SETFONT, (WPARAM)hFont, TRUE);

Mismatch between print preview and real print output

I want to print a bitmap. To avoid printing small bitmap I set CScrollView mode as MM_LOMETRIC with sizes 3830 x 1995. I have created the bitmap and made the bitblt to the screen. There were everythig just like I want on the screen and on the print preview but when I printed the document I've got very bad result.
The same picture on print preview.
It seems to me that printer does not see a bitmap the same way as print preview does.
Pay attantion that the first ractangle puts directly on the DC and memDC puts into it.
Are there any ideas how to fix this mismatch between print previw and the real printing?
Project files
void OnDraw()
{
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* OldPen = pDC->SelectObject(&pen);
CRect rcView;
GetClientRect(rcView);
int iClientWidth = rcView.right;
int iClientHeight = rcView.bottom;
int iMemWidth = 1900;
int iMemHeight = 950;
CDC memDC;
CBitmap memBitmap;
memDC.CreateCompatibleDC(pDC);
memBitmap.CreateCompatibleBitmap(pDC, iMemWidth, iMemHeight);
memDC.SelectObject(&memBitmap);
memDC.SetMapMode(MM_LOMETRIC);
CPen pen1;
pen1.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
memDC.SelectObject(&pen1);
CBrush brBK;
brBK.CreateSolidBrush(RGB(255, 255, 255));
memDC.SelectObject(&brBK);
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = iMemWidth;
rc.bottom = iMemHeight;
memDC.FillRect(&rc, &brBK);
memDC.Rectangle(rc.left, rc.top, rc.right, -rc.bottom);
memDC.MoveTo(0, 0);
memDC.LineTo(1900, -950);
memDC.MoveTo(0, -950);
memDC.LineTo(200, -750);
CFont font;
font.CreateFont(
50, // 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"));
memDC.SelectObject(&font);
memDC.TextOut(100, -100, _T("Hello"));
pDC->BitBlt(10, -10, iMemWidth, -iMemHeight, &memDC, 0, 0, SRCCOPY);
font.DeleteObject();
brBK.DeleteObject();
memDC.DeleteDC();
memBitmap.DeleteObject();
pen.DeleteObject();
pen1.DeleteObject();
}
void OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
sizeTotal.cx = 3830;
sizeTotal.cy = 1995;
SetScrollSizes(MM_LOMETRIC, sizeTotal);
}

How do I draw a pixmap to root window with xcb?

I'm trying to write a window manager with a wallpaper switcher:
void smoke_change_background(smoke_wm_state_t *config, char *desktop) {
struct smoke_desktop_list_element_t *node = smoke_desktop_list_node(config->tree, desktop);
if (node->background != 0) {
xcb_intern_atom_reply_t *xpmap = xcb_intern_atom_reply(config->connection,
xcb_intern_atom(config->connection,
false,
strlen("_X_ROOTPMAP_ID"),
"_X_ROOTPMAP_ID"),
NULL);
xcb_intern_atom_reply_t *esetr = xcb_intern_atom_reply(config->connection,
xcb_intern_atom(config->connection,
false,
strlen("ESETROOT_PMAP_ID"),
"ESETROOT_PMAP_ID"),
NULL);
xcb_get_property_reply_t *xrepl = xcb_get_property_reply(config->connection,
xcb_get_property(config->connection,
0,
config->screen->root,
xpmap->atom,
XCB_ATOM_PIXMAP,
0,
32),
NULL);
uint32_t *xp = (uint32_t*) xcb_get_property_value(xrepl);
xcb_get_property_reply_t *erepl = xcb_get_property_reply(config->connection,
xcb_get_property(config->connection,
0,
config->screen->root,
esetr->atom,
XCB_ATOM_PIXMAP,
0, 32),
NULL);
uint32_t *ep = (uint32_t*) xcb_get_property_value(erepl);
xcb_change_property(config->connection,
XCB_PROP_MODE_REPLACE,
config->screen->root,
xpmap->atom,
XCB_ATOM_PIXMAP,
32, 1,
(void*) &node->background);
xcb_change_property(config->connection,
XCB_PROP_MODE_REPLACE,
config->screen->root,
esetr->atom,
XCB_ATOM_PIXMAP,
32, 1,
(void*) &node->background);
xcb_free_pixmap(config->connection, *xp);
xcb_free_pixmap(config->connection, *ep);
uint32_t values[1];
values[0] = node->background;
xcb_change_window_attributes(config->connection, config->screen->root, XCB_CW_BACK_PIXMAP, values);
xcb_clear_area(config->connection, false, config->screen->root, 0, 0, 0, 0);
xcb_flush(config->connection);
}
}
This code should change the root's pixmap and it should be displayed in my Xephyr window, but it is not. What's wrong?

How do I make a button caption bold in MFC?

I have a button on an MFC dialog. How can I make the text bold?
class CYourDialog : CDialog
{
public:
virtual BOOL OnInitDialog(); // override
private:
CButton m_button;
CFont m_font;
};
BOOL CYourDialog::OnInitDialog()
{
__super::OnInitDialog();
CFont* font = m_button.GetFont();
LOGFONT logFont;
font->GetLogFont(&logFont);
logFont.lfWeight = FW_BOLD;
m_font.CreateFontIndirect(&logFont);
m_button.SetFont(&m_font);
return TRUE; // => system will set input focus to the first control item in the dialog box; (0 => you set the focus to a control of your choice)
}
You can create a new CFont and call WM_SETFONT on the button. Something like this:
// note: m_font is a class variable of type CFont
m_font.CreateFont(10, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, "Arial")
GetDlgItem(IDC_BUTTON1)->SendMessage(WM_SETFONT, WPARAM(HFONT(font)), 0);

Resources