2014년 10월 26일 일요일

다이얼로그에 스킨을 쉽게 입혀보자.

CDialogSK_Src.zip
CDialogSK_demo.zip

소스와 데모.

사용방법은 간단한데, 문제는 다이얼로그에 확대 혹은 최소화 기능(즉, 창의 크기를 늘리고 줄일 수 있게...)을 구현해놓고, 창의 크기를 변경하면 엄청난 깜빡임을 보게 된다.

구현상 어쩔 수 없긴 한데...이왕이면 이거 쓰는 사람은 그냥 창의 크기를 변경할 수 없게 해놓고 쓰는게 정신건강상 좋다.

Using The Code
The class can be used using the following steps
Add the files CDialogSK.h and CDialogSK.cpp to your project.
Include CDialogSK.h in the .h file for your dialog class
Replace all occurrences of "CDialog" with "CDialogSK" in the .h and .cpp files for your dialog class
If you plan to use a background image (bitmap) go to dialog properties, styles tab and make Style=Popup, Border=None, and uncheck the "Title Bar" check box.
At the end of OnInitDialog of your dialog class add calls to the appropriate methods in CDialogSK
BOOL CSkinDialog_DemoDlg::OnInitDialog()
{
    ...
    EnableEasyMove();  // enable moving of the dialog
                       // by clicking anywhere in the dialog
    SetBitmap (IDB_BACKGROUND); // set background bitmap
    SetStyle (LO_RESIZE); // resize dialog to the size of the bitmap
    SetTransparentColor(RGB(0, 255, 0)); // set green as the
                                         // transparent color

    return TRUE;
}
If you want to change the window style later you can call any of these method anywhere in your code at run time.
If for example you want to make a circular dialog. Create a image which has a blue circle on a green background. Then call SetBitmap with the path to the image and call SetTransparentColor passing the color of background (green). This will remove the background from view and you will get a circular window.
Methods
The following methods are present in CDialogSK class
DWORD SetBitmap (HBITMAP hBitmap); Sets a background bitmap based on a HBITMAP.
DWORD SetBitmap (int nBitmap); Sets a background bitmap based on a resource ID.
DWORD SetBitmap (LPCTSTR lpszFileName); Sets a background bitmap based on a Windows bitmap (.bmp) file.
void SetStyle (LayOutStyle style); Sets the bitmap layout style. Can be any of LO_DEFAULT, LO_TILE (tile image), LO_CENTER (center image), LO_STRETCH (stretch image to fit dialog), LO_RESIZE (resize dialog to fit the image).
void EnableEasyMove (BOOL pEnable = TRUE); If called with TRUE then you can move the dialog by clicking anywhere in the client area of the dialog.
BOOL SetTransparent (BYTE bAlpha); Make the dialog as a whole transparent. Range is 0(transparent) - 255 (opaque). This works only on Windows2000 and above (WinXP).
BOOL SetTransparentColor (COLORREF col, BOOL bTrans = TRUE); Make a particular color transparent. This works only on Windows2000 and above (WinXP). This can be used to create dialog in which you can see through parts of the dialog.

이 코드에 오타라고 보여지는 버그가 있다.

BOOL CDialogSK::OnEraseBkgnd(CDC* pDC)
{
...
pbmpOldBmp = (HBITMAP)::SelectObject(dc.m_hDC, m_hBitmap);
...
::SelectObject(dc.m_hDC, m_hBitmap);

return bRetValue;
}

아래처럼 고쳐야한다. 그냥 오타로 생각됨

BOOL CDialogSK::OnEraseBkgnd(CDC* pDC)
{
...
pbmpOldBmp = (HBITMAP)::SelectObject(dc.m_hDC, m_hBitmap);
...
::SelectObject(dc.m_hDC, pbmpOldBmp);

return bRetValue;
}

도움이 되길 바랍니다.

댓글 없음: