I registered a custom button in WM_CREATE but my custom cursor resource is not recognized and I am getting a double arrow cursor when hovering over the custom buttons that I create at run time. Any solutions? I have included resource.h and triplechecked that the custom resource with IC_CURSOR2 is there in the resources.
WNDCLASSEX buttonx; //subclass our custom buttons
buttonx.cbSize = sizeof(WNDCLASSEX);
GetClassInfoEx(NULL,TEXT("BUTTON"), &buttonx);
buttonx.lpszClassName = "CustomButton";
buttonx.hInstance = hInst;
buttonx.hCursor = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR2));
RegisterClassEx(&buttonx);
hButton1 = CreateWindowEx(NULL, "CustomButton", "Close", WS_CHILD
| WS_VISIBLE | BS_OWNERDRAW, 410, 570, 100, 30, hWnd,
(HMENU)ID_BUTTON1, g_hInstance, NULL);
I am getting a double arrow cursor
That sounds like one of the stock cursors, like IDC_SIZENS. Which indicates that you hInst variable is NULL. Maybe you should have used g_hInstance, it isn't clear from the question.
Related
I have a windows app on which when I do a right click a lot of options are shown in the pop up context menu. How can one place an edit text box, in the context menu so that I can type something (and on the change of text box disable few menu items ) . I tried something like this . This doesn't work.
HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"),
WS_CHILD | WS_VISIBLE, 100, 20, 140,
20, NULL, NULL, NULL, NULL);
AppendMenu(MyLocalRightClick, MF_STRING, (UINT)hWndEdit,NULL);
I'm trying to add CBS_OWNERDRAWFIXED style to an existing combobox , my code doesn't work and I have no idea why .
I suspect maybe the expression oldStyle | addedStyle is not valid , but I can't figure out why.
HWND hwnd = CreateWindow(
L"ComboBox",
L"",
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST ,
200,
200,
200,
200,
parentHandle,
(HMENU)1,
GetModuleHandle(NULL),
NULL);
auto comboBoxStyle = GetWindowLongPtr(hwnd, GWL_STYLE);
comboBoxStyle = comboBoxStyle | CBS_OWNERDRAWFIXED;
SetWindowLongPtr(hwnd, GWL_STYLE, comboBoxStyle);
the output is just regular combobox , without any change.
This particular style can only be specified at creation time. The documentation alludes to this when it says:
CBS_OWNERDRAWFIXED
Specifies that the owner of the list box is responsible for drawing its contents and that the items in the list box are all the same height. The owner window receives a WM_MEASUREITEM message when the combo box is created and a WM_DRAWITEM message when a visual aspect of the combo box has changed.
I am working on a program with grouped check box and get confused on how the messages convey through different handles.
IDE:VC++, Win32 API
First, I have a main window which has a handle, say hWnd.
And in the WndProc function under case WM_CREATE, we create the "group button" and the individual 2 check boxes
Note: the first button uses "BS_GROUPBOX" style, and it was created with the handle hGrpButton while its parent handler is hWnd. The second and third button is "BS_AUTORADIOBUTTON" style and it's parent handle is hGrpButton.
If the 2 buttons are not grouped(so their parent hanlder would be hWnd), it is easy to check the status of them. Just simply go to case WM_COMMAND AND use their ID to check with the IsDlgButtonChecked function. After the two check boxes are grouped (their parent handle are not longer hWnd but hGrpButtons), I don't think the case WM_COMMAND will find their IDs since it is looking for the IDs under hWnd.
In short, after 2 check boxes are grouped, I don't know what is the event to monitor them.
case WM_CREATE:
{
/*Group for Radio button for preview/single or batch operation */
hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Select Process Mode:",
WS_VISIBLE | WS_CHILD|BS_GROUPBOX, // Styles
10,280,
350,100,
hWnd,
NULL,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Batch Process Mode",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
10,20,
300,20,
hGrpButtons,
(HMENU)IDC_CHK1,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Single Process Mode (Preview Mode)",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
10,45,
300,20,
hGrpButtons,
(HMENU)IDC_CHK2,
hInst, NULL);
After read Coder_Dan's comment and MSDN article "http://msdn.microsoft.com/en-us/library/bb775947(v=vs.85).aspx#related_topics" about the button types, I finally sort it out.
BS_GROUPBOX is just an "eye candy" and it did no contribute to the group of the radio box!! The critical parameter to control the grouping is the "WS_GROUP"
How to group the radio box: Put the WS_GROUP in the first radio button's style. It will group the current radio button until it see the second WS_GROUP as mentioned by Coder_Dan.
Modification on my previous code
a. use hWnd as parent for all 4 radio buttons
b. Put WS_GROUP in the first and third button style so that we group the 1,2 and 3,4 radio buttons
c. Now you can go to the WM_COMMAND, and listen to the messages from the 4 buttons according to their IDs under the main window's handle hWnd
case WM_CREATE:
{
/*Group for Radio button for preview/single or batch operation */
hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Select Process Mode:",
WS_VISIBLE | WS_CHILD|BS_GROUPBOX,// <----BS_GROUPBOX does nothing on the grouping
10,280,
350,100,
hWnd,
(HMENU)IDC_GRPBUTTONS,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"first radio button",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON|WS_GROUP, // <---- WS_GROUP group the following radio buttons 1st,2nd button
10,520,
300,20,
hWnd, //<----- Use main window handle
(HMENU)IDC_CHK1,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"second radio button",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
10,545,
300,20,
hWnd,
(HMENU)IDC_CHK2,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"third radio button",
WS_VISIBLE | WS_CHILD|BS_AUTOCHECKBOX|WS_GROUP, //<---Start second group for 3rd,4th button
10,570,
300,20,
hWnd,
(HMENU)IDC_CHK3,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"forth radio button",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
500,545,
300,20,
hWnd,
(HMENU)IDC_CHK4,
hInst, NULL);
im starting with Win32 api, im adding a button control to my main window with the flowing code:
HWND boton = CreateWindow(
"BUTTON", //
"Caption", //
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles.
250, // x position.
10, // y position.
100, // Button width.
40, // Button height.
hwnd, // Parent window.
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
how can i assign it an id, so i can get the message on the loop, in the message loop im trying to catch the message as WM_COMMAND but i don't get any result i've tried with WM_NOTIFY too.
To assign it an ID, you have to use the hMenu parameter. If you have specified that the window will be a child (i.e. with WS_CHILD), the hMenu parameter will be interpreted as an integer ID for the window. Also, provide the BS_NOTIFY style.
HWND boton = CreateWindow (
"BUTTON",
WS_TAPSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_NOTIFY,
250, 10, 100, 40,
hwnd,
(HMENU)101, // This becomes the Control ID
(HINSTNACE)GetWindowLong(hwnd,GWL_HINSTANCE),
NULL);
EDIT: Special shout goes out to Heath Hunnicutt for the info on BS_NOTIFY.
In fact, you do not need to specify an ID for the button. The problem is your code is missing a style bit to CreateWindow().
You must specify the style BS_NOTIFY for the parent window to receive notifications from Button controls.
You will then receive window message WM_COMMAND with (HIWORD(w_param) == BN_CLICKED) every time your button is clicked. See the BN_CLICKED documentation for more.
Using a control ID is unnecessary because the BN_CLICKED message will provide you with the control's window handle. Because you are already required to keep track of the window handle in order to properly call DestroyWindow when you receive WM_DESTROY comparing the button's window handle is just as easy as using a control ID.
To set the window id, pass it in as if it were an HMENU:
(HMENU) nChildID
I know how to draw a button in C++ but how would i make an icon on it can someone post source or give reference please? by SendMessage() or if not that way just please paste
Please need easier anwsers without so many files im new a bit
Since you're new, you may also wish to consult the MSDN Library. You can find information on Button Styles (see, specifically, the BS ICON and BS BITMAP styles) and the BM_SETIMAGE message .
If you use MFC then I would recommend you to use the following CButton method SetIcon:
CButton myButton;
// Create an icon button.
myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_ICON,
CRect(10,10,60,50), pParentWnd, 1);
// Set the icon of the button to be the system question mark icon.
myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) );
This works very well.
send BM_SETIMAGE message, and pass loaded image handle to lParam.
button1 = CreateWindowW(L"BUTTON", L"&Button", WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_BITMAP, 20, 50, 80, 25, hwnd, (HMENU) 600, NULL, NULL);
hImg = LoadImageW(NULL, L"test123.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
SendMessageW(button1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImg);
P.S: you need to use BS_BITMAP flag when CreateWindow()