Win32 popup menu width [duplicate] - c++

I'm creating a custom control in wxWidgets that displays a menu as part of it, and currently working on the Windows side of things. wxWidgets doesn't have a way of setting the width of a menu. It just makes the window as wide as the longest string plus a few pixels on either side.
In the API, there is a way to get the actual Windows API menu handle. Does the Windows API have a method of setting the width of a menu other than just calculating it on its own based on the width of the string?

With the handle of the menu, you can cycle through the menu items and call SetMenuItemInfo, indicating that you want to owner-draw the menu items. When you do this, the window that the menu is attached to will receive the WM_MEASUREITEM message, which you would then respond to by setting the dimensions required for the menu. It is here you can set your width.
Of course, this means you have to subclass the windows message handler for the window that contains the menu.

First of all, try to obtain the HWND to the menu:
(1) WM_DRAWITEM (2)get the HDC (3)WindowFromDC(),
then you can arbitrarily adjust aspects of the menu.
NOTE: don't send WM_QUIT WM_CLOSE to the menu, or you'll effectively shut down your computer with no clue.

Related

window procedures for dialog box controls

I've created a dialog box with buttons in it using Visual Studio's resource editor, and I want the buttons to display different icons under different circumstances.
I gather that in order to set an icon for a button, I need to send a BM_SETIMAGE message to a window procedure for that button.
I'm not clear on what handling such a message would involve, but I haven't gotten to the point of figuring that out, because I don't see how to associate a window procedure to a button in the first place. I'm used to associating a window to its window procedure by passing a pointer to the procedure into the function that initializes the window, but that doesn't seem to apply here since the buttons are initialized automatically when I initialize the dialog box.
I thought there might be a way to specify window procedures for controls in the resource editor, but if there is, I'm missing it.

Reliably identifying any window's client area

I am working on a program that will replicate, and then extend the functionality of Aero Snap.
Aero Snap restores a maximized window, if the user "grabs" it's title bar, and I am having difficulties identifying this action.
Given a cursor position in screen coordinates, how would I check if the position is within the window's title bar? I am not really at home in the Win32 API, and could not find a way that works reliably for complicated scenarios such as:
Note that tabs that chrome inserts into the title bar. Office does something similar with the quick launch menu.
title bar hits are via the message "non client" messages - ie the area of a window that is not the client (or inner) window.
WM_NCLBUTTONDOWN is probably the message you want to trap.
You also probably want to set a window hook to hook mouse messages, if its the NC message you want, you handle it your way, if not - pass it on to the message chain.
Edit: if Chrome is using the DwmExtendFrameIntoClientArea calls to draw the tabs, then you will need to use WM_NCHITTEST.

Simulating right click on system tray icon and clicking on context menu in C++

I am a Python developer with a little knowledge of C++.
With that said, I would like to understand how I can right click on a system tray icon, and click on one of the options on the context menu.
I have looked around the internet and was unable to find something that can get me the location of the system tray icons relative to the 'Notification Area'. Also, I can get the Button text of the tray icon.
I get the handle of ToolbarWindow32 using FindWindowEx.
I have tried to send WM_RBUTTONDOWN and WM_RBUTTONUP to the handle of ToolbarWindow32 with the X and Y coordinates, using SendMessage and nothing happens.
I am completely oblivious as to how I can right click the icon, and get the context menu information, and using that, click on one of the options.
After my research there's no way to send a click message to a system tray icon, at least not through any API that I tried. The best way to do it and this is the way I'm following is the following:
You Send the message TB_GETBUTTON to the toolbar.
This will retrieve you an "idCommand" for the button you retrieve so you can use a loop to get all the "idCommand", which is found in the TBBUTTON structure.
With the idComman you can send a message to the toolbar button with the toolbar handle to get the dimensions of the icon with the TB_RECT message.
Once you know the dimensions of the button you just need to get the dimensions of the toolbar which is simple because it's just a window you make a cal to GetWindowRect
Last step is now you want to send the click you make a call to win32api.mouse_event with x being: the left bound of the toolbar + half the width of the icon and y being: the top bound of the toolbar + half the height of the icon. (so you're sending the click to the center).
That's it hope it helps!
I've asked a similar question and answered it here.

How to send message from one dialog to another?

I was given a task.
First dialog based application has 4
buttons (up, down, left, right).
Second dialog based application has
two controls (e.g. text area, button).
When on the first dialog I click
"left" button - controls on the second
dialog must move to the left.
But unfortunately I don't know Win32 API at all.
How can I implement it? What kind of Win32 API mechanism should I be using?
Thanks.
If you got handles (HWND) to the controls on the other dialog then you can use the Win32 MoveWindow api call to move them.
When reading the api documentation it might be useful to remember that everything (buttons, list boxes, combo boxes etc) is a window...

How to change height of owner drawn listbox dynamically in windows mobile?

I am trying to create owner drawn listbox on windows mobile 6.1 I have specified LBS_OWNERDRAWFIXED style while creating listbox control. I am handling WM_MEASUREITEM and WM_DRAWITEM accordingly.
I want to change height of listbox item whenever its font changes (I change it using WM_SETFONT after creating control). The problem is that WM_MEASUREITEM message gets sent only once. I need a way to generate WM_MEASUREITEM message again so that I can set height of item to height of font. I have seen the article http://www.codeguru.com/Cpp/controls/listview/advanced/article.php/c1013/, but it uses MFC, while I am developing this control directly using WINAPI. Some articles mention that resizing the control generates WM_MEASUREITEM message again. However, it is not getting generated in my case.
Please let me know is there any way to achieve this in windows mobile.
There is nothing particularly MFC in that code. OnSetFont is, simply, handling WM_SETFONT. MeasureItem is handling, in the MyListCtrl, the WM_MEASUREITEM sent to its parent.
As for generating a new WM_MEASUREITEM when you resize the window can't you just do a SendMessage when handling the WM_SIZE/WM_SIZING?