dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: win32- hook: why i can't change anything?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,274

    win32- hook: how changing the aligment text and position of messagebox?

    i have these functions for show a messagebox:
    Code:
    int GetWindowString(HWND hwnd, std::string &s)
    {
        char buffer[65536];
    
        int txtlen=GetWindowTextLength(hwnd) + 1;
        GetWindowText(hwnd, buffer, txtlen);
    
        s = buffer;
        return txtlen;
    }
    
    HHOOK hhookCBTProc = 0;
    
    LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
    {
      if (nCode == HCBT_CREATEWND)
      {
        CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
    
        if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
        {
          HWND hwnd = (HWND)wParam;
    
          // At this point you have the hwnd of the newly created
          // message box that so you can position it at will
          HDC test=GetDC(hwnd);
          //these 2 messages aren't showed
          if(SetTextAlign(test,TA_CENTER)==GDI_ERROR)
            MessageBox(NULL,"aligment", "error", MB_OK);
          if(SetWindowPos(hwnd, 0, 10,10,0,0,SWP_NOSIZE | SWP_NOZORDER)==FALSE)
            MessageBox(NULL,"position", "error", MB_OK);
          ReleaseDC(hwnd,test);
        }
      }
    
      return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
    }
    
    void MessageBox(string text, string title="",UINT flags=MB_OK, bool modal=false)
    {
        HWND parent=NULL;
        if(modal==true)
            parent=ActivatedForm;
        if(title=="")
        {
            GetWindowString(ActivatedForm,title);
        }
        if(hhookCBTProc==0)
            hhookCBTProc = SetWindowsHookEx(WH_CBT,
                                    pfnCBTMsgBoxHook,
                                    0, GetCurrentThreadId());
        if(hhookCBTProc==NULL)
            ::MessageBox(parent,"error","error", MB_OK);
        ::MessageBox(parent,text.c_str(),title.c_str(), flags);
        if(hhookCBTProc!=0)
            UnhookWindowsHookEx(hhookCBTProc);
    
    }
    i have an error on pfnCBTMsgBoxHook() function. how i know? the aligment and position aren't changed. why?
    and no error messageboxes are showed
    Last edited by Cambalinho; July 31st, 2015 at 10:25 AM.

  2. #2
    Join Date
    Apr 2009
    Posts
    1,274

    Re: win32- hook: why i can't change anything?

    changing the pcs->x and pcs->y values, the messagebox is changed in real time:
    Code:
    LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
    {
      if (nCode == HCBT_CREATEWND)
      {
        CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
    
        if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
        {
          HWND hwnd = (HWND)wParam;
    
          // At this point you have the hwnd of the newly created
          // message box that so you can position it at will
          //SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
          RECT parentrct;
          
          GetWindowRect(ActivatedForm,&parentrct);
          LONG x=parentrct.left+(parentrct.right-parentrct.left)/2 - pcs->cx/2;
          LONG y=parentrct.top+(parentrct.bottom-parentrct.top)/2 - pcs->cy/2;
          pcs->x=x;
          pcs->y=y;
        }
      }
    
      return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
    }
    anotherthing: we can't call UnhookWindowsHookEx() after show the messagebox... only when it's not needed.

    the SendMessage() have 1 message for change the text aligment or is just for HDC?

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: win32- hook: how changing the aligment text and position of messagebox?

    1. Your test is strange: you are trying to create a MessageBox form the hook set on creation of message boxes; looks like infinite recursion.
    2. Because of #1 and multiple other reasons, NEVER use MessageBox() for debugging/tracing. Instead, call ::OutputDebugString().
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Apr 2009
    Posts
    1,274

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by VladimirF View Post
    1. Your test is strange: you are trying to create a MessageBox form the hook set on creation of message boxes; looks like infinite recursion.
    2. Because of #1 and multiple other reasons, NEVER use MessageBox() for debugging/tracing. Instead, call ::OutputDebugString().
    1 - humm.... confused me that
    2 - i'm using the codeBlocks. for now, i don't know see the OutputDebugString() messages.
    i'm reading: http://wiki.codeblocks.org/index.php...h_Code::Blocks
    but i only see the watches window nothing more

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by Cambalinho View Post
    2 - i'm using the codeBlocks. for now, i don't know see the OutputDebugString() messages.
    i'm reading: http://wiki.codeblocks.org/index.php...h_Code::Blocks
    but i only see the watches window nothing more
    Try DebugView at http://technet.microsoft.com/en-us/s.../bb896647.aspx
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Apr 2009
    Posts
    1,274

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by VladimirF View Post
    thanks for all
    going back to the code. the messagebox 1 STATIC child control, wright?
    so i can catch its HWND:
    Code:
    HWND hwnd = (HWND)wParam;
    
            // At this point you have the hwnd of the newly created
            // message box that so you can position it at will
            //SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
    
            HWND hwnd2 = FindWindowEx(hwnd, NULL, TEXT("STATIC"), NULL);
            if (hwnd2!=NULL)
                SetWindowLong(hwnd2, GWL_STYLE, GetWindowLong(hwnd2, GWL_STYLE) | SS_CENTER);
            else
                MessageBox(NULL, to_string(GetLastError()).c_str(), "error", MB_OK);
    but the hwnd2 recives NULL. what you can tell me?
    (the GetLastError() is 0)

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by Cambalinho View Post
    ...but the hwnd2 recives NULL. what you can tell me?
    I can say that the STATIC child control is not yet created. Are you calling that from your HCBT_CREATEWND? If yes - it is too early. Try HCBT_ACTIVATE.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Apr 2009
    Posts
    1,274

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by VladimirF View Post
    I can say that the STATIC child control is not yet created. Are you calling that from your HCBT_CREATEWND? If yes - it is too early. Try HCBT_ACTIVATE.
    yes you have wright. thanks:
    Code:
    HHOOK hhookCBTProc = 0;
    
    LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if(nCode==HCBT_ACTIVATE)//center the text on STATIC child control
        {
            HWND hwnd = (HWND)wParam;
            HWND hwnd2 = FindWindowEx(hwnd, NULL,"STATIC",NULL);
            if (hwnd2!=NULL)
            {
    
                SetWindowLong(hwnd2, GWL_STYLE, GetWindowLong(hwnd2, GWL_STYLE) | SS_CENTER);
                //MessageBox(NULL, to_string(GetLastError()).c_str(), "error", MB_OK);
            }
        }
        else if (nCode == HCBT_CREATEWND)//center the messagebox on it's parent
        {
            CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
    
            if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
            {
                //HWND hwnd = (HWND)wParam;
    
                // At this point you have the hwnd of the newly created
                // message box that so you can position it at will
                //SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
                RECT parentrct;
                GetWindowRect(ActivatedForm,&parentrct);
                LONG x=parentrct.left+(parentrct.right-parentrct.left)/2 - pcs->cx/2;
                LONG y=parentrct.top+(parentrct.bottom-parentrct.top)/2 - pcs->cy/2;
                pcs->x=x;
                pcs->y=y;
            }
        }
    
      return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
    }
    
    void MessageBox(string text, string title="",UINT flags=MB_OK | MB_ICONERROR, bool modal=true)
    {
        HWND parent=NULL;
        if(modal==true)
            parent=ActivatedForm;
        if(title=="")
        {
            GetWindowString(ActivatedForm,title);
        }
        if(hhookCBTProc==0)
            hhookCBTProc = SetWindowsHookEx(WH_CBT,
                                    pfnCBTMsgBoxHook,
                                    0, GetCurrentThreadId());
        if(hhookCBTProc==NULL)
            OutputDebugString("error");
        ::MessageBox(parent,text.c_str(),title.c_str(), flags);
    }
    now i'm thinking on anotherthing, that i don't know how: can i do 2 new flags for the messagebox() and how can i test if that flag is used?

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by Cambalinho View Post
    can i do 2 new flags for the messagebox() and how can i test if that flag is used?
    What flags are you asking about? Could you please explain?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #10
    Join Date
    Apr 2009
    Posts
    1,274

    Re: win32- hook: how changing the aligment text and position of messagebox?

    Quote Originally Posted by VladimirF View Post
    What flags are you asking about? Could you please explain?
    the messagebox() have MB_RIGHT, MB_OK and others. imagine that i want do more 2 MB_CENTER and MB_PARENTCENTER. what values can i use?

  11. #11
    Join Date
    Apr 2009
    Posts
    1,274

    Re: win32- hook: why i can't change anything?

    i must create enum values\flags, but if i don't know, correctly, the messagebox flags values, i can't add them. so i inclued my own enum:
    Code:
    HHOOK hhookCBTProc = 0;
    
    //for flags we must do 2^X
    enum MessageBoxExtendedFlags
    {
        MB_EX_NONE=1,  //2^0 = 1
        MB_EX_MODAL=2, //2^1 = 2
        MB_EX_PARENTCENTER=4, //2^2 = 4
        MB_EX_TEXTCENTER=8 //2^3 = 8
    };
    
    UINT MGBEXTENDED=0;
    
    LRESULT CALLBACK MsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if(nCode==HCBT_ACTIVATE)//center the text on STATIC child control
        {
            HWND hwnd = (HWND)wParam;
            HWND hwnd2 = FindWindowEx(hwnd, NULL,"STATIC",NULL);
            if (hwnd2!=NULL && (MGBEXTENDED & MB_EX_TEXTCENTER))
            {
                SetWindowLong(hwnd2, GWL_STYLE, GetWindowLong(hwnd2, GWL_STYLE) | SS_CENTER);
                //MessageBox(NULL, to_string(GetLastError()).c_str(), "error", MB_OK);
            }
        }
        else if (nCode == HCBT_CREATEWND)//center the messagebox on it's parent
        {
            CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
    
            if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
            {
                HWND hwnd = (HWND)wParam;
    
                // At this point you have the hwnd of the newly created
                // message box that so you can position it at will
                //SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
                if(MGBEXTENDED & MB_EX_PARENTCENTER)
                {
                    RECT parentrct;
                    GetWindowRect(ActivatedForm,&parentrct);
                    LONG x=parentrct.left+(parentrct.right-parentrct.left)/2 - pcs->cx/2;
                    LONG y=parentrct.top+(parentrct.bottom-parentrct.top)/2 - pcs->cy/2;
                    pcs->x=x;
                    pcs->y=y;
                }
            }
        }
    
      return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
    }
    
    int MessageBox(string text, string title="",UINT flags=MB_OK, UINT ExtendedFlags=MB_EX_NONE)
    {
        HWND parent=NULL;
        MGBEXTENDED=ExtendedFlags;
        if(ExtendedFlags & MB_EX_MODAL)
            parent=GetForegroundWindow();
        if(title=="")
        {
            GetWindowString(ActivatedForm,title);
        }
        if(hhookCBTProc==0)
            hhookCBTProc = SetWindowsHookEx(WH_CBT,
                                    MsgBoxHook,
                                    0, GetCurrentThreadId());
        if(hhookCBTProc==NULL)
            OutputDebugString("error");
        return ::MessageBox(parent,text.c_str(),title.c_str(), flags);
    }
    maybe with time i can include the checkbox
    out-off-topic: i have 2 windows(not child), 1 above other. on back isn't enabled and on front is.
    heres the 2 situations:
    1 - i'm on 2nd window and i show 1 messagebox. then i close the messagebox. and then the window. why the window on back loses the focus and other window\program get the focus?
    2 - i close the 2nd window. the window on back get the focus... normal.
    can anyone explain to me these ZOrder problem?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)