Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my SDI project I have a vast menu and I create a button named Close, so that the user can close all items static edit images etc. that appear after a menu item was pressed . SO I have this in WM_COMMAND:

C++
case IDBCLOSE: // Close Sub menu Items
            {
                SendMessage((HWND)hShaD1, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD2, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD3, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD4, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD5, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD6, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD7, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD8, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD9, WM_CLOSE, 0, 0);
                SendMessage((HWND)hShaD10, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt1, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt2, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt3, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt4, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt5, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt6, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt7, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt8, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt9, WM_CLOSE, 0, 0);
                SendMessage((HWND)hMArt10, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav1, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav2, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav3, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav4, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav5, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav6, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav7, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav8, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav9, WM_CLOSE, 0, 0);
                SendMessage((HWND)hJav10, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM1, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM2, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM3, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM4, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM5, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM6, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM7, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM8, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM9, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPassM10, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow1, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow2, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow3, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow4, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow5, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow6, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow7, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow8, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow9, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBow10, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButElemSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButSShiftSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButSummDSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButColdSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButLightSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButFireSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButDefSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButOffSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButCombPSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButWarSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButMastSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButCombBSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButSummNSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButPoisSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButCursSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButArtsSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButDisSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButTrapSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButJavSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButPassSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hButBowSkill, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hSkillStatic, WM_CLOSE, 0, 0);
                SendMessageW((HWND)hSkillEdit, WM_CLOSE, 0, 0);
                SendMessage((HWND)hAmazonB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hAssassinB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hNecromancerB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBarbarianB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPaladinB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hSorceressB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hDruidB, WM_CLOSE, 0, 0);
                SendMessage((HWND)hAmazonS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hAssassinS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hNecromancerS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hBarbarianS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hPaladinS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hSorceressS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hDruidS, WM_CLOSE, 0, 0);
                SendMessage((HWND)hReadOnly, WM_CLOSE, 0, 0);
                SendMessage((HWND)hStatic, WM_CLOSE, 0, 0);
                SendMessage((HWND)hCBdesc, WM_CLOSE, 0, 0);
                SendMessage((HWND)hCSdesc, WM_CLOSE, 0, 0);
                SendMessage((HWND)hCloseAll, WM_CLOSE, 0, 0);
                SendDlgItemMessage(hMainWindow, IDC_STATUS, SB_SETTEXT, 1,
                                   (LPARAM)"ver. 1.00");
                PlaySound(MAKEINTRESOURCE(IDW_SUBMENU), NULL, SND_RESOURCE | SND_ASYNC);
                return DefWindowProcW(hWnd, msg, wp, lp);
            }


Well there is an option that the user wanna press directly another menu item and he ignores Close Button .. So for every submenu item I have to send a WM_CLOSE to close the current opened.
My question is how to avoid all these messages and How can I transform the
case IDBCLOSE in to a macro .. so that I only write a single line not this insanity ?

What I have tried:

I have no idea, but I need to get rid of this mess. is quite annoying to write a bunch of lines just to clear all the controls that have been crated.
Posted
Updated 8-Jul-20 8:51am
Comments
Richard MacCutchan 8-Jul-20 11:06am    
Are these all independent Windows, or child Windows of your main Window?
Greg Utas 8-Jul-20 12:54pm    
I know nothing of the framework you're using, but it looks like each of these things being cast as a HWND could be added to a vector when created, so that you could run through the vector and close them later.

If what KarstenK mention does not take care of it, I would have the submenus add themselves to a list when they open - or do it for them if they can't. When you want to close them all you just traverse the list and close each entry in the list. Apparently you only need to save a handle in the list. Since it's C++ this list could be a vector. It would be for me. I would guess you have to add them to the list yourself so make a function you can call that opens the submenu, does what ever else you need it to, and adds it the list.
 
Share this answer
 
Comments
M@gelearn 9-Jul-20 8:32am    
Well thank you Rick .. this was the right solution. I can tell ya that I saved like 10.000 lines in my project with the vector.
Thank you once again..! 5 :)
One way is to use arrays for that command. But better is it to structure the code better with classes and making the cleanup in in the message handler in the instance. For that you must check where all these HWND are from.

Read the DestroyWindow documentation. When the controls are child windows of the app they dont need to be explicitly closed.

What a mess :-/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900