본문 바로가기

Windows/MFC

CMyMessageBox (skin)

// MyMessageBox.cpp : implementation file
//

// DATE        : June 27, 2006
// CODER    : aucd29 (ccd@apsat.co.kr)
// URL        : http://www.apsat.co.kr
//
// Copyright Asia pasific satellite industries Co., Ltd.
//
// ----------------------------------------------------------------------
// NOTE : August 31, 2006
// ----------------------------------------------------------------------
// * 메시지 박스 디자인을 변경해야 하기 때문에 +_+
// 하나 만들었다... -_- 근데 너무 허접해.. ㅋㅋㅋ
//

#include "stdafx.h"
#include "SMS_WIZARD.h"
#include "MyMessageBox.h"
#include "memdc.h"


// CMyMessageBox dialog

IMPLEMENT_DYNAMIC(CMyMessageBox, CDialogSkin)

CMyMessageBox::CMyMessageBox(CWnd* pParent /*=NULL*/)
    : CDialogSkin(CMyMessageBox::IDD, pParent)
    , m_szMessage(_T(""))
    , m_nType(0)
{

}

CMyMessageBox::CMyMessageBox(CString szMessage, CWnd* pParent)
    : CDialogSkin(CMyMessageBox::IDD, pParent)
    , m_szMessage(_T(""))
    , m_nType(0)
{
    m_szMessage = szMessage;
    DoModal();
}

CMyMessageBox::CMyMessageBox(CString szMessage, int nType, CWnd* pParent)
    : CDialogSkin(CMyMessageBox::IDD, pParent)
    , m_szMessage(_T(""))
    , m_nType(0)
{
    m_szMessage = szMessage;
    m_nType        = nType;
    //DoModal();
}


CMyMessageBox::~CMyMessageBox()
{
}

void CMyMessageBox::DoDataExchange(CDataExchange* pDX)
{
    CDialogSkin::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CMyMessageBox, CDialogSkin)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CMyMessageBox message handlers

BOOL CMyMessageBox::OnInitDialog()
{
    CDialogSkin::OnInitDialog();

    CButton* pBtnCancel = (CButton*)GetDlgItem(IDCANCEL);
    CStatic* pBitmap    = (CStatic*)GetDlgItem(IDC_IMG);

    if (!m_nType)
    {
        pBtnCancel->ShowWindow(false);
        
        CBitmap bmpQuestion;
        bmpQuestion.LoadBitmap(IDB_QUESTION);
        pBitmap->SetBitmap(bmpQuestion);
    }

    // 경고음 보내주시고.
    MessageBeep(MB_ICONEXCLAMATION);

    int nWidthMax = 0, nWidthCnt = 0;
    int nHeightCnt = 0;
    int nLen = m_szMessage.GetLength();
    CString szDataChar;
    int i;
    CRect rect, rtBase;

    GetWindowRect(&rect);
    GetClientRect(&rtBase);

    int nCntOnePixelWord = 0, nCntOnePixelWordMax = 0;
    CString szWord;
    int nOld = 0;

    // 단어에서 개행이 몇개인지 최대 가로길이가 몇인지 체크
    for (i=0; i<nLen; ++i)
    {
        szWord = m_szMessage.Mid(i, 1);
    
        // 문자 크기별로.. -_- 정리해주기..
        // 내가 사용한게 Tahoma 이므로 -_- 그걸 기준으로다가..
        if (szWord == L"W")
        {
            nCntOnePixelWord += 12;            
        }
        else if (szWord == L"\"")
        {
            nCntOnePixelWord += 9;
        }
        else if (szWord == L"M" || szWord == L"O" ||
                 szWord == L"Q" || szWord == L"m" ||
                 szWord == L"w")
        {
            nCntOnePixelWord += 8;
        }
        else if (szWord == L"U" || szWord == L"R" ||
                 szWord == L"A" || szWord == L"C" ||
                 szWord == L"D" || szWord == L"G" ||
                 szWord == L"H" || szWord == L"N" ||
                 szWord == L"P" || szWord == L"E")
        {
            nCntOnePixelWord += 7;
        }
        else if (szWord == L"c" || szWord == L"k" ||
                 szWord == L"s" || szWord == L"z" ||
                 szWord == L"J" || szWord == L"L")
        {
            nCntOnePixelWord += 5;
        }
        else if (szWord == L"f" || szWord == L"r" ||
                 szWord == L"t" || szWord == L"I" ||
                 szWord == L" " )
        {
            nCntOnePixelWord += 4;
        }
        else if (szWord == L"j" || szWord == L"!")
        {
            nCntOnePixelWord += 3;
        }
        else if (szWord == L"i" || szWord == L"l")
        {
            nCntOnePixelWord += 2;
        }
        else
        {
            nCntOnePixelWord += 6;
        }

        if (szWord == L"\n")
        {
            ++nHeightCnt;

            if (nWidthCnt > nWidthMax)
            {
                nWidthMax             = nWidthCnt;
                nWidthCnt             = 0;
            }

            if (nCntOnePixelWord > nCntOnePixelWordMax)
            {
                nCntOnePixelWordMax = nCntOnePixelWord;
                nCntOnePixelWord    = 0;
            }
        }
        else
        {
            ++nWidthCnt;

            if (nWidthCnt > nWidthMax)
            {
                nWidthMax = nWidthCnt;
            }

            if (nCntOnePixelWord > nCntOnePixelWordMax)
            {
                nCntOnePixelWordMax = nCntOnePixelWord;
            }
        }
    }

    int nResizeWindow[2];
    int nAddPos[2] = {0,};
    nResizeWindow[1] = rtBase.Height()+25;

    if (nHeightCnt)
    {
        nAddPos[1]             = nHeightCnt * 12;
        nResizeWindow[1]    += nAddPos[1];
    }

    CStatic* pStc = (CStatic*)GetDlgItem(IDC_STC_MSGX);    

    if (nCntOnePixelWordMax > 128)
    {
        MoveWindow(rect.left, rect.top, nCntOnePixelWordMax + 75, nResizeWindow[1]);

        // 스테틱 사이즈 변경
        CRect rtStatic;

        pStc->GetWindowRect(&rtStatic);
        pStc->MoveWindow(60, 28, nCntOnePixelWordMax, rtStatic.Height()+nAddPos[1]);        
    }

    pStc->SetWindowText(m_szMessage);

    // 버튼이 한가운데로 오게 만들기
    GetClientRect(&rtBase);
    CRect rtBtn;    
    CButton* pBtn = (CButton*)GetDlgItem(IDOK);
    pBtn->GetClientRect(&rtBtn);

    int nBtnX, nBtnY;
    if (!m_nType)
    {
        nBtnX = int((rtBase.Width() - rtBtn.Width()) / 2);
    }
    else
    {
        nBtnX = int((rtBase.Width() - (rtBtn.Width()*2 + 10)) / 2);
    }
    
    nBtnY = rtBase.Height() - 40;
    pBtn->MoveWindow(nBtnX, nBtnY, rtBtn.Width(), rtBtn.Height());
    
    if (m_nType)
    {
        pBtnCancel->MoveWindow(nBtnX+rtBtn.Width()+10, nBtnY, rtBtn.Width(), rtBtn.Height());
    }

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

HBRUSH CMyMessageBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogSkin::OnCtlColor(pDC, pWnd, nCtlColor);

    if (pWnd->GetDlgCtrlID() == IDC_STC_MSGX)
    {
        pDC->SetBkMode(TRANSPARENT);
        return (HBRUSH)GetStockObject(NULL_BRUSH);
    }

    return hbr;
}

[code]
사용 예

// one
#define _LSMSG(S) { CString szLoadStr; szLoadStr.LoadString(S); CMyMessageBox dlg(szLoadStr); }

// two
CMyMessageBox dlg(m_szLoadString, 1);

if (dlg.DoModal() == IDOK)
{
    
}
[/code]

'Windows > MFC' 카테고리의 다른 글

#pragma warning(disable: number)  (0) 2013.10.02
warning C4996  (0) 2013.10.02
MyMessageBox  (0) 2013.10.02
MessageBox Hock  (0) 2013.10.02
Rounding window (CRgn CreateRoundRectRgn SetWindowRgn)  (0) 2013.10.02