How to create win7 style button using CreateWindowEx [duplicate] - c++

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.

Related

Can't show/hide buttons in the main window of my C++ Win32 app [duplicate]

This question already has answers here:
Access a variable from a different switch case (from WM_CREATE to WM_CTLCOLORSTATIC in the WinApi)
(2 answers)
Closed last month.
I have created a basic button in case: WM_CREATE in the windows procedure with the following.
/*The "new_game_button" is declared as type HWND at the
start of the windows procedure function but not initialized.*/
new_game_button = CreateWindow ( "BUTTON", "New Game",
WS_CHILD | WS_BORDER ,
50, 50, 100, 100,
hwnd, NULL, NULL, NULL);
My intent is to create an instructions and "start new game" button as the first thing in my simple tictactoe app. It will immediately show as expected if I give the parameter WS_VISIBLE.
Further in the same case:WM_CREATE if I use the lines
if (!start_Game){ //global variable default is false
ShowWindow( new_game_button, SW_SHOW);
}
The button will show as expected.
Outside of those two cases I cannot get the button to show at a later stage.
Further, if I use one of those two methods to show the button I can never get it to go away using
ShowWindow ( new_game_button, SW_HIDE);
Once the button is showing, it stays for the duration of the programs execution. Doesn't matter which case. Command/Create/Paint
I have tried using
if (start_Game){
ShowWindow( new_game_button, SW_HIDE);
UpdateWindow ( new_game_button );
//UpdateWindow ( hwnd ); tried this as well
}
inside case WM_CREATE.
I have also tried the same SW_HIDE line inside case: WM_COMMAND where a new game is generated (compiles but doesn't hide the button.)
I have tried declaring the button child window outside the WM_CREATE inside the windows procedure function. Then using WM_CREATE to show the window - works -- still WM_COMMAND will not hide the window.
I have also tried creating the button window inside of case:WM_PAINT which works to show the button but not to get rid of it. I have even tried DestroyWindow which just fails. [returns 0]
In trying to understand the behaviour of the button window - I have found that I cannot get
ShowWindow( new_game_button, SW_SHOW);
to work in the case:WM_COMMAND.
You said (in a code comment in the question).
new_game_button is declared as type HWND at the start of the windows procedure function but not initialized.
Each incoming message means a new call to your window procedure. Variables which are local to a function don't retain their value between calls unless they are marked static.
When your window procedure returned from processing WM_CREATE, you lost the value of new_game_button. When you try to use it during WM_COMMAND processing later, it is uninitialized and your program causes undefined behavior by passing it to ShowWindow.
Every comment helped me solve this which I appreciate. I am too new to give reputation sadly.
case WM_CREATE:
{
new_game_button = CreateWindow ("BUTTON", "New Game",
WS_CHILD | WS_BORDER ,
50, 50, 100, 100,
hwnd, (HMENU) 1, NULL, NULL);
if (!start_Game){
ShowWindow( new_game_button, SW_SHOW);
}
}
break;
After I had an ID for the dialog I am able to use GetDlgItem function to show or hide as I please.
new_game_button = GetDlgItem (hwnd, 1);
ShowWindow( new_game_button, SW_HIDE);

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 :)

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

*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.

Creating a multi-lined textbox/textfield in C++, not VC++

I was wondering how to create a text box that can respond to the "enter" key and create a new line just like the field I am typing in now.
CreateWindow ("edit", 0, WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 130, 20, hwnd, (HMENU) 1000, GetModuleHandle (NULL), NULL);
That is what I have currently, but when I create it, it is a tiny strip and when I press the "Enter" key, it does not jump down to the next line like a paragraph. Any ideas? I am using C++ not VC++ or any .net framework and I am on Windows 7. Thank you.
You need to use the "ES_MULTILINE" flag. See Edit Control Styles