Msgbox centered on the window

User avatar
Marco Turco
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London
Contact:

Msgbox centered on the window

Post by Marco Turco »

Hi all,
I need to center the msgbox (example: Msginfo, MsgNoYes etc.) automatically on my windows instead of the screen.
I tried SetCenterOnParent(.t.) but this runs only for dialogs.

Any solution ? Thanks in advance
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
José Luis Sánchez
Posts: 484
Joined: Thu Oct 13, 2005 9:23 am
Location: Novelda - Alicante - España
Contact:

Re: Msgbox centered on the window

Post by José Luis Sánchez »

Use this code:

Code: Select all

   ACTIVATE DIALOG oDlgInfo ;
      ON INIT oDlgInfo:Center( oApp():oWndMain )
where oDlgInfo is your dialog and oApp():oWndMain the main window of the application.

Regards,
User avatar
Marco Turco
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London
Contact:

Re: Msgbox centered on the window

Post by Marco Turco »

Hi José,
your solution runs only with dialogs not with the msgbox functions.
The only running solution I have found is to make new function dialogs based for the msgbox functions.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
José Luis Sánchez
Posts: 484
Joined: Thu Oct 13, 2005 9:23 am
Location: Novelda - Alicante - España
Contact:

Re: Msgbox centered on the window

Post by José Luis Sánchez »

Marco,
I've the msgbox functions redefined based on dialogs, and for this runs ok in my programs.
Regards,
User avatar
Massimo Linossi
Posts: 474
Joined: Mon Oct 17, 2005 10:38 am
Location: Italy

Re: Msgbox centered on the window

Post by Massimo Linossi »

Hello Josè.
I have the same problem of Marco. Is possible to see your functions ?
Thanks a lot.
Massimo
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Msgbox centered on the window

Post by Antonio Linares »

Here there is some code that we could try to adapt to FWH:

http://vbnet.mvps.org/index.html?code/h ... centre.htm
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Massimo Linossi
Posts: 474
Joined: Mon Oct 17, 2005 10:38 am
Location: Italy

Re: Msgbox centered on the window

Post by Massimo Linossi »

It would be great.
Thanks Antonio.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Msgbox centered on the window

Post by Antonio Linares »

This is already working

Code: Select all

#pragma BEGINDUMP

#include <windows.h>
#include <hbapi.h>

LRESULT CALLBACK MsgBoxHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   if( nCode == HCBT_ACTIVATE )
   {
      HWND hWndMsgBox = wParam;
      
      SetWindowText( hWndMsgBox, "It works" );      
   }        
    
   return 0; // allow normal processing
}

HINSTANCE GetInstance( void );

HB_FUNC( CENTERMSGS )
{
   hb_retptr( ( void * ) SetWindowsHookEx( WH_CBT,  
                                MsgBoxHookProc, GetInstance(), 
                                GetCurrentThreadId() ) );
}

#pragma ENDDUMP
 
Now we need to find a way to locate the active window. I guess that the idea is to set this for the entire app.

Here you have an example:

Code: Select all

#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()

   local oWnd

   DEFINE WINDOW oWnd
   
   CenterMsgs()
    
   ACTIVATE WINDOW oWnd ;
      ON CLICK  MsgInfo( "Hello world" )
   
return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Msgbox centered on the window

Post by Antonio Linares »

In this version we are already able to move the Msg... window:

Code: Select all

static HHOOK hHook = NULL;

LRESULT CALLBACK MsgBoxHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   if( nCode < 0 )  // do not process message 
        return CallNextHookEx( hHook, nCode, wParam, lParam ); 

   if( nCode == HCBT_CREATEWND )
   {
      HWND hWndMsgBox = wParam; 
      char cn[ 200 ];
            
      GetClassName( hWndMsgBox, cn, 199 );      

      if( cn[ 0 ] == '#' &&
          cn[ 1 ] == '3' &&
          cn[ 2 ] == '2' &&
          cn[ 3 ] == '7' &&
          cn[ 4 ] == '7' &&
          cn[ 5 ] == '0' &&
          cn[ 6 ] == 0 )
      {     
         ( ( CBT_CREATEWND * ) lParam )->lpcs->x = 50;
         ( ( CBT_CREATEWND * ) lParam )->lpcs->y = 50;
      }             
   }        
   
   return 0; // allow normal processing
}

HINSTANCE GetInstance( void );

HB_FUNC( CENTERMSGS )
{
   hHook = SetWindowsHookEx( WH_CBT,  
                             MsgBoxHookProc, GetInstance(), 
                             GetCurrentThreadId() );
}
 
Still we need to get the handle of the parent window
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Msgbox centered on the window

Post by Antonio Linares »

Ok, this one seems to be working fine :-)

Still we need to implement a function to remove the hook.

Code: Select all

static HHOOK hHook = NULL;

static void CenterWindowOnParent( HWND hChildWnd, LPARAM lParam )
{
   HWND hParent = GetParent( hChildWnd );
   RECT rcChild, rcParent;
   int cxChild, cyChild, cxParent, cyParent;

   GetWindowRect( hChildWnd, &rcChild );
   GetWindowRect( hParent, &rcParent );

   cxChild = rcChild.right - rcChild.left;
   cyChild = rcChild.bottom - rcChild.top;
   cxParent = rcParent.right - rcParent.left;
   cyParent = rcParent.bottom - rcParent.top;
   
   SetWindowPos( hChildWnd, NULL, 
               rcParent.left + ( cxParent - cxChild ) / 2, 
                 rcParent.top + ( cyParent - cyChild ) / 2, 0, 0, 0x15 );       
}

LRESULT CALLBACK MsgBoxHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   if( nCode < 0 )  // do not process message 
        return CallNextHookEx( hHook, nCode, wParam, lParam ); 

   if( nCode == HCBT_ACTIVATE )
   {
      HWND hWndMsgBox = ( HWND ) wParam;    
      char cn[ 200 ];
            
      GetClassName( hWndMsgBox, cn, 199 );      

      if( cn[ 0 ] == '#' &&
          cn[ 1 ] == '3' &&
          cn[ 2 ] == '2' &&
          cn[ 3 ] == '7' &&
          cn[ 4 ] == '7' &&
          cn[ 5 ] == '0' &&
          cn[ 6 ] == 0 )
         CenterWindowOnParent( hWndMsgBox, lParam );            
   }        
    
   return 0; // allow normal processing
}

HINSTANCE GetInstance( void );

HB_FUNC( CENTERMSGS )
{
   hHook = SetWindowsHookEx( WH_CBT,  
                             MsgBoxHookProc, GetInstance(), 
                             GetCurrentThreadId() );
}
 
Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
dutch
Posts: 1395
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand

Re: Msgbox centered on the window

Post by dutch »

Dear Antonio,

The theme (Dialog, MsgBox) is so nice. Is this Win8 theme or standard screen? If so, can we make it the screen on other Windows version such as Win7 or WinXP.
Antonio Linares wrote:Ok, this one seems to be working fine :-)

Still we need to implement a function to remove the hook.

Code: Select all

static HHOOK hHook = NULL;

static void CenterWindowOnParent( HWND hChildWnd, LPARAM lParam )
{
   HWND hParent = GetParent( hChildWnd );
   RECT rcChild, rcParent;
   int cxChild, cyChild, cxParent, cyParent;

   GetWindowRect( hChildWnd, &rcChild );
   GetWindowRect( hParent, &rcParent );

   cxChild = rcChild.right - rcChild.left;
   cyChild = rcChild.bottom - rcChild.top;
   cxParent = rcParent.right - rcParent.left;
   cyParent = rcParent.bottom - rcParent.top;
   
   SetWindowPos( hChildWnd, NULL, 
               rcParent.left + ( cxParent - cxChild ) / 2, 
                 rcParent.top + ( cyParent - cyChild ) / 2, 0, 0, 0x15 );       
}

LRESULT CALLBACK MsgBoxHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   if( nCode < 0 )  // do not process message 
        return CallNextHookEx( hHook, nCode, wParam, lParam ); 

   if( nCode == HCBT_ACTIVATE )
   {
      HWND hWndMsgBox = ( HWND ) wParam;    
      char cn[ 200 ];
            
      GetClassName( hWndMsgBox, cn, 199 );      

      if( cn[ 0 ] == '#' &&
          cn[ 1 ] == '3' &&
          cn[ 2 ] == '2' &&
          cn[ 3 ] == '7' &&
          cn[ 4 ] == '7' &&
          cn[ 5 ] == '0' &&
          cn[ 6 ] == 0 )
         CenterWindowOnParent( hWndMsgBox, lParam );            
   }        
    
   return 0; // allow normal processing
}

HINSTANCE GetInstance( void );

HB_FUNC( CENTERMSGS )
{
   hHook = SetWindowsHookEx( WH_CBT,  
                             MsgBoxHookProc, GetInstance(), 
                             GetCurrentThreadId() );
}
 
Image
Regards,
Dutch

FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Msgbox centered on the window

Post by ukoenig »

I needed a solution, to display different messages
on any defined position and with any style ( color,brush, gradient, font, icon .... )

the sample shows a message displayed on left upper corner on button-action

Image

MYMSGBOX( .T., { 100, 50, 100, 300 }, ; // .F. = center
"< " + ALLTRIM(STR( nMenge1 )) + " > Patient(en) mit" + CRLF + ;
"< " + ALLTRIM(STR( nMenge2 )) + " > Verordnungen geprüft " + CRLF + ;
" " + CRLF + ;
"< " + ALLTRIM(STR( nMenge3 )) + " > Fehler gefunden", ;
{"&Weiter"}, "VERORDNUNGS-PRÜFUNG", "A", 1, ;
{{ nDColorB, nDColorB, nDColorB, nDColorB }}, , , , ;
oFontSys, aVal[27], c_Pfad1 + "Info1.bmp" )


YES / NO centered

Image

DO WHILE nOpt = 0
nOpt := MYMSGBOX( .F., { 10, 10, 100, 300 }, ;
"Wollen Sie den Monat : " + cNach + CRLF + ;
"neu aufbauen ?", ;
{"&Aufbau","A&bbruch"}, ;
"MONATSÜBERNAHME", ;
"O", 1, ;
{{ nDColorB, nDColorB, nDColorB, nDColorB }}, , , , ;
oFontSys, aVal[27], c_Pfad1 + "Sort.bmp" )
ENDDO

IF nOpt = 1
....
....


best regards
Uwe :)
Last edited by ukoenig on Mon Jul 20, 2015 12:16 pm, edited 1 time in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Msgbox centered on the window

Post by Antonio Linares »

Dutch,

I am using Windows 10 :-)
dutch wrote:Dear Antonio,

The theme (Dialog, MsgBox) is so nice. Is this Win8 theme or standard screen? If so, can we make it the screen on other Windows version such as Win7 or WinXP.
Antonio Linares wrote:Ok, this one seems to be working fine :-)

Still we need to implement a function to remove the hook.

Code: Select all

static HHOOK hHook = NULL;

static void CenterWindowOnParent( HWND hChildWnd, LPARAM lParam )
{
   HWND hParent = GetParent( hChildWnd );
   RECT rcChild, rcParent;
   int cxChild, cyChild, cxParent, cyParent;

   GetWindowRect( hChildWnd, &rcChild );
   GetWindowRect( hParent, &rcParent );

   cxChild = rcChild.right - rcChild.left;
   cyChild = rcChild.bottom - rcChild.top;
   cxParent = rcParent.right - rcParent.left;
   cyParent = rcParent.bottom - rcParent.top;
   
   SetWindowPos( hChildWnd, NULL, 
               rcParent.left + ( cxParent - cxChild ) / 2, 
                 rcParent.top + ( cyParent - cyChild ) / 2, 0, 0, 0x15 );       
}

LRESULT CALLBACK MsgBoxHookProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   if( nCode < 0 )  // do not process message 
        return CallNextHookEx( hHook, nCode, wParam, lParam ); 

   if( nCode == HCBT_ACTIVATE )
   {
      HWND hWndMsgBox = ( HWND ) wParam;    
      char cn[ 200 ];
            
      GetClassName( hWndMsgBox, cn, 199 );      

      if( cn[ 0 ] == '#' &&
          cn[ 1 ] == '3' &&
          cn[ 2 ] == '2' &&
          cn[ 3 ] == '7' &&
          cn[ 4 ] == '7' &&
          cn[ 5 ] == '0' &&
          cn[ 6 ] == 0 )
         CenterWindowOnParent( hWndMsgBox, lParam );            
   }        
    
   return 0; // allow normal processing
}

HINSTANCE GetInstance( void );

HB_FUNC( CENTERMSGS )
{
   hHook = SetWindowsHookEx( WH_CBT,  
                             MsgBoxHookProc, GetInstance(), 
                             GetCurrentThreadId() );
}
 
Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
Horizon
Posts: 997
Joined: Fri May 23, 2008 1:33 pm

Re: Msgbox centered on the window

Post by Horizon »

Hi,

Should we call CenterMsgs() function just in main window or should we call before every MsgInfo function?
Regards,

Hakan ONEMLI

Harbour & VS 2019 & FWH 20.12
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Msgbox centered on the window

Post by Antonio Linares »

Hakan,

Just once at the beginning of your app.

And we should remove the hook before the app quits
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply