I tried following all the examples I could find but I'm missing something so I will put all the pieces here for others to view. FYI - I'm modifying the MFC Feature Pack example Slider.
I see the layer button (not a string or down arrow) if I select the button (click) I see the depress motion and get to the OnLayers() function with the ID of the button. I almost looks like the ReplaceButton() is doing nothing.
Any ideas?
Thanks
For the toolbar I've added ID_LAYERS_1
IDR_MAINFRAME TOOLBAR 16, 15
BEGIN
BUTTON ID_FILE_NEW
BUTTON ID_FILE_OPEN
BUTTON ID_FILE_SAVE
SEPARATOR
BUTTON ID_SLIDER
SEPARATOR
BUTTON ID_EDIT_CUT
BUTTON ID_EDIT_COPY
BUTTON ID_EDIT_PASTE
SEPARATOR
BUTTON ID_FILE_PRINT
SEPARATOR
BUTTON ID_APP_ABOUT
SEPARATOR
BUTTON ID_LAYERS_1
END
my menu is
IDR_LAYERS MENU
BEGIN
POPUP "Layers"
BEGIN
MENUITEM "0", ID_LAYERS_1
MENUITEM "1", ID_LAYERS_2
MENUITEM "2", ID_LAYERS_3
END
END
and the code
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
ON_WM_CREATE()
ON_WM_CLOSE()
ON_COMMAND(ID_SLIDER, OnSlider)
ON_COMMAND(ID_VIEW_CUSTOMIZE, OnViewCustomize)
ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset)
ON_REGISTERED_MESSAGE(AFX_WM_TOOLBARMENU, OnToolbarContextMenu)
ON_UPDATE_COMMAND_UI_RANGE(ID_LAYERS_1, ID_LAYERS_3, OnUpdateLayers)
ON_COMMAND_RANGE(ID_LAYERS_1, ID_LAYERS_3, OnLayers)
END_MESSAGE_MAP()
CMFCToolBarMenuButton* CreateLayerButton()
{
CMenu menu;
VERIFY(menu.LoadMenu(IDR_LAYERS));
CMFCToolBarMenuButton* pLayerButton = NULL;
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
if (pPopup != NULL)
{
HMENU hMenu = pPopup->GetSafeHmenu();
pLayerButton = new CMFCToolBarMenuButton(ID_LAYERS_1, hMenu, -1, NULL, FALSE);
}
return pLayerButton;
}
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM)
{
UINT uiToolBarId = (UINT)wp;
if (uiToolBarId == IDR_MAINFRAME)
{
CSliderButton btnSlider(ID_SLIDER);
btnSlider.SetRange(0, 100);
m_wndToolBar.ReplaceButton(ID_SLIDER, btnSlider);
// layer button/menu
CMFCToolBarMenuButton* pLayerButton = CreateLayerButton();
m_wndToolBar.ReplaceButton(ID_LAYERS_1, *pLayerButton);
delete pLayerButton;
}
return 0;
}
void CMainFrame::OnUpdateLayers(CCmdUI* pCmdUI)
{
//pCmdUI->SetCheck(true);
}
void CMainFrame::OnLayers(UINT id)
{
}
I think you are using it the wrong way. Try this way:
CMenu menu;
VERIFY(menu.LoadMenu(IDR_LAYERS));
CString str;
str.LoadString (IDS_TEXT_OF_YOUR BUTTON);
m_wndToolBar.ReplaceButton (ID_LAYERS1,
CMFCToolBarMenuButton ( (UINT)-1, menu,
GetCmdMgr ()->GetCmdImage (ID_LAYERS1), str,TRUE));
Related
I'm using the winsafe crate and want to know when a menu item's been clicked in a window.
flags & MF_MOUSESELECT as u16 should be 1 if so and 0 if not, but it's always 32768 everytime the event is fired, even if it's just from the user hovering a menu item, or even clicking away to make it close.
Why?
self.wnd.on().wm(winsafe::co::WM::MENUSELECT, {
move |params| {
let wparam = params.wparam;
let lparam = params.lparam;
let flags = (wparam >> 16 & 0xffff) as u16;
let MF_MOUSESELECT = 0x00008000 as u32;
println!("{}", flags & MF_MOUSESELECT as u16);
// always 32768
0
}
});
The menu is generated by a resource script which is compiled and embedded in the program:
1 MENU
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
{
POPUP "&File"
{
MENUITEM "&Open", 1
MENUITEM "&Save", 2
}
POPUP "&Help"
{
MENUITEM "&About", 3
}
}
You should handle WM_COMMAND. There is actually built in functionality for menus. Simply provide the id you specified in your resource file to check when the correponding menu item is clicked.
Replace your event listener with these:
self.wnd.on().wm_command(co::CMD::Menu, 1, {
move || {
println!("Open clicked.")
}
});
self.wnd.on().wm_command(co::CMD::Menu, 2, {
move || {
println!("Save clicked.")
}
});
self.wnd.on().wm_command(co::CMD::Menu, 3, {
move || {
println!("About clicked.")
}
});
I'm attempting to populate a "submenu" in my dialog box from an array of strings as shown in this answer.
My attempt looks like the following:
#define ID_APP0 14000
#define ID_APP1 14001
#define ID_APP2 14002
#define ID_APP3 14003
#define ID_APP4 14004
#define ID_APP5 14005
#define ID_APP6 14006
#define ID_APP7 14007
void SoftwareDlg::DynamicAppMenu()
{
CMenu MyMainMenu;
VERIFY(MyMainMenu.LoadMenu(IDR_MENU1));
CMenu* SomeMenu = MyMainMenu.GetSubMenu(0);
if (SomeMenu)
{
for (auto i = 0; i < 1; i++)
{
SomeMenu->AppendMenu(MF_STRING, 14000+i, Client::m_vszAppArr[i]);
}
}
}
...but I'm getting an exception from the assert below immediately after(during?) the AppendMenu() function.
_AFXWIN_INLINE BOOL CMenu::AppendMenu(UINT nFlags, UINT_PTR nIDNewItem, LPCTSTR lpszNewItem)
{ ASSERT(::IsMenu(m_hMenu)); return ::AppendMenu(m_hMenu, nFlags, nIDNewItem, lpszNewItem); }
I no longer know how to continue debugging this issue because the LoadMenu() function appears to work correctly, and none of the variables are populated where the exception takes place.
Am I possibly just calling this in the wrong place? It's happening(conditionally) inside of a scheduled member function of the dialog box... does it need to happen somewhere like OnDraw(), OnPaint(), or something?
Edit1: Value of SettingsMenu->m_hMenu
Edit2: IDR_MENU1 resource definition:
IDR_MENU1 MENU
BEGIN
POPUP "Tools"
BEGIN
MENUITEM "New Test F11", ID_TOOLS_NEWTEST
MENUITEM "Export Data", ID_TOOLS_EXPORTDATA
MENUITEM "Upload Data", ID_TOOLS_UPLOADDATA
MENUITEM "Test Control", ID_TOOLS_TESTCONTROL
END
POPUP "Settings"
BEGIN
POPUP "USB_PORT"
BEGIN
MENUITEM "Serial Ports", ID_PORT_SERIALPORTS, INACTIVE
MENUITEM "COM1", ID_PORT_COM1
MENUITEM "COM2", ID_PORT_COM2
MENUITEM "COM3", ID_PORT_COM3
MENUITEM "COM4", ID_PORT_COM4
MENUITEM "COM5", ID_PORT_COM5
MENUITEM "COM6", ID_PORT_COM6
MENUITEM "COM7", ID_PORT_COM7
MENUITEM "COM8", ID_PORT_COM8
END
MENUITEM "Debug Mode", ID_SETTINGS_DEBUGMODE
MENUITEM "Display in OSD", ID_SETTINGS_DISPLAYINOSD
MENUITEM "Test Upload Mode", ID_SETTINGS_TESTUPLOADMODE
MENUITEM "Preferences", ID_SETTINGS_PREFERENCES
POPUP "Target Window"
BEGIN
MENUITEM "Placeholder", ID_TARGETWINDOW_PLACEHOLDER
END
END
END
As hinted to by #Vlad Feinstein's comment, CMenu MyMainMenu; should not have been declared inside the function... That fix allowed me to get past the assertion, but the new menu items were still not appearing. I began catching and logging the return values from the creation and deletion functions which lead me to realize that because I already had an existing menu, I needed to use a pointer to that menu(as opposed to creating my own that I then left un-used).
My final function ended up looking like this:
void SoftwareDlg::DynamicAppMenu(){
CMenu* MainMenu = GetMenu();
CMenu* SettingsMenu = MainMenu->GetSubMenu(1);
CMenu* TargetAppMenu = SettingsMenu->GetSubMenu(5);
if (TargetAppMenu)
{
BOOL appended = false;
BOOL deleted = false;
for (auto i = 0; i < Client::m_vszAppArr.size(); i++)
{
appended = TargetAppMenu->AppendMenu(MF_STRING, 14000+i, Client::m_vszAppArr[i].c_str());
}
deleted = TargetAppMenu->DeleteMenu(ID_TARGETWINDOW_PLACEHOLDER, MF_BYCOMMAND);
OutputDebugString(("String appended: " + std::to_string(appended)).c_str());
OutputDebugString(("Placeholder deleted: " + std::to_string(deleted)).c_str());
}
}
I'm checking if a button is checkbox of 32bit process on 64bit windows10.
The problem is that I can not distingush checkbox from normal button.
The buttons are different in Window-Detective:
(After I restart the application, even Window-Detective shows it is a button now!)
But the checkbox can't be recognized as checkbox in Spy++
BS_CHECKBOX is not listed.
Code (compiled as 32bit):
TEST_METHOD(ShouldCheckStyle) {
auto styleOfButton = ::GetWindowLongPtr((HWND)0x003F06E8, GWL_STYLE);
auto styleOfCheckbox = ::GetWindowLongPtr((HWND)0x01101642, GWL_STYLE);
auto bsOfButton = styleOfButton & BS_TYPEMASK;
auto bsOfCheckbox = styleOfCheckbox & BS_TYPEMASK;
auto resultOfButton = (bsOfButton == BS_CHECKBOX);
auto resultOfCheckbox = (bsOfCheckbox == BS_CHECKBOX);
auto debugger = 0;
}
Debug output
The code indicates they both have BS_OWNERDRAW. The above behaves the same for the button and the checkbox.
The weird thing is Window-Detective can recognize the style of checkbox. The code is same as I used above. Here's a piece of code:
Window* WindowManager::createWindow(HWND handle) {
WindowClass* windowClass = getWindowClassFor(handle);
String className = windowClass->getName().toLower();
if (className == "button") {
LONG typeStyle = GetWindowLong(handle, GWL_STYLE) & BS_TYPEMASK;
switch (typeStyle) {
case BS_CHECKBOX:
case BS_AUTOCHECKBOX:
case BS_3STATE:
case BS_AUTO3STATE: {
return new CheckBox(handle, windowClass);
}
case BS_RADIOBUTTON:
case BS_AUTORADIOBUTTON: {
return new RadioButton(handle, windowClass);
}
case BS_GROUPBOX: {
return new GroupBox(handle, windowClass);
}
default: {
// If none of the above is true, then the control is just a Button
return new Button(handle, windowClass);
}
}
}
After some discussion, you can use GetWindowText to get the text from each control and compare the specific text.
BS_CHECKBOX cannot be detected from the properties of the "checkbox" control beacuse of BS_OWNERDRAW.
Creates an owner-drawn button. The owner window receives a WM_DRAWITEM
message when a visual aspect of the button has changed. Do not combine
the BS_OWNERDRAW style with any other button styles.
Try the below code:
WCHAR str1[20];
WCHAR str2[] = L"Agree me";
GetWindowText(hwnd_checkbox, str1, 256);
if (_tcscmp(str1, str2) == 0)
{
//it is checkbox
}
else
{
//it isn't checkbox
}
After you get the correct control handle of the checkbox, you can use SendDlgItemMessage or SendMessage to send BM_SETCHECK check message.
SendMessage(hwnd_checkbox, BM_SETCHECK, BST_CHECKED, 0);
When you click on the "dropdown" button of a combobox, the dropped down listbox appears below the combobox, unless there is not enough space below, in which case the listbox appears above.
Now I wonder if there is a possibility to force the lisbox to appear above the combobox, even if there is enough space below.
Illustration
When I click on the combo box, I'd like the "drop down" list box appear always above as on the left screen copy.
Everything is possible, and you don't need to implement the control "from scratch".
First, you can subclass the ListBox part of your ComboBox to get complete control over it, as explained in MSDN. You can create a class, derived from CListBox, using the Class Wizard. You only need to implement WM_WINPOSITIONCHANGING handler in it:
void CTopListBox::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
CListBox::OnWindowPosChanging(lpwndpos);
if ((lpwndpos->flags & SWP_NOMOVE) == 0)
{
lpwndpos->y -= lpwndpos->cy + 30;
}
}
Here, for simplicity, I am moving the box up by the (heights+30). You can get the height of your ComboBox instead of my 30.
Then you declare a member variable in your dialog class:
CTopListBox m_listbox;
and subclass it like that:
HBRUSH CMFCDlgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (nCtlColor == CTLCOLOR_LISTBOX)
{
if (m_listbox.GetSafeHwnd() == NULL)
{
m_listbox.SubclassWindow(pWnd->GetSafeHwnd());
CRect r;
m_listbox.GetWindowRect(r);
m_listbox.MoveWindow(r);
}
}
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
Note that I am calling m_listbox.MoveWindow(r) there; it is needed because first WM_CONTROLCOLOR message for that list box comes after it is positioned, so the very first time it would drop down instead of up.
Disclaimer: this is not a very clean solution, as, if you have windows animation enabled, you'd see that the list unrolls from top to bottom.
Alternatively, you should be able to "fool" the combobox that it is too close to the bottom of the screen; then it will drop up by itself. I leave it as an exercise for the readers :)
This would be relatively easy except when combo box has "slide open" effect. If you move the dropdown listbox to the top, and the combo slides open from top-to-bottom, it would look odd. So you have to disable the animation or reverse it.
In this function I call AnimateWindow in OnWindowPosChanging, it doesn't seem to cause any problems but I am not a 100% sure about it!
class CComboBox_ListBox : public CListBox
{
public:
CWnd *comboBox;
void OnWindowPosChanging(WINDOWPOS *wndpos)
{
CListBox::OnWindowPosChanging(wndpos);
if (comboBox && wndpos->cx && wndpos->cy && !(wndpos->flags & SWP_NOMOVE))
{
CRect rc;
comboBox->GetWindowRect(&rc);
//if listbox is at the bottom...
if (wndpos->y > rc.top) {
//if there is enough room for listbox to go on top...
if (rc.top > wndpos->cy) {
wndpos->y = rc.top - wndpos->cy;
BOOL animation;
SystemParametersInfo(SPI_GETCOMBOBOXANIMATION, 0, &animation, 0);
//if combobox slides open...
if (animation) {
//we have to set the x coordinate otherwise listbox
//is in the wrong place when parent window moves
SetWindowPos(0, wndpos->x, wndpos->y, 0, 0,
SWP_NOSENDCHANGING | SWP_HIDEWINDOW | SWP_NOSIZE);
AnimateWindow(100, AW_VER_NEGATIVE);
}
}
}
}
}
DECLARE_MESSAGE_MAP()
};
Usage:
COMBOBOXINFO ci = { sizeof(COMBOBOXINFO) };
comboBox.GetComboBoxInfo(&ci);
CComboBox_ListBox *listBox = new CComboBox_ListBox;
listBox->comboBox = &comboBox;
listBox->SubclassWindow(ci.hwndList);
Also you can use SetMinVisibleItems to reduce the listbox height and make sure the dropdown list fits on top.
I have a need to add a custom action (say ‘About’ clicking which a QMessageBox needs to be displayed) in the system menu shown when the icon on the title bar of a QDialog is clicked. How do I achieve this?
Regards,
Bharath
You cannot do it with Qt because it's OS specific. But you can use GetSystemMenu and AppendMenu functions in Windows to modify the menu and then catch events that then item is clicked.
Here is a simple example from here. It appends a separator and an about item to the menu:
#include "windows.h"
// IDM_ABOUTBOX must be in the system command range
// (IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX)
// and (IDM_ABOUTBOX < 0xF000)
#define IDM_ABOUTBOX 0x0010
MyWidget::MyWidget() : QMainWindow()
{
...
HMENU hMenu = ::GetSystemMenu(winId(), FALSE);
if (hMenu != NULL)
{
::AppendMenuA(hMenu, MF_SEPARATOR, 0, 0);
::AppendMenuA(hMenu, MF_STRING, IDM_ABOUTBOX, "About MyApp...");
}
...
}
bool MyWidget::winEvent(MSG *m, long *result)
{
if (m->message == WM_SYSCOMMAND)
{
if ((m->wParam & 0xfff0) == IDM_ABOUTBOX)
{
*result = 0;
// open About dialog
about();
return (true);
}
}
return (false);
}
PRO-file:
LIBS += -lUser32