subclassing window not subclassed yet

Help
2004-11-24
2013-04-15
  • Steven Weiss
    Steven Weiss
    2004-11-24

    hi john,
    i'm trying to remap / subclass a window that's not subclassed by win32gui or another library - it's the windows' menu window (class: #32768).

    so i tried the remap_wnd and the menu gets subclassed...but it seems to be immediately destroyed (the c++ object, the menu still exists). so i looked into remap_wnd and then create_wnd_from_hwnd and in the comment there's written:
    "Use this function ONLY if you want to subclass a window (most likely a dialog) who's already
        subclassed by another framework (like ATL or MFC) ..."

    how else can i subclass the window? or do i have to use a plain old wndproc by myself?

     
    • John Torjo
      John Torjo
      2004-11-25

      About the comment:
      it's for create_wnd_from_hwnd, not for remap_wnd :)

      About subclassing the menu window: I can only say that I find it strange. I have subclassed a menu window, and it worked fine.

      So, it's either be a bug of mine, or a bug of yours :D
      Could you send me the code - and I'll take a look at it next week. Or, if it's short, post it here. Thanks.

      Best,
      John

       
      • Steven Weiss
        Steven Weiss
        2004-11-25

        // thanx to Paul DiLascia!
        static BOOL CALLBACK find_menu_hwnd_enum_proc(HWND hWnd, LPARAM lParam)
        {
            char buf[16];
            GetClassName(hWnd, buf, sizeof(buf));
            // if menu class is found stop enumeration and save HWND
            if (strcmp(buf, "#32768") == 0) {
                 *((HWND*) lParam) = hWnd;
                 return FALSE;
            }
            return TRUE;
        }

        static HWND find_menu_hwnd()
        {
            HWND hMenuWnd = NULL;
            EnumWindows(&icon_menu_wnd_handler::find_menu_hwnd_enum_proc, (LPARAM) &hMenuWnd);
            return hMenuWnd;       
        }

        handle_event on_init_menu_popup()
        {
            if (HWND hMenuWnd = find_menu_hwnd())
                      mapping::remap_wnd<menu_wnd> (hMenuWnd);

            return event<WM_INITMENUPOPUP> ().HANDLED_BY(&me::on_init_menu_popup);
        }

        menu_wnd extends window_base, is empty and has a handler

         
        • John Torjo
          John Torjo
          2004-11-25

          Have not tested the code, but maybe the code should be:

          if (HWND hMenuWnd = find_menu_hwnd())
            if ( !try_cast<menu_wnd>( find_by_hwnd(hMenuWnd))
              mapping::remap_wnd<menu_wnd> (hMenuWnd);

          Best,
          John

           
          • Steven Weiss
            Steven Weiss
            2004-11-25

            i made a mistake which wasn't obvious...in on_full_create() i changed the width of the window-menu to test if the subclassing was successful. and indeed it is successfull but the menu gets destroyed immediately when you try to apply SetWindowPos() to it...windows doesn't like that!

            so i removed it and now it works even without the try_cast. thanks anyway!

            mfg steven

             
            • John Torjo
              John Torjo
              2004-11-25

              :)

              Note: you can do SetWindowPos by using
              window_base::set_window_pos - just in case you didn't know....

              Best,
              John

               
              • Steven Weiss
                Steven Weiss
                2004-11-25

                i knew it and used it ;-)