返回> 网站首页 

Vc自定义贴图界面的简单实现

yoours2011-03-10 14:04:15 阅读 1405

简介一边听听音乐,一边写写文章。

1.导入皮肤资源

2.在.h中定义
// 标题栏
CBitmap m_BitmapTopLeft; 
CSize m_szBitmapTopLeft;
CBitmap m_BitmapTopMid;
CSize m_szBitmapTopMid;
CBitmap m_BitmapTopRight;
CSize m_szBitmapTopRight;
// 窗体
CBitmap m_BitmapCenterLeft; 
CSize m_szBitmapCenterLeft;
CBitmap m_BitmapCenterMid;
CSize m_szBitmapCenterMid;
CBitmap m_BitmapCenterRight;
CSize m_szBitmapCenterRight;
// 窗体底部
CBitmap m_BitmapBottomLeft; 
CSize m_szBitmapBottomLeft;
CBitmap m_BitmapBottomMid;
CSize m_szBitmapBottomMid;
CBitmap m_BitmapBottomRight;
CSize m_szBitmapBottomRight;

3.在xxxapp.h中定义


void SetImage(CBitmap &bitmap, UINT nID);
void SetImage(CBitmap &bitmap, CString strName);
void SetImageSize(CBitmap *pBitmap, CSize &sz);
void DrawPosImage(CBitmap *pBitmap, CDC *pDC, CPoint pt);
void DrawRangeImage(CBitmap *pBitmap, CDC *pDC, CRect rc);

void ChangeWindowRgn(CDialog *pDlg, CDC *pDC, COLORREF MaskColor = RGB(255,0,0));

};
extern CSkinTestApp theApp;//here

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}


4.在xxxapp.cpp中实现
//定点画位图
void CSkinTestApp::DrawPosImage(CBitmap *pBitmap, CDC *pDC, CPoint pt)
{
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap = (CBitmap*)MemDC.SelectObject(pBitmap);

BITMAP bm;
pBitmap->GetBitmap(&bm);
int li_Width = bm.bmWidth;
int li_Height = bm.bmHeight;

pDC->BitBlt(pt.x, pt.y, li_Width, li_Height, &MemDC, 0, 0, SRCCOPY);

MemDC.SelectObject(pOldBitmap);
MemDC.DeleteDC();
}

//平铺画位图
void CSkinTestApp::DrawRangeImage(CBitmap *pBitmap, CDC *pDC, CRect rc)
{
CDC MemDC;
BITMAP bm;
pBitmap->GetBitmap(&bm);

int li_Width = bm.bmWidth;
int li_Height = bm.bmHeight;

MemDC.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = MemDC.SelectObject(pBitmap);

int x=rc.left;
int y=rc.top;
while (y < (rc.Height()+rc.top)) 
{
while(x < (rc.Width()+rc.left)) 
{
pDC->BitBlt(x, y, li_Width, li_Height, &MemDC, 0, 0, SRCCOPY);
x += li_Width;
}
x = rc.left;
y += li_Height;
}

MemDC.SelectObject(pOldBitmap);
MemDC.DeleteDC();
}

//载入硬盘位图
void CSkinTestApp::SetImage(CBitmap &bitmap, CString strName)
{
HBITMAP hBitmap = NULL;
hBitmap = (HBITMAP)::LoadImage(NULL, strName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE); 

if(hBitmap == NULL)
{
CString str;
str.Format(_T("加载 %s 失败"), strName); 

MessageBox(NULL, str, _T("加载位图失败"), MB_ICONSTOP);
PostQuitMessage(0);
}

if (bitmap.m_hObject)
bitmap.Detach();

bitmap.Attach(hBitmap);
}

//载入资源位图
void CSkinTestApp::SetImage(CBitmap &bitmap, UINT nID)
{
if (bitmap.m_hObject)
bitmap.Detach();

bitmap.LoadBitmap(nID);
}

//保存位图尺寸
void CSkinTestApp::SetImageSize(CBitmap *pBitmap, CSize &sz)
{
BITMAP bm;
pBitmap->GetBitmap(&bm);

sz = CSize(bm.bmWidth, bm.bmHeight);
}

// 创建不规则窗体
void CSkinTestApp::ChangeWindowRgn(CDialog *pDlg, CDC *pDC, COLORREF MaskColor)
{
COLORREF col = RGB(255,0,0);

CRect rcClient;
pDlg->GetClientRect (rcClient);
CRgn rgn;
rgn.CreateRectRgn (0, 0, rcClient.Width(), rcClient.Height());
int i=8,x,y;
for (y=0; y<i; y++)
{
for (x=0; x<i; x++)
{
// If this pixel is transparent
if (pDC->GetPixel(x,y)==col)
{
CRgn rgnAdd;
rgnAdd.CreateRectRgn (x,
y, x+1, y+1);
rgn.CombineRgn(&rgn, &rgnAdd, RGN_DIFF);
}
}
for (x=rcClient.Width()-i; x<rcClient.Width(); x++)
{
// If this pixel is transparent
if (pDC->GetPixel(x,y)==col)
{
CRgn rgnAdd;
rgnAdd.CreateRectRgn (x,
y, x+1, y+1);
rgn.CombineRgn(&rgn, &rgnAdd, RGN_DIFF);
}
}

for (y=rcClient.Height()-i; y<rcClient.Height(); y++)
{
for (x=0; x<i; x++)
{
// If this pixel is transparent
if (pDC->GetPixel(x,y)==col)
{
CRgn rgnAdd;
rgnAdd.CreateRectRgn (x,
y, x+1, y+1);
rgn.CombineRgn(&rgn, &rgnAdd, RGN_DIFF);
}
}
for (x=rcClient.Width()-i; x<rcClient.Width(); x++)
{
// If this pixel is transparent
if (pDC->GetPixel(x,y)==col)
{
CRgn rgnAdd;
rgnAdd.CreateRectRgn (x,
y, x+1, y+1);
rgn.CombineRgn(&rgn, &rgnAdd, RGN_DIFF);
}
}
}

pDlg->SetWindowRgn (rgn, TRUE);
}

5.在xxxdlg.cpp中的OnInitDialog中调用ReLoadSkin();
void CSkinTestDlg::ReLoadSkin()
{
theApp.SetImage(m_BitmapTopLeft, IDB_TOP_LEFT);
theApp.SetImageSize(&m_BitmapTopLeft, m_szBitmapTopLeft);

theApp.SetImage(m_BitmapTopMid, IDB_TOP_CENTER);
theApp.SetImageSize(&m_BitmapTopMid, m_szBitmapTopMid);

theApp.SetImage(m_BitmapTopRight, IDB_TOP_RIGHT);
theApp.SetImageSize(&m_BitmapTopRight, m_szBitmapTopRight);

theApp.SetImage(m_BitmapCenterLeft, IDB_CENTER_LEFT);
theApp.SetImageSize(&m_BitmapCenterLeft, m_szBitmapCenterLeft);

theApp.SetImage(m_BitmapCenterRight, IDB_CENTER_RIGHT);
theApp.SetImageSize(&m_BitmapCenterRight, m_szBitmapCenterRight);

theApp.SetImage(m_BitmapBottomMid, IDB_BOTTOM);
theApp.SetImageSize(&m_BitmapBottomMid, m_szBitmapBottomMid);

}

void CSkinTestDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
// 移动标题栏
if(point.y <= m_szBitmapTopLeft.cy)
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x, point.y));
CDialog::OnLButtonDown(nFlags, point);
}

6.在xxxdlg.cpp中的OnPaint中
void CSkinTestDlg::OnPaint() 
{

if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{

CPaintDC dc(this); // device context for painting

CRect rcClient;
GetClientRect(&rcClient);

//内存画图//////////////////////////
CDC m_MemDC;
m_MemDC.CreateCompatibleDC(&dc);

CBitmap btScreen;
btScreen.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());

m_MemDC.SelectObject(&btScreen);
btScreen.DeleteObject(); 

//画背景
m_MemDC.Rectangle(rcClient);

// 画标题栏
theApp.DrawPosImage(&m_BitmapTopLeft, &m_MemDC, CPoint(0, 0));
theApp.DrawRangeImage(&m_BitmapTopMid, &m_MemDC, CRect(m_szBitmapTopLeft.cx, 0, rcClient.Width()-m_szBitmapTopRight.cx, m_szBitmapTopMid.cy));
theApp.DrawPosImage(&m_BitmapTopRight, &m_MemDC, CPoint(rcClient.Width()-m_szBitmapTopRight.cx, 0));

// 画窗体(左 右)
theApp.DrawPosImage(&m_BitmapCenterLeft, &m_MemDC, CPoint(0, m_szBitmapTopLeft.cy));
theApp.DrawPosImage(&m_BitmapCenterRight, &m_MemDC, CPoint(rcClient.Width()-m_szBitmapCenterRight.cx,m_szBitmapTopLeft.cy)); 

// 画窗体底部
theApp.DrawPosImage(&m_BitmapBottomMid, &m_MemDC, CPoint(0, rcClient.Height()-m_szBitmapBottomMid.cy));

//创建不规则窗体
//theApp.ChangeWindowRgn(this, &m_MemDC);
//画到显示器上////////////////////////////
dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &m_MemDC, 0, 0, SRCCOPY);

m_MemDC.DeleteDC();
}
}
微信小程序扫码登陆

文章评论

1405人参与,0条评论