自繪static控制元件,實現特殊效果

shaderdx發表於2015-08-12


#pragma once




// CMyStatic


class CMyStatic : public CStatic
{
DECLARE_DYNAMIC(CMyStatic)


public:
CMyStatic();
virtual ~CMyStatic();


protected:
DECLARE_MESSAGE_MAP()


private:
int m_iDrawFlag;//0無繪,1左繪,2右繪,3左右繪
int m_iDrawNum;//序列迴圈次數
int m_iDrawType;//需要繪製的圖片
bool m_bSelected;//當前是否被選中
public:
void SetDrawFlag(int iDrawFlag) {m_iDrawFlag = iDrawFlag;}
void SetDrawNum(int iDrawNum) {m_iDrawNum = iDrawNum;}
void SetDrawType(int iDrawType) {m_iDrawType = iDrawType;}
void SetSelected(bool bSel) {m_bSelected = bSel;}
afx_msg void OnPaint();
};





// MyStatic.cpp : 實現檔案
//


#include "stdafx.h"
#include "TestDock.h"
#include "MyStatic.h"




// CMyStatic


IMPLEMENT_DYNAMIC(CMyStatic, CStatic)


CMyStatic::CMyStatic()
: m_iDrawFlag(0)
, m_iDrawNum(0)
, m_iDrawType(0)
, m_bSelected(false)
{
//ModifyStyle(0,SS_BITMAP|SS_CENTERIMAGE);
}


CMyStatic::~CMyStatic()
{
}




BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()






// CMyStatic 訊息處理程式








void CMyStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此處新增訊息處理程式程式碼
// 不為繪圖訊息呼叫 CStatic::OnPaint()
CRect reWnd;
GetClientRect(&reWnd);
//dc.MoveTo(reWnd.left+2, reWnd.top+2);
//dc.LineTo(reWnd.left+2, reWnd.bottom-2);
//dc.MoveTo(reWnd.right-2, reWnd.top+2);
//dc.LineTo(reWnd.right-2, reWnd.bottom-2);
dc.SetTextColor(RGB(255,0,255));
dc.SetBkMode(TRANSPARENT);
CPen pen(PS_SOLID, 2, RGB(255,0,0));
CPen* pOldPen = dc.SelectObject(&pen);
if (!m_bSelected)
{
dc.FillSolidRect(reWnd, RGB(0,255,255));
}
else
{
dc.FillSolidRect(reWnd, RGB(255,255,0));
}
CImage img;
if (m_iDrawType == 0)
{
img.Load(L"res\\self.png");
}
else
{
img.Load(L"res\\CurTrade.png");
}
CRect reImg = reWnd;
reImg.DeflateRect(3,3);
img.Draw(dc.m_hDC, reImg);


const int ciLeft = 6;
const int ciTop = 6;

if (m_iDrawFlag == 3)
{
//左右繪
dc.MoveTo(reWnd.left+ciLeft*2, reWnd.top+ciTop);
dc.LineTo(reWnd.left+ciLeft, reWnd.top+ciTop);
dc.LineTo(reWnd.left+ciLeft, reWnd.bottom-ciTop);
dc.LineTo(reWnd.left+ciLeft*2, reWnd.bottom-ciTop);
CString csText;
csText.Format(L"%d", m_iDrawNum);
dc.TextOutW(reWnd.right-ciLeft*4, reWnd.top+ciTop*2, csText);
dc.MoveTo(reWnd.right-ciLeft*2, reWnd.top+ciTop);
dc.LineTo(reWnd.right-ciLeft, reWnd.top+ciTop);
dc.LineTo(reWnd.right-ciLeft, reWnd.bottom-ciTop);
dc.LineTo(reWnd.right-ciLeft*2, reWnd.bottom-ciTop);
}
else if (m_iDrawFlag == 1)
{
//左繪
dc.MoveTo(reWnd.left+ciLeft*2, reWnd.top+ciTop);
dc.LineTo(reWnd.left+ciLeft, reWnd.top+ciTop);
dc.LineTo(reWnd.left+ciLeft, reWnd.bottom-ciTop);
dc.LineTo(reWnd.left+ciLeft*2, reWnd.bottom-ciTop);
}
else if (m_iDrawFlag == 2)
{
//右繪
CString csText;
csText.Format(L"%d", m_iDrawNum);
dc.TextOutW(reWnd.right-ciLeft*4, reWnd.top+ciTop*2, csText);
dc.MoveTo(reWnd.right-ciLeft*2, reWnd.top+ciTop);
dc.LineTo(reWnd.right-ciLeft, reWnd.top+ciTop);
dc.LineTo(reWnd.right-ciLeft, reWnd.bottom-ciTop);
dc.LineTo(reWnd.right-ciLeft*2, reWnd.bottom-ciTop);
}
else
{
//無繪
}


dc.SelectObject(pOldPen);
}



相關文章