본문 바로가기
다물칸 주소복사
조회 수 184 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
Extra Form
구분 팁&트릭
출처 내가작성

안녕하세요. 메시지 박스(XtraMessageBox)에 대해 심층적으로 파보겠습니다. 

오늘 기술된 내용의 특징은 다음과 같습니다. 

 

스크린 사이즈(=해상도)가 커질 수록 메시지와 타이틀, 버튼의 폰트가 작아지는 것을 방지

버튼의 Locale설정 또는 커스텀 가능

 

자 먼저 소스를 보시죠...

 

MsgBox 함수가 주요함수입니다. 전역 (싱글톤) 클래스로 구현된 상태라 메시지 박스 아규먼트외에 스크린 사이즈를 추가로 넣었습니다. 이것을 통해 폰트 크기를 특정합니다. 스크린 사이즈는 호출할 때 this.Width로 넣어서 호출하시면 됩니다. 

static int MsgBox_ScreenSize = 0;
        public static DialogResult MsgBox(string Message, string Title, 
        	MessageBoxButtons button, MessageBoxIcon icon, int ScreenSize = 1920)
        {
            MsgBox_ScreenSize = ScreenSize;
            XtraMessageBoxArgs args = new XtraMessageBoxArgs();
            args.Appearance.FontSizeDelta = MsgBox_ScreenSize > 1920 ? 14 : 11;
            if (button == MessageBoxButtons.OKCancel)
            {
                args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel };
            }
            else if (button == MessageBoxButtons.YesNo)
            {
                args.Buttons = new DialogResult[] { DialogResult.Yes, DialogResult.No };
            }
            else if (button == MessageBoxButtons.OK)
            {
                args.Buttons = new DialogResult[] { DialogResult.OK };
            }

            if (icon == MessageBoxIcon.Question)
            {
                args.Icon = StockIconHelper.GetWindows8AssociatedIcon(SystemIcons.Question);
            }
            else if (icon == MessageBoxIcon.Error)
            {
                args.Icon = StockIconHelper.GetWindows8AssociatedIcon(SystemIcons.Error);
            }
            else if (icon == MessageBoxIcon.Information)
            {
                args.Icon = StockIconHelper.GetWindows8AssociatedIcon(SystemIcons.Information);
            }
            else if (icon == MessageBoxIcon.Exclamation)
            {
                args.Icon = StockIconHelper.GetWindows8AssociatedIcon(SystemIcons.Exclamation);
            }
            else if (icon == MessageBoxIcon.Warning)
            {
                args.Icon = StockIconHelper.GetWindows8AssociatedIcon(SystemIcons.Warning);
            }

            
            args.Showing += Args_Showing;
            

            if (MsgBox_ScreenSize > 1920)
            {
                XtraMessageBox.AllowHtmlText = true;
                args.Caption = string.Format("<size=16>{0}</size>", Title);
                args.AllowHtmlText = DevExpress.Utils.DefaultBoolean.True;
                args.Text = string.Format("<size=16>{0}</size>", Message);
                
                return XtraMessageBox.Show(args);
            }
            else
            {
                XtraMessageBox.AllowHtmlText = false;
                return XtraMessageBox.Show(Message, Title, button, icon);
            }
        }

위 함수 내에 추가된 이벤트 소스입니다. 

위에서는 메시지와 제목을 수정했다면 이번에는 버튼 스타일을 이벤트에서 변경하는 방식으로 구현됩니다. 

 

        private static void Args_Showing(object sender, XtraMessageShowingArgs e)
        {
            e.Form.Appearance.FontStyleDelta = System.Drawing.FontStyle.Bold; 
            MessageButtonCollection buttons = e.Buttons as MessageButtonCollection;
            int fontSize = 9;

            SimpleButton okButton = buttons[DialogResult.OK] as SimpleButton;
            if (okButton != null)
            {
                fontSize = MsgBox_ScreenSize > 1920 ? 16 : 11;
                okButton.Text = Locale.__("Ok");
                okButton.Appearance.Font = new Font("D2 Coding", fontSize, FontStyle.Regular);
                okButton.Height += 10;
            }

            SimpleButton cancelButton = buttons[DialogResult.Cancel] as SimpleButton;
            if (cancelButton != null)
            {
                fontSize = MsgBox_ScreenSize > 1920 ? 16 : 11;
                cancelButton.Text = Locale.__("Cancel");
                cancelButton.Appearance.Font = new Font("D2 Coding", fontSize, FontStyle.Regular);
                cancelButton.Height += 10;
            }

            SimpleButton yesButton = buttons[DialogResult.Yes] as SimpleButton;
            if (yesButton != null)
            {
                fontSize = MsgBox_ScreenSize > 1920 ? 16 : 11;
                yesButton.Text = Locale.__("Yes");
                yesButton.Appearance.Font = new Font("D2 Coding", fontSize, FontStyle.Regular);
                yesButton.Height += 10;
            }

            SimpleButton noButton = buttons[DialogResult.No] as SimpleButton;
            if (noButton != null)
            {
                fontSize = MsgBox_ScreenSize > 1920 ? 16 : 11;
                noButton.Text = Locale.__("No");
                noButton.Appearance.Font = new Font("D2 Coding", fontSize, FontStyle.Regular);
                noButton.Height += 10;
            }
        }

이벤트로 스크린사이즈를 넘길 수가 없어서 지역변수로 추가해서 버튼의 폰트크기를 조정합니다.

버튼의 텍스트는 직접 하드코딩해서 넣으셔도 되고 저처럼 다국어 처리를 해서 넣으면 되겠죠?

 

(참고로 저의 다국어처리는 리소스 방식이 아닌 i18N처럼 외부파일 가져와서 사용하는 방식으로 구현합니다.)

D2 Coding폰트는 다른 폰트를 사용해도 되고 폰트의 크기만 설정해서 사용하셔도 될 것 같네요.

 

폰트크기도 제가 보기엔 저정도 크기면 될 것 같은데, 테스트 해보시면서 조정해서 사용하시면 됩니다. 

 

그럼 이만...