How to increase size of font in MessageBox c++ builder - c++

*I'm using c++ builder (bcb6)
I would like enlarge font size of message box without creating custom message box.
I searched over google and find that it's possible to use by WM_SETFONT.
I tried doing:
HWND hWnd = CreateWindowEx(0,"WC_DIALOG","Questions!", WS_OVERLAPPEDWINDOW | WS_VISIBLE,400, 100, 100, 100,NULL, NULL, NULL, NULL);
HFONT hFont=CreateFont (30, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
SendMessage (hWnd, WM_SETFONT, WPARAM (hFont), TRUE);
MessageBox(hWnd,message.c_str(),"Info",MB_OK | MB_ICONINFORMATION);
And it dorsn't work..
Any suggestions please?
Your help is very appreciated.

There is nothing specific in C++Builder for changing font in a MessageBox. Your options are:
Make a custom dialog
Use window hooks to change the default MessageBox, via the Windows API.
Both of these are described in more detail with linked examples on this thread. If you have tried something from that thread and it didn't work then post the code that you tried as a new question.

Related

How to make tab key work in C++ Win32 API

I am a noob for C++ Win32 API. I am trying to make a textbox (edit control). In the textbox, I want to make the tab key act as it would in notepad, i.e., it would advance the cursor to the next tab stop in a textbox. I made a simple edit control with some scrollbars, auto-scrolling and multiline styles. I want the tab key to advance the cursor to the next tab stop. But it doesn't happen. The tab key doesn't do anything, literally nothing. It acts as if it wasn't assigned to do any task inside an edit control.
If anyone knows how to do that, I would be really grateful to him.
Here's the full code which I used for creating an edit control :-
HWND hTb = CreateWindow
(
L"Edit", L"LOL",
WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_HSCROLL | WS_VSCROLL | ES_WANTRETURN | WS_TABSTOP,
0, 0, LOWORD (lp), HIWORD (lp),
hWnd, NULL, NULL, NULL
);
I am creating hTb in the WM_CREATE message of the WndProc. This edit control is just used for resizing it and nothing complicated. Here's the code :-
case WM_SIZE :
{
MoveWindow (hTb, 0, 0, LOWORD (lp), HIWORD (lp), TRUE);
break;
}
Thanks in advance.
Edit controls will not receive tabs if "the dialog manager" portion of Win32 is engaged. For a modeless dialog box, the way you will see dialog box semantics showing up in a window is if the application's main message loop is filtering messages with the API call IsDialogMessage. IsDialogMessage will reroute tab presses to the window rather than the focused control, in this case the edit box.
To fix the problem use a standard message loop. That is,
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
not
while(GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
You can read more about the dialog manager on Raymond Chen's blog: here, here, and here

How to update the current state for window after showing/hiding controls?

I'm a beginner and today is my first day in learning to create Windows application.
I have two buttons.
#define BUTTON_SW 1
#define BUTTON_SW2 2
HWND Button1;
HWND Button2;
Button1 = CreateWindow("button", "Enter", WS_VISIBLE | WS_CHILD, 215, 10, 40, 25, hwnd, HMENU(BUTTON_SW), NULL, NULL);
Button2 = CreateWindow("button", "You'll be gone", WS_VISIBLE | WS_CHILD, 260, 10, 95, 25, hwnd, HMENU(BUTTON_SW2), NULL, NULL);
When Button1 is clicked, how can I hide Button2 or make it lose its WS_VISIBLE flag and reflect current situation correctly, like this?
LONG style = GetWindowLong(Button2,GWL_STYLE);
style = style | WS_VISIBLE; // style = style & ~WS_VISIBLE
SetWindowLong(Button2,GWL_STYLE,style);
This works very well. However, once WS_VISIBLE flag is assigned, button still stays invisible until the first mouseclick on it.
Vice versa, when I use style = style & ~WS_VISIBLE; once WS_VISIBLE flag is removed, button becomes passive (unclickable) but stays visible.
How to fix this? Tried many things that I've found online but couldn't fix it. Also please don't suggest me to buy a decent book, I don't have the money for now.
(P.S: ShowWindow function with SW_HIDE/SW_SHOW somehow doesn't work for me, perhaps I'm using it wrong. Can you help me on how I can hide this Button2 correctly? I'm trying the following command, but nothing happens.)
ShowWindow(GetDlgItem(Button2, 2), SW_HIDE);
#Edit: I've noticed that when I minimize the app and maximize it
again, the state of button updates. But how will it automatically
update the state?
This should work
ShowWindow(Button2, SW_HIDE);
or
ShowWindow(GetDlgItem(DialogHWND, BUTTON_SW2), SW_HIDE);
GetDlgItem needs HWND of the parent window (dialog) as first argument.
In order to a window to reflect changes you must ask the OS to do it.
Learn about RedrawWindow and the invalidated area.
Note that some actions, like resizing or restoring from minimized, automatically makes the OS to invalidate the region and redraw it.
Use:
RedrawWindow(Button2,NULL,NULL,RDW_INVALIDATE | RDW_INTERNALPAINT);

Visual style set on BS_CHECKBOX button overrides NM_CUSTOMDRAW

I am looking to create a checkbox in the C++ WinAPI with a custom bitmap resource as the actual check box.
Unfortunately, when I create a WM_NOTIFY-compatible checkbox (That actually deals with NM_CUSTOMDRAW in a meaningful way), it seems to work just fine except for one thing: The checkbox seems to fade when changing states. This has to do with the visual style, it seems, so I simply unset the visual style with SetWindowTheme(chkLogging, _T(" "), _T(" "));.
HWND chkLogging = CreateWindowEx(
NULL,
_T("button"),
_T("Export to log file"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX | BS_NOTIFY,
300, 100, 100, 20,
hWnd, (HMENU)IDC_EXPORTLOG, NULL, NULL);
CheckDlgButton(hWnd, IDC_EXPORTLOG, BST_CHECKED);
SetWindowTheme(chkLogging, _T(" "), _T(" "));
Unfortunately, setting the theme for that HWND will override the NM_CUSTOMDRAW rendering. I have tested this by removing the SetWindowTheme line and trying with the default theme, and the checkbox NM_CUSTOMDRAW worked like a charm.
This typically wouldn't be an issue, but it actually works as seemingly intended for normal buttons.
Any assistance on this matter would be greatly appreciated :)

win32 - Using the default button font in a button

I'm creating a small WinAPI application in C++. I am trying to create a button on my form by using the code:
HWND hwndButton = CreateWindow(
TEXT("BUTTON"),
TEXT("Click Here"),
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, // Styles
10,
10,
100,
30,
hwnd,
NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
This code is based off of an MSDN sample. My issue is that it uses a bold font on the button like this:
When I want to use the standard font like this:
I already have the preprocessor directive at the top of my file to enable visual styles.
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
What steps should I take to use the standard system wide font?
Thanks
GetStockObject isn't the recommended way of retrieving the GUI font (it doesn't take themes into account, and different fonts can be chosen for buttons, menus, etc). Instead you should use SystemParametersInfo (see Remarks section of GetStockObject).
It is not recommended that you employ this method to obtain the current font used by dialogs and windows. Instead, use the SystemParametersInfo function with the SPI_GETNONCLIENTMETRICS parameter to retrieve the current font. SystemParametersInfo will take into account the current theme and provides font information for captions, menus, and message dialogs.
NONCLIENTMETRICS metrics = {};
metrics.cbSize = sizeof(metrics);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
HFONT guiFont = CreateFontIndirect(&metrics.lfCaptionFont);
// When you're done with the font, don't forget to call
DeleteObject(guiFont);
There's no such thing as default system wide font for controls, initially you get a control created with "System" font, that's what you see on first picture. When button is created as part of a dialog, it uses a font from dialog template, so using something like "MS Shell Dlg" with appropriate size + WM_SETFONT on a button should give you the same result as on picture 2. Note that there's no physical MS Shell Dlg font on a system, it's mapped to particular font according to registry settings.
Common control manifest has nothing to do with this, behavior has not changed with comctl32 version 6.
The default GUI font is stored in DEFAULT_GUI_FONT, and can be retrieved via
GetStockObject(DEFAULT_GUI_FONT);
To set the font of the button you can use:
HWND yourButton; // use CreateWindow or anything else to get this
SendMessage(yourButton, WM_SETFONT, (LPARAM)GetStockObject(DEFAULT_GUI_FONT), true);
A convenient way of doing this without calling SendMessage on every single child window manually is to use the EnumChildWindows function with the following callback function -
Create the callback function EnumChildProc:
BOOL CALLBACK EnumChildProc(
HWND hWnd,
LPARAM lParam
)
{
HFONT hfDefault = *(HFONT *) lParam;
SendMessageW(hWnd, WM_SETFONT, (WPARAM) hfDefault, MAKELPARAM(TRUE, 0));
return TRUE;
}
At the start of your (w)WinMain function, add the code:
NONCLIENTMETRICSW ncm;
HFONT hfDefault;
ZeroMemory(&ncm, sizeof(NONCLIENTMETRICSW));
ncm.cbSize = sizeof(NONCLIENTMETRICSW);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, FALSE);
hfDefault = CreateFontIndirectW(&ncm.lfMessageFont);
Then, after the ShowWindow call, add this line:
EnumChildWindows(hWnd, EnumChildProc, (LPARAM)&hfDefault);

How to create win7 style button using CreateWindowEx [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
winapi CreateWindowEx -> create button with user system styles?
Hi,
I am kinda new to WinAPI and C++ and I am using Visual Studio 2010. I want to create some buttons in the main window. So there are two questions:
1) can I use a dialog window created with resource editor as a main window so that I wouldnt have to create all controls "by hand" in the "post-WM_CREATE message" section?
2) (If I cant use resource script made window with buttons as a main window) When I "hand make" button using CreateWindowEx like this:
case WM_CREATE:
{
HFONT buttonFont = CreateFont(-11, 0, 0, 0, 400, FALSE, FALSE, FALSE, 1, 400, 0, 0, 0, fontButtonFont);
HWND bMainOK = CreateWindowEx(
0,
WC_BUTTON,
szOkButton,
WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_PUSHBUTTON,
24, 200, 75, 23,
hWnd,
0,
hInst,
0);
SendMessage(bMainOK, WM_SETFONT, (WPARAM)buttonFont, FALSE);
}
I get very ugly oldstyle button. How do I make it look like Win7/Vista button? Or better how do I make it behave as system style setting says (when using XP get XP style button, when using Vista get Vista style button etc.)?
Thanks
You need to link a manifest to your app that specifies v6 common controls. Websearch will do the rest for you.