📄 skindialog.cpp
字号:
// SkinDialog.cpp : implementation file
//
#include "stdafx.h"
#include "MtoU.h"
#include "SkinDialog.h"
#include "common.h"
#include "BitmapBtn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////
////////////////////////////////////////////////////
class CAboutDialog : public CDialog
{
public:
CAboutDialog(CWnd* pParent = NULL);
//{{AFX_DATA(CAboutDialog)
enum { IDD = IDD_ABOUT };
//}}AFX_DATA
//{{AFX_VIRTUAL(CAboutDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
protected:
//{{AFX_MSG(CAboutDialog)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDialog::CAboutDialog(CWnd* pParent /*=NULL*/)
: CDialog(CAboutDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CAboutDialog)
//}}AFX_DATA_INIT
}
void CAboutDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDialog)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDialog, CDialog)
//{{AFX_MSG_MAP(CAboutDialog)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
////////////////////////////////////////////////////
////////////////////////////////////////////////////
CSkinDialog::~CSkinDialog()
{
if(pMemDC!=NULL)
delete pMemDC;
if(pBkBitmap!=NULL)
delete pBkBitmap;
if(pMainMenu!=NULL)
delete pMainMenu;
}
CSkinDialog::CSkinDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
//{{AFX_DATA_INIT(CSkinDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
pMainMenu = NULL;
pBkBitmap=NULL;
pMemDC=NULL;
pBtnIDArray=NULL;
}
void CSkinDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSkinDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSkinDialog, CDialog)
//{{AFX_MSG_MAP(CSkinDialog)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_COMMAND(IDM_LOADSKIN, OnLoadskin)
ON_WM_ERASEBKGND()
ON_COMMAND(IDM_ABOUT, OnAbout)
ON_COMMAND(IDM_EXIT, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CSkinDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION, 0);
CDialog::OnLButtonDown(nFlags, point);
}
void CSkinDialog::OnRButtonDown(UINT nFlags, CPoint point)
{
CPoint pp = point;
ClientToScreen(&pp);
if(pMainMenu!=NULL)
pMainMenu->GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, pp.x, pp.y, this);
CDialog::OnRButtonDown(nFlags, point);
}
void CSkinDialog::SetMainMenu(UINT nMenuID)
{
m_nMainMenuID = nMenuID;
}
void CSkinDialog::SetBtnID(CDWordArray *pArray)
{
pBtnIDArray = pArray;
}
void CSkinDialog::SetCtrlID(CDWordArray *pArray)
{
pCtrlIDArray = pArray;
}
BOOL CSkinDialog::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);
if(pBkBitmap!=NULL && pMemDC!=NULL)
pDC->BitBlt(0,0,m_WinSize.cx, m_WinSize.cy, pMemDC, 0,0, SRCCOPY);
return TRUE;
}
void CSkinDialog::InitSkinPathName()
{
char szAppName[MAX_PATH];
int len;
CString strDefault, strPathName;
strPathName = AfxGetApp()->GetProfileString("Skin", "PathName", NULL);
if(strPathName.IsEmpty())
{
::GetModuleFileName(AfxGetInstanceHandle(), szAppName, sizeof(szAppName));
len = strlen(szAppName);
for(int i=len; i>0; i--)
{
if(szAppName[i] == '\\')
{
szAppName[i+1]='\0';
break;
}
}
strcat(szAppName, "Skin\\Sample1\\skin.ini");
strPathName = szAppName;
}
m_SkinIniFile.SetPathName(strPathName);
AfxGetApp()->WriteProfileString("Skin", "PathName", strPathName);
strcpy(szAppName, strPathName);
len = strlen(szAppName);
for(int i=len; i>0; i--)
{
if(szAppName[i] == '\\')
{
szAppName[i+1]='\0';
break;
}
}
m_SkinPath = szAppName;
}
void CSkinDialog::OnLoadskin()
{
CFileDialog fd(TRUE);
if(fd.DoModal()!=IDOK)
return;
CString strPathName = fd.GetPathName();
m_SkinIniFile.SetPathName(strPathName);
AfxGetApp()->WriteProfileString("Skin", "PathName", strPathName);
char* szPathName = strPathName.GetBuffer(strPathName.GetLength());
int len = strlen(szPathName);
for(int i=len; i>0; i--)
{
if(szPathName[i] == '\\')
{
szPathName[i+1]='\0';
break;
}
}
strPathName.ReleaseBuffer();
m_SkinPath = strPathName;
LoadSkin();
}
BOOL CSkinDialog::OnInitDialog()
{
CDialog::OnInitDialog();
ModifyStyle(WS_CAPTION,NULL,SWP_DRAWFRAME);
ModifyStyleEx(WS_EX_DLGMODALFRAME,NULL,SWP_DRAWFRAME);
InitSkinPathName();
LoadSkin();
return TRUE;
}
void CSkinDialog::SetWindowTransparent()
{
CString strTrans,strDefault;
strTrans = m_SkinIniFile.ReadString("SCREEN", "TransparentQuotiety", strDefault);
int nTQ = atoi(strTrans);
if(nTQ == 0) nTQ = 100;
HWND hWnd = GetSafeHwnd();
HMODULE hModule =GetModuleHandle("User32.DLL");
if(hModule == NULL)
{
return;
}
typedef BOOL (WINAPI* SETLAYEREDWND)( HWND, COLORREF, BYTE, DWORD);
SETLAYEREDWND SetLayeredWindowPtr = NULL;
SetLayeredWindowPtr = (SETLAYEREDWND)GetProcAddress(hModule, "SetLayeredWindowAttributes");
if(SetLayeredWindowPtr)
{
LONG lStyle = GetWindowLong(hWnd, GWL_EXSTYLE) | 0x00080000;
SetWindowLong( hWnd, GWL_EXSTYLE, lStyle);
SetLayeredWindowPtr( hWnd,
0,
BYTE((255 * nTQ) / 100),
2);
}
}
void CSkinDialog::LoadSkin()
{
LoadMenu();
LoadBkBitmap();
LoadBtnCtrl();
LoadCtrlPosition();
//The function is valid only in win2000
SetWindowTransparent();
Invalidate();
}
void CSkinDialog::LoadMenu()
{
if(pMainMenu!=NULL)
{
delete pMainMenu;
pMainMenu = NULL;
}
pMainMenu = new CMenu;
if(!pMainMenu->LoadMenu(m_nMainMenuID))
{
delete pMainMenu;
pMainMenu = NULL;
return;
}
CMenu* pMenu = pMainMenu->GetSubMenu(0);
int nCount = pMenu->GetMenuItemCount();
CString strText, strDefault, strItem;
UINT nMenuID;
for(int i=0; i<nCount; i++)
{
pMenu->GetMenuString(i, strText, MF_BYPOSITION);
if(!strText.IsEmpty())
{
strItem =m_SkinIniFile.ReadString("MAINMENU", strText, strDefault);
if(!strItem.IsEmpty())
{
nMenuID = pMenu->GetMenuItemID(i);
pMenu->ModifyMenu(i, MF_BYPOSITION, nMenuID, strItem);
}
}
}
}
void CSkinDialog::LoadBkBitmap()
{
CString strDefault, strNormal,strTitle;
strTitle = m_SkinIniFile.ReadString("SCREEN", "Title", strDefault);
SetWindowText(strTitle);
strNormal = m_SkinIniFile.ReadString("SCREEN", "Normal", strDefault);
if(!strNormal.IsEmpty())
strNormal = m_SkinPath + strNormal;
else
return;
//Load normal bitmap ,set background bitmap
if(pBkBitmap!=NULL)
{
delete pBkBitmap;
pBkBitmap = NULL;
}
pBkBitmap = new CBitmap;
if(!LoadImageFile(strNormal, pBkBitmap, m_WinSize) )
{
delete pBkBitmap;
pBkBitmap = NULL;
return;
}
//Set window region and size
CRect rect;
GetWindowRect(&rect);
MoveWindow(rect.left, rect.top, m_WinSize.cx, m_WinSize.cy);
COLORREF crTrans=0, crArr=0;
GetBkColorValue(crTrans, crArr);
HRGN hRgn = BitmapToRegion((HBITMAP)pBkBitmap->GetSafeHandle(), crTrans, crArr);
SetWindowRgn(hRgn, TRUE);
if(pMemDC!=NULL)
{
pMemDC->DeleteDC();
delete pMemDC;
}
CDC *pDC=GetDC();
pMemDC = new CDC;
pMemDC->CreateCompatibleDC(pDC);
pMemDC->SelectObject(pBkBitmap);
ReleaseDC(pDC);
}
void CSkinDialog::LoadBtnCtrl()
{
ASSERT(pBtnIDArray!=NULL);
int nCount = pBtnIDArray->GetSize();
if(nCount<=0)
return;
CString strDefault, strNormal, strOver, strDown, strDisable;
strNormal = m_SkinIniFile.ReadString("SCREEN", "Normal", strDefault);
strOver = m_SkinIniFile.ReadString("SCREEN", "Over", strDefault);
strDown = m_SkinIniFile.ReadString("SCREEN", "Down", strDefault);
strDisable = m_SkinIniFile.ReadString("SCREEN", "Disable", strDefault);
CBitmap* pNormalBitmap = NULL;
CBitmap* pOverBitmap = NULL;
CBitmap* pDownBitmap = NULL;
CBitmap* pDisableBitmap = NULL;
CSize size;
if(!strNormal.IsEmpty())
{
strNormal = m_SkinPath + strNormal;
pNormalBitmap = new CBitmap;
if(!LoadImageFile(strNormal, pNormalBitmap, size) )
{
delete pNormalBitmap;
pNormalBitmap = NULL;
}
}
if(!strOver.IsEmpty())
{
strOver = m_SkinPath + strOver;
pOverBitmap = new CBitmap;
if(!LoadImageFile(strOver, pOverBitmap, size) )
{
delete pOverBitmap;
pOverBitmap = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -