Respondida Question about the safety of customized MessageBox

  • terça-feira, 10 de abril de 2012 06:45
     
      Contém Código

    Hi!

    I have found a very smart solution through the Internet.

    Could I have a question about safety/stability, please?

    Below I have attached a code with a hook.

    It is a bit different from original, but it is not important.

    	LRESULT CALLBACK CBTProc(INT nCode, WPARAM wParam, LPARAM lParam)
    	{
    		lParam;
    		// notification that a window is about to be activated
    		// window handle is wParam
    		if( HCBT_ACTIVATE == nCode )
    		{
    			HWND hChildWnd;    // msgbox is "child"
    			// set window handles
    			hChildWnd = (HWND)wParam;
    			//to get the text of the Yes button
    			SetDlgItemText(hChildWnd,IDYES,GetYes().c_str());
    			SetDlgItemText(hChildWnd,IDOK,GetOK().c_str());
    			SetDlgItemText(hChildWnd,IDNO,GetNo().c_str());
    			SetDlgItemText(hChildWnd,IDCANCEL,GetCancel().c_str());
    			//Continue changing other button captions
    
    			// exit CBT hook
    			UnhookWindowsHookEx(hhk);
    		}
    		// otherwise, continue with any possible chained hooks
    		//else 
    		CallNextHookEx(hhk, nCode, wParam, lParam);
    		return 0;
    	}
    
    	int MonadMessageBox( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType )
    	{
    		hhk = SetWindowsHookEx( WH_CBT, &CBTProc, 0, GetCurrentThreadId() );
    		const int iRes = MessageBox( hWnd, lpText, lpCaption, uType | MB_SETFOREGROUND );
    
    		return iRes;
    	}
    

    Is it possible to use such code in the official release of application?

    The problem would be, that this method is altering the system dialog box.

    Thank you very much


    Włodzimierz O. Kubera

Todas as Respostas

  • terça-feira, 10 de abril de 2012 23:02
     
     Respondido

    For most uses MessageBoxes are fairly straightforward. I would write a custom MessageBox rather than trying to hack the built in one.

    What you're doing there will probably work, but depends on the MessageBox keeping a flat window hierarchy. This hierarchy is an undocumented implementation detail, so not something to depend on if you want to future-proof your application.

    It also depends on your new text fitting nicely in the existing button.

    --Rob

    • Marcado como Resposta JujiPL quarta-feira, 11 de abril de 2012 02:30
    •  
  • quarta-feira, 11 de abril de 2012 02:31
     
     

    I highly appreciate the answer.

    It would be enough.


    Włodzimierz O. Kubera