📄 croundctrl.cpp
字号:
// CRoundCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "USBOscillograph.h"
#include "CRoundCtrl.h"
#include "math.h"
#define PI 3.1415926
#define STEP 3*PI/40
//#define START_A -3*PI/4
#define START -3*PI/4
#define SLEEP 15
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCRoundCtrl
CCRoundCtrl::CCRoundCtrl()
{
Bitmap.LoadBitmap (IDB_ROUNDBACK);
Bitmap.GetObject(sizeof(BITMAP),&bm);
B_point.LoadBitmap (IDB_POINT);
B_point.GetObject(sizeof(BITMAP),&m_bm);
// backBitmap.LoadBitmap (IDB_BACK);
sum_a=7*PI/4;
a=-3*PI/4;
start_a=START;
START_A=START;
SwitchNumber=4;
disStr=_T("");
sFont.CreateFont(15,0,0,0,400,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,NULL);
//默认颜色
backColor=GetSysColor(COLOR_3DFACE); //默认颜色 系统浅灰色
textColor=RGB(0,0,0); //文本颜色
lineColor=RGB(0,0,0);
SwitchName=_T("旋转开关"); //默认按钮名称
}
CCRoundCtrl::~CCRoundCtrl()
{
}
BEGIN_MESSAGE_MAP(CCRoundCtrl, CButton)
//{{AFX_MSG_MAP(CCRoundCtrl)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_KILLFOCUS()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCRoundCtrl message handlers
//绘制旋转开关外貌
BOOL CCRoundCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CBrush brush(backColor);
GetClientRect (&rect);
pDC->SelectStockObject (NULL_PEN);
pDC->SelectObject (&brush);
pDC->Rectangle (rect);
CPen pen( PS_SOLID,1, RGB(0,0,0));
//显示3D 粗线条
CPen pen2(PS_SOLID,5,RGB(120,120,120));
CPen pen1(PS_SOLID,5,RGB(255,255,255));
CPen * pOldPen=pDC->SelectObject (&pen);
roundRect.top = rect.top+rect.Height ()/4;
roundRect.left =rect.left +rect.Width ()/4;
roundRect.bottom=roundRect.top+rect.Height ()/2;
roundRect.right =roundRect.left+rect.Width ()/2;
CRect rrect(roundRect.left+3,roundRect.top+3,roundRect.right-3,roundRect.bottom-3);
CBrush brush1(backColor);/*RGB(192,192,192)*/
pDC->SelectObject (&brush1);
pDC->Ellipse (roundRect); //绘制圆
pDC->SelectObject (pOldPen);
selRect.left =roundRect.left +9;
selRect.top =roundRect.top+9;
selRect.right =roundRect.right -9;
selRect.bottom =roundRect.bottom -9;
int x1,y1,x2,y2;
x1=roundRect.left+roundRect.Width ()/2+roundRect.Width ()/2*cos(PI/4);
y1=roundRect.top+roundRect.Height()/2 -roundRect.Height()/2*sin(PI/4);
x2=roundRect.left+roundRect.Width ()/2-roundRect.Width ()/2*sin(PI/4);
y2=roundRect.top+roundRect.Height()/2 +roundRect.Height()/2*cos(PI/4);
pOldPen=pDC->SelectObject (&pen1);
CPoint p1(x1,y1);
CPoint p2(x2,y2);
pDC->Arc (&rrect,p1,p2);
pDC->SelectObject (pOldPen);
pOldPen=pDC->SelectObject (&pen2);
pDC->Arc (&rrect,p2,p1);
Drawstutas(pDC,&selRect);
CRect rt(10,rect.Height ()-20,rect.Width (),rect.Height ()-1);
pDC->DrawText (SwitchName,rt,DT_CENTER|DT_BOTTOM |DT_SINGLELINE);
/*
int r=selRect.Width () /2;
int round_r=r-10;
int x=round_r*sin(a);
int y=round_r*cos(a);
int XX=selRect.left+selRect.Width ()/2+x-m_bm.bmWidth/2;
int YY=selRect.top+selRect.Height ()/2-y-m_bm.bmHeight/2;
CDC m_DC;
m_DC.CreateCompatibleDC (pDC);
CBitmap * pOldBitmap =m_DC.SelectObject(&B_point);
pDC->BitBlt (XX,YY,m_bm.bmWidth ,m_bm.bmHeight,&m_DC,0,0,SRCCOPY);
m_DC.SelectObject (pOldBitmap);
m_DC.DeleteDC ();
*/
return true;
}
void CCRoundCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
}
void CCRoundCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptClicked=point;
CButton::OnLButtonDown(nFlags, point);
}
void CCRoundCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(nFlags=MK_LBUTTON)
{m_ptClicked=point;
SendMessage(BM_SETSTATE,0,0);
}
CButton::OnMouseMove(nFlags, point);
}
void CCRoundCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
//if(lpDrawItemStruct->itemState==ODA_SELECT||lpDrawItemStruct->itemState==ODA_DRAWENTIRE )
//return;
if(lpDrawItemStruct->itemAction==ODA_SELECT)
{
CPen pen( PS_DOT,1, RGB(100,100,100));
CPen * pOldPen=pDC->SelectObject (&pen);
pDC->SelectStockObject (NULL_BRUSH );
pDC->Ellipse (&selRect);
CPoint ptMid(rect.Width ()/2,rect.Height ()/2);
int dRelX=m_ptClicked.x-ptMid.x;
int dRelY=ptMid.y-m_ptClicked.y;
a=atan2(dRelX,dRelY);
for(int i=0;i<SwitchNumber;i++)
{
if(a>(START_A+i*step_a-step_a/2)&&a<START_A+i*step_a+step_a/2)
break;
}
if(i==SwitchNumber)
i=0;
a=START_A+i*step_a;
Select=i;
int r=selRect.Width () /2;
int round_r=r-10;
int x;
int y;
int XX;
int YY;
CDC m_DC;
m_DC.CreateCompatibleDC (pDC);
CBitmap * pOldBitmap =m_DC.SelectObject(&B_point);
COLORREF bkcolor=pDC->GetPixel(rect.Width ()/2-2,rect.Height ()/2-2);
//显示动画过程
double ta;
if(a>start_a)
{for(ta=start_a;ta<=a;ta+=STEP)
{
x=round_r*sin(ta);
y=round_r*cos(ta);
XX=selRect.left +selRect.Width ()/2+x-m_bm.bmWidth/2;
YY=selRect.top + selRect.Height ()/2-y-m_bm.bmHeight/2;
pDC->BitBlt (XX,YY,m_bm.bmWidth ,m_bm.bmHeight,&m_DC,0,0,SRCCOPY);
Sleep(SLEEP);
pDC->FillSolidRect(XX,YY,m_bm.bmWidth+2 ,m_bm.bmHeight+2,bkcolor);
}
x=round_r*sin(a);
y=round_r*cos(a);
XX=selRect.left+selRect.Width ()/2+x-m_bm.bmWidth/2;
YY=selRect.top+selRect.Height ()/2-y-m_bm.bmHeight/2;
pDC->BitBlt (XX,YY,m_bm.bmWidth ,m_bm.bmHeight,&m_DC,0,0,SRCCOPY);
}
else
{
for(ta=start_a;ta>=a;ta-=STEP)
{
x=round_r*sin(ta);
y=round_r*cos(ta);
XX=selRect.left+selRect.Width ()/2+x-m_bm.bmWidth/2;
YY=selRect.top+selRect.Height ()/2-y-m_bm.bmHeight/2;
pDC->BitBlt (XX,YY,m_bm.bmWidth ,m_bm.bmHeight,&m_DC,0,0,SRCCOPY);
Sleep(SLEEP);
pDC->FillSolidRect(XX,YY,m_bm.bmWidth+2 ,m_bm.bmHeight+2,bkcolor);
}
x=round_r*sin(a);
y=round_r*cos(a);
XX=selRect.left+selRect.Width ()/2+x-m_bm.bmWidth/2;
YY=selRect.top+selRect.Height ()/2-y-m_bm.bmHeight/2;
pDC->BitBlt (XX,YY,m_bm.bmWidth ,m_bm.bmHeight,&m_DC,0,0,SRCCOPY);
}
start_a=a;
m_DC.SelectObject (pOldBitmap);
m_DC.DeleteDC ();
}
}
BOOL CCRoundCtrl::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CButton::PreTranslateMessage(pMsg);
}
void CCRoundCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CButton::PreSubclassWindow();
}
LRESULT CCRoundCtrl::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
return CButton::DefWindowProc(message, wParam, lParam);
}
void CCRoundCtrl::SetSwitchNumber(int Number)
{
SwitchNumber=Number;
step_a=(sum_a)/(SwitchNumber);
}
void CCRoundCtrl::SetDisString(CString str)
{
disStr=str;
char *cp=disStr.GetBuffer (disStr.GetLength ());
int j=0;
int length=disStr.GetLength ();
int l,i;
//disStr.IsEmpty
BOOL IsEmpty =false;
i=0;
for(l=0;l<length;l++)
{
if(*cp!=' ')
{dismes[j][i++]=* cp;
IsEmpty=false;
}
else
{
if(!IsEmpty&&i!=0)
{ dismes[j][i]=0;
i=0;
j++;
IsEmpty=true;
}
}
cp++;
}
dismes[j][i]=0;
}
void CCRoundCtrl::SetStartSwitch(int sit) /* sit =0,1...Number-1*/
{
if(sit>SwitchNumber)return;
Select=sit;
a=START_A+sit*step_a;
start_a=a;
}
int CCRoundCtrl::GetSelectSwitch()
{
return Select;
}
void CCRoundCtrl::SetSwitchName(LPCSTR name)
{
SwitchName=name;
}
void CCRoundCtrl::SetBKColor(COLORREF color)
{
backColor=color;
}
void CCRoundCtrl::SetTextColor(COLORREF color)
{
textColor=color;
}
void CCRoundCtrl::SetLineColor(COLORREF color)
{
lineColor=color;
}
void CCRoundCtrl::OnKillFocus(CWnd* pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
Invalidate (true);
// TODO: Add your message handler code here
}
void CCRoundCtrl::SetStartPoint(float sum_A,float start_A)
{
sum_a=sum_A;
START_A=start_A;
}
void CCRoundCtrl::Drawstutas(CDC *pDC, CRect selRect)
{
int r=selRect.Width () /2;
int round_r=r-10;
int x=round_r*sin(a);
int y=round_r*cos(a);
int XX=selRect.left+selRect.Width ()/2+x-m_bm.bmWidth/2;
int YY=selRect.top+selRect.Height ()/2-y-m_bm.bmHeight/2;
CDC m_DC;
m_DC.CreateCompatibleDC (pDC);
CBitmap * pOldBitmap =m_DC.SelectObject(&B_point);
pDC->BitBlt (XX,YY,m_bm.bmWidth ,m_bm.bmHeight,&m_DC,0,0,SRCCOPY);
m_DC.SelectObject (pOldBitmap);
m_DC.DeleteDC ();
CFont * pOldFont;
pOldFont=pDC->SelectObject(&sFont);
r=roundRect.Width ()/2;
round_r=r+roundRect.Width ()/4;
double round_r1=r+2;
double round_r2=r+10;
pDC->SetBkColor (backColor);
pDC->SetTextColor (textColor);
CPen pen( PS_SOLID,1, lineColor );
CPen * pOldPen=pDC->SelectObject (&pen);
for(int i=0;i<SwitchNumber;i++)
{
x=(round_r)*sin(START_A+step_a*i);
y=(round_r)*cos(START_A+step_a*i);
XX=rect.Width ()/2+x-roundRect.Width ()/8;
YY=rect.Height ()/2-y-roundRect.Width ()/8;
pDC->TextOut(XX,YY,dismes[i]);
x=round_r2*sin(START_A+step_a*i);
y=round_r2*cos(START_A+step_a*i);
XX=rect.Width ()/2+x;
YY=rect.Height ()/2-y;
pDC->MoveTo (XX,YY);
x=round_r1*sin(START_A+step_a*i);
y=round_r1*cos(START_A+step_a*i);
XX=rect.Width ()/2+x;
YY=rect.Height ()/2-y;
pDC->LineTo (XX,YY);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -