Center of Parent


Code.MessageBox_CenterApplication(caption); 

System.Windows.Forms.MessageBox.Show(smessage,
                caption,
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Information);

Constants.gTHREAD_MESSAGEBOXCENTER.Abort();



        public static void MessageBox_CenterApplication(
            string caption)
        {
            try
            {
                Constants.Structure_RECT oRect;
                System.IntPtr ohandle;
                System.Windows.Forms.NativeWindow nativeWindow;

                oRect = new Constants.Structure_RECT();
                ohandle = Code.Method_HandleGet(Globals.ThisAddIn.Application);
                nativeWindow = new System.Windows.Forms.NativeWindow();
                nativeWindow.AssignHandle(ohandle);
                Constants.GetWindowRect(ohandle, ref oRect);
                nativeWindow.ReleaseHandle();

                int ExcelWindow_CenterX = oRect.Left + (oRect.Right - oRect.Left) / 2;
                int ExcelWindow_CenterY = oRect.Top + (oRect.Bottom - oRect.Top) / 2;

                Constants.gTHREAD_MESSAGEBOXCENTER = new System.Threading.Thread(() =>
                {
                    Constants.Structure_RECT oRect2;
                    System.IntPtr ohandle2;

                    oRect2 = new Constants.Structure_RECT();

                    while ((ohandle2 = Constants.FindWindow(System.IntPtr.Zero, caption)) == System.IntPtr.Zero) ;

                    Constants.GetWindowRect(ohandle2, ref oRect2);

                    int Msgbox_Left = ExcelWindow_CenterX - (int)(oRect2.Right - oRect2.Left) / 2;
                    int Msgbox_Top = ExcelWindow_CenterY - (int)(oRect2.Bottom - oRect2.Top) / 2;

                    Constants.MoveWindow(ohandle2, Msgbox_Left, Msgbox_Top,
                        oRect2.Right - oRect2.Left,
                        oRect2.Bottom - oRect2.Top, true);
                });
                Constants.gTHREAD_MESSAGEBOXCENTER.Start();

                //Must include this after the MessageBox.Show
                //Constants.gTHREAD_MESSAGEBOXCENTER.Abort();
            }
            catch (System.Exception ex)
            {
                ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
        }


© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext