How to resolve aliased text in MFC controls - c++

I'm currently in the second stage of development for a data management project I'm involved with; at the moment, we're in the stage of revamping the GUI (we're going for an office 2007-themed GUI).
At the moment I've only got experience with Dialog-based MFC projects, from which this migrates from, where we drew controls onto the dialog, then got handles to them using the GetDlgItem command, using the IDs of the controls.
Now, we're using SDI, instead of a dialog-based project, and thus, it's required to procedurally create the controls. I've had no problems creating and showing controls in the ChildView area; however, the text is very low quality and aliased. I googled the problem, but was unable to find anything particularly pertinent to the problem, indeed, the only thing which bore any real relevance was using GDI+ to draw anti-aliased text, which is fine for simple text, but I need MFC controls such as CEdit and CListBox.
Here is an image to help illustrate my problem:
I am creating both the static GDI+ text and the MFC control in the OnPaint function, as follows:
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
Gdiplus::Graphics graphics(dc);
static Gdiplus::FontFamily* fontFamily = new Gdiplus::FontFamily( _T("Segoe UI") );
static Gdiplus::Font* font = new Gdiplus::Font( fontFamily, 12, Gdiplus::FontStyle::FontStyleRegular, Gdiplus::Unit::UnitPixel );
static Gdiplus::SolidBrush* solidBrush = new Gdiplus::SolidBrush( Gdiplus::Color::RoyalBlue );
Gdiplus::PointF point( 10, 10 );
graphics.SetTextRenderingHint( Gdiplus::TextRenderingHint::TextRenderingHintAntiAlias );
graphics.DrawString( _T("Hello, World!"), 13, font, point, solidBrush );
// TODO: Add your message handler code here
CEdit* pEditBox = new CEdit();
pEditBox->CreateEx( WS_EX_CLIENTEDGE, _T("EDIT"), _T("Why is this text so poor?"), WS_BORDER | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 200), this, UINT_MAX-9 );
// Do not call CWnd::OnPaint() for painting messages
}
At the moment, I am wondering if it necessary to derive each of the controls and change their rendering methods to use GDI+ antialiased text. Hopefully their is an easier way to resolve this problem.
I will be extremely grateful for any help/advice.
Thanks in advance!
EDIT: Just for reference, I used to following to resolve my problem:
CFont* pFont = new CFont();
pFont->CreatePointFont( 120, _T("Segoe UI") );
CEdit* pEditBox = new CEdit();
pEditBox->CreateEx( WS_EX_CLIENTEDGE, _T("EDIT"), _T("Test Edit"), WS_BORDER | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 200), this, UINT_MAX-9 );
pEditBox->SetFont( pFont );
Alternatively, the following allows for the default font for the window to be used:
CEdit* pEditBox = new CEdit();
pEditBox->CreateEx( WS_EX_CLIENTEDGE, _T("EDIT"), _T("This text is no longer poor"), WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 200), this, UINT_MAX-9 );
if( ::IsWindow( pEditBox->GetSafeHwnd() ) )
{
::SendMessage( pEditBox->GetSafeHwnd(), WM_SETFONT, (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT), FALSE );
}

Change the font used by the edit box, it's just the default one that looks so ugly.
If you want to get the "standard" font, see e.g. SystemParametersInfo with SPI_GETNONCLIENTMETRICS argument. Of course, you can use any other font if you want to.

Related

How do I create a button in c++ with my own custom image?

I've been looking for a way to make a picture a button in c++ for a few hours now.. I've found stuff on using bitmaps, what what i am currently using to display the image is GDI+, because i want to use jpg/png files.
This is how i created my Image with gdiplus:
void Example_DrawImage(HDC hdc) {
Graphics graphics(hdc);
image = Image::FromFile(L"Path:/To/Image");
myBitmap = dynamic_cast<Bitmap*>(image);
Pen pen(Color(0, 0, 0, 0), 2);
graphics.DrawImage(image, 10, 10);
}
I converted that to a bitmap with:
myBitmap = dynamic_cast<Bitmap*>(image);
Then, in WM_CREATE I created a button which it's standard style is a Windows XP Button:
button = CreateWindow(TEXT("button"), TEXT("Hello"),
WS_VISIBLE | WS_CHILD | BS_BITMAP,
10, 10, /* x & y*/ 80, 25, /*width & height*/
hwnd, (HMENU) 1, hInstance, NULL
);
button is globally defined as HWND button;
All I want is to have a button that is a jpg picture. I tried doing it manually by seeing if a mouse click was inside a certain area, but I could not find a way to find the position of the Image.

c++ win32 edit box cursor not flashing

I'm a newbie in windows programming and am continuously running into different kinds of problems, most of which I have been able to solve by myself.
My problem at hand is the caret (or cursor) shown in text areas. The thing that indicates where you are typing your text? Well it is shown, at least, but it doesn't blink like it should.
I have an EDIT box created in WM_CREATE like so:
case WM_CREATE:
{
if(!logged) {
HWND userField = CreateWindow(
"EDIT", // Predefined class; Unicode assumed
NULL, // Button text
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Styles
C_WIDTH/2 - 80, // x position
C_HEIGHT - 240, // y position
160, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
// initialize NONCLIENTMETRICS structure
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
// obtain non-client metrics
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
// create the new font
HFONT hNewFont = CreateFontIndirect(&ncm.lfMessageFont);
// set the new font
SendMessage(userField, WM_SETFONT, (WPARAM)hNewFont, 0);
}
}
break;
That is all code concerning the edit box. I'm sorry if I'm not being clear enough or my supply of code is lacking; I'm unsure of what parts of code is relevant here and what are irrelevant. I don't think I should paste my whole code here, either.
The problem, again, is that the caret in the textbox (userField) does not blink.
Please ask for more details if you need them.
Using your code, I didn't get a flashing caret. But then i added:
SetFocus( userField );
and voilà, a flashing caret :-)
This may not be the problem the OP was experiencing, but I was experiencing the same symptom, and I'm posting my solution here in case someone else experiences this problem...
In short, if you subclass an edit control, and handle the WM_SETFOCUS event, you need to call DefSubclassProc() or your caret won't show up. Presumably, you can call ShowCaret() yourself, but you're probably safer just calling DefSubclassProc() in case there's other processing that needs to happen.
After playing around - making my code a bit tidier and stuff - I accidentally solved this on my own
I changed
HWND userField = CreateWindow(
"EDIT", // Predefined class; Unicode assumed
NULL, // Button text
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Styles
C_WIDTH/2 - 80, // x position
C_HEIGHT - 240, // y position
160, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
Into
HWND userField = CreateWindow("EDIT", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
C_WIDTH/2 - 80, C_HEIGHT - 240, 160, 25, hwnd, NULL, g_hInstance, NULL);
The only difference there is the hInstance: in the first code it was apparently wrong. I changed it into my global reference of hInstance.

create a CAxWindow inside a BHO (C++)

I'm having trouble opening a new CAxWindow inside my BHO, I can see the request to "microsoft.com" being fired but no window is shown.
I tried many different ways, this is my last, anyone has a clue what's wrong?
thanks.
CAxWindow m_axWindow;
CRect rc;
HWND wndIE = NULL;
m_pWebBrowser->get_HWND((SHANDLE_PTR*)&wndIE);
GetWindowRect(wndIE, &rc);
CSize sz = CSize(100, 200);
CRect rcPage = new CRect(10, 10, 10, 10);
m_axWindow.Create(wndIE, rcPage, _TEXT("http://www.microsoft.com"), WS_POPUP | WS_TABSTOP, 0, 0U, 0);
HRESULT hRet = m_axWindow.QueryControl(IID_IWebBrowser2, (void**)&m_pWebBrowser);
I think m_axWindow.Create creates a child window. Check its style for WS_CHILD after that call. You probably need to create a plain popup top-level window first, then create a CAxWindow using that popup window as parent, not the wndIE. Make sure to do ShowWindow on the pop-up, too.

how to change the position of the child window inside the parent window and show the toolbar?

I have the following code which passes a window handler form OpenCV window to win32 handler, therefore I can show the grabbed images from camera to the screen and the images will show as a child window of my main API.
but the problem is that when I want to add a tooldbar to my program, the image window handler comes at the top of the toolbar. how can I sort this out?
//create a window and set the handler from openCV to win32
cv::namedWindow("test",cv::WINDOW_AUTOSIZE);
hWnd2 = (HWND) cvGetWindowHandle("test");
hParent = ::GetParent(hWnd2);
::SetParent(hWnd2, hWnd);
::ShowWindow(hParent, SW_HIDE);
_liveCapturing=true;
lastPicNr = 0;
SetWindowTextW(hStatus, L"Live Capturing ... ");
if(FullScreen()){
::ShowWindow(hWnd, SW_MAXIMIZE);
}
code for the toolbar :
HWND CreateToolbar(HWND hwnd){
HWND hTbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | CCS_TOP , 0, 0, 0, 0, hwnd, (HMENU)12, GetModuleHandle(NULL), NULL);
SendMessage(hTbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
TBBUTTON tbb[3];
TBADDBITMAP tbab;
tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hTbar, TB_ADDBITMAP, 0, (LPARAM)&tbab);
return hTbar;
}
Probably you have found the solution a long time ago, but i want to post my anwers in case other users need it.
You can simply add the OpenCV window with the same code you have to a child window in your window (which you set it position in advance). For example you can add it to a static text window (label) ...
If you want to move the OpenCV window, call SetWindowPos() with the desired coordinates.
SetWindowPos(hWnd2, 0, 0, 30, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

How to print bold string in C++?

I got an old application which was written in a C++. I have 0 experience with it but I am suppose to make some changes in app. One of them is to change some text. Problem is that part of updated text needs to be bold, but i have no idea how to do that. I googled but with no much success. Only think I now is to go to new line with \nand new tab with \t.
Any clever advise?
EDIT:
Example of code:
BEGIN
STRING1 "First Example"
STRING2 "Second Example"
And place where STRING1 is used:
// WelcomeTip ---------------------------------------------//
LPSTR idsWelcomeTip = (LPSTR)GlobalAlloc(GPTR, sizeof(CHAR) * 4098 );
LoadString( waveInDlg->hInstance, STRING1, idsWelcomeTip, 4098 );
waveInDlg->hwndWelcomeTip = CreateWindow(
"STATIC",
idsWelcomeTip,
WS_CHILD | WS_VISIBLE | SS_LEFT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
waveInDlg->hwnd,
NULL,
waveInDlg->hInstance,
NULL
);
SetWindowLongPtr(waveInDlg->hwndWelcomeTip, GWLP_USERDATA ,(LONG)waveInDlg );
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT , (WPARAM)waveInDlg->hFontDefault , TRUE );
ShowWindow(waveInDlg->hwndWelcomeTip, SW_HIDE);
GlobalFree( (HGLOBAL)idsWelcomeTip );
Thanks,
Ile
There is no concept of bold text in C++, there may be in a particular device that displays character text, for example rich-text-format or HTML tagging or a terminal screen. The latter usually involves sending some "escape sequence" relevant to that particular terminal.
OK, I've knocked up some code that should give an overview of what you're after, I've not managed to compile it as I'd need to write a lot more to test, but it should point you in the right direction:
// Create the font you need
LOGFONT lf;
zeromemory(&lf, sizeof(LOGFONT))
lf.lfHeight = 20; // 20 pixel high font
lf.lfWeight = FW_BOLD;
strcpy(lf.lfFaceName, "Arial");
HFONT hFont = ::CreateFondIndirect(&lf);
// Set the control to use this font
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT, (WPARAM)hFont, NULL);
I hope this helps.
Please go through the below link for help
http://msdn.microsoft.com/en-us/library/dd162499(VS.85).aspx
Yes, you have to override WM_PAINT in your dialog class and call drawtext function.
Use DrwaText API in WM_PAINT message handler.dc.DrawText (_T ("Hello, MFC"), -1, &rect,
DT_SINGLELINE ¦ DT_CENTER ¦ DT_VCENTER);
use DrawTextEx method.
For more inforamtion go through the follwoing link
ms-help://MS.MSDNQTR.v90.en/gdi/fontext_4pbs.htm