📄 clockctl.cpp
字号:
// ClockCtl.cpp : implementation file
//
#include "stdafx.h"
#include "ClockCtl.h"
#include "pos.h"
// CClockCtl
IMPLEMENT_DYNAMIC(CClockCtl, CWnd)
CClockCtl::CClockCtl()
{
GetLocalTime(&RealTime);
m_Second = RealTime.wSecond;
m_Minute = RealTime.wMinute;
if (RealTime.wHour>11)
{
m_Hour = RealTime.wHour-12;
DayOrAfter = FALSE;
}
else
{
m_Hour = RealTime.wHour;
DayOrAfter = TRUE;
}
}
CClockCtl::~CClockCtl()
{
}
BEGIN_MESSAGE_MAP(CClockCtl, CWnd)
ON_WM_PAINT()
ON_WM_SHOWWINDOW()
ON_WM_ACTIVATE()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CClockCtl message handlers
void CClockCtl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rect;
// GetParent()->GetClientRect(&rect);
rect.left=0;
rect.top=0;
rect.right=320;
rect.bottom=234;
SetWindowPos(&wndTop, 0, 0, rect.Width(), rect.Height(), SWP_SHOWWINDOW | SWP_NOMOVE);
CreateOffScreenDC(&dc,rect);
dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_MemDC,0,0,SRCCOPY);
static BOOL bisStart = FALSE;
if(bisStart == FALSE )
{
SetTimer(1000,200,NULL);
bisStart = TRUE;
}
m_MemDC.DeleteDC();
m_Bitmap.DeleteObject();
// Do not call CWnd::OnPaint() for painting messages
}
void CClockCtl::CreateOffScreenDC(CDC* pdc,CRect& rect)
{
// CDC* pdc = GetDC();
if(m_MemDC.m_hDC != NULL)
{
m_MemDC.DeleteDC();
}
if( m_Bitmap.m_hObject != NULL)
{
m_Bitmap.DeleteObject();
}
m_MemDC.CreateCompatibleDC(pdc);
m_Bitmap.CreateCompatibleBitmap(pdc,rect.Width(),rect.Height());
m_MemDC.SelectObject(&m_Bitmap);
//Create back
CBitmap bitmapBack;
bitmapBack.LoadBitmap(IDB_BACKGROUD);
CDC dcBack;
dcBack.CreateCompatibleDC(pdc);
CBitmap* pOldBitmap=dcBack.SelectObject(&bitmapBack);
//paint back
m_MemDC.BitBlt(0,0,rect.Width(),rect.Height(),&dcBack,0,0,SRCCOPY);
dcBack.SelectObject(pOldBitmap);
DrawHalfDayStatus(DayOrAfter);
int nTemp = (m_Hour * 60 + m_Minute)/12;
DrawPointer(pdc,IDB_HOUR,nTemp);
DrawPointer(pdc,IDB_MINUTE,m_Minute);
DrawPointer(pdc,IDB_SECOND,m_Second);
bitmapBack.DeleteObject();
dcBack.DeleteDC();
}
void CClockCtl::RotateBitblt(CDC* pdc,HDC hSrc,HDC& hDes,/*HBITMAP& hBitmap,*/CSize size,CPoint pos)
{
int width = size.cx;
int height = size.cy;
BITMAPINFO bmInfo;
memset(&bmInfo.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = width;
bmInfo.bmiHeader.biHeight = height;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 24;
bmInfo.bmiHeader.biCompression = BI_RGB;
//Create a compatible memory DC
HDC memDC =::CreateCompatibleDC(pdc->m_hDC);
//create a new bitmap and select it in the memory dc
BYTE *pbase;
HBITMAP hBmp = ::CreateDIBSection(memDC, &bmInfo, DIB_RGB_COLORS, (void**)&pbase, 0, 0);
HBITMAP hBmpOld = (HBITMAP)::SelectObject(memDC,hBmp);
::BitBlt(memDC,0,0,width,height,hSrc,pos.x,pos.y,SRCCOPY);
hBmp = (HBITMAP)SelectObject(memDC,hBmpOld); //Jack modify
//TODO : process DATA
BYTE* pScreenBufferSrc = pbase;
int widthBytes = 4*((width*24+31)/32);
int widthBytes2 = 4*((height*24+31)/32);
BYTE* pScreenBufferDes = new BYTE[widthBytes2*width];
for(int j = 0;j< height;j++)
{
for(int i=0;i<width;i++)
{
memcpy(pScreenBufferDes + Pixel2Offset(height-1-j,i,height,width),pScreenBufferSrc +Pixel2Offset(i,j,width,height),3*sizeof(BYTE));
}
}
//Create rotate DC
BITMAPINFO bmInfoRotate;
memset(&bmInfoRotate.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
bmInfoRotate.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfoRotate.bmiHeader.biWidth = height;
bmInfoRotate.bmiHeader.biHeight = width;
bmInfoRotate.bmiHeader.biPlanes = 1;
bmInfoRotate.bmiHeader.biBitCount = 24;
bmInfoRotate.bmiHeader.biCompression = BI_RGB;
HDC hdcRotate = ::CreateCompatibleDC(pdc->m_hDC);
BYTE *pbaseBuffer;
HBITMAP hBmpRotate = ::CreateDIBSection(hdcRotate, &bmInfoRotate, DIB_RGB_COLORS, (void**)&pbaseBuffer, 0, 0);
memcpy(pbaseBuffer,pScreenBufferDes,widthBytes2*width);
delete []pScreenBufferDes;
HBITMAP hBmpRotateOld = (HBITMAP)::SelectObject(hdcRotate,hBmpRotate);//Jack modify
//Test only
/* HDC dc = ::GetDC(NULL);
BitBlt(dc,0,20,height,width,hdcRotate,0,0,SRCCOPY);
BitBlt(dc,0,0,width,height,memDC,0,0,SRCCOPY); */
//paint des bitmap
::BitBlt(hDes,0,0,height,width,hdcRotate,0,0,SRCCOPY);
hBmpRotate = (HBITMAP)SelectObject(hdcRotate,hBmpRotateOld);//Jack modify
DeleteDC(memDC);
DeleteObject(hBmp);
DeleteObject(hBmpRotate);
DeleteDC(hdcRotate);
}
int CClockCtl::Pixel2Offset(int x,int y,int width,int height)
{
int Offset = 0;
int widthBytes = 4*((width*24+31)/32);
Offset = (height-y-1)*widthBytes + x*3;
return Offset;
}
void CClockCtl::CalulateDisplayType(int Num,int& OutType,int& OutId)
{
if(Num >= 0 && Num< 8)
{
OutType = 1;
}
else if(Num >= 8 && Num<= 15)
{
OutType = 2;
}
else if(Num >15&& Num<= 22)
{
OutType = 3;
}
else if(Num >22&& Num<= 30)
{
OutType = 4;
}
else if(Num >30&& Num<= 37)
{
OutType = 5;
}
else if(Num >37&& Num<= 45)
{
OutType = 6;
}
else if(Num >45&& Num<= 52)
{
OutType = 7;
}
else if(Num >52&& Num< 60)
{
OutType = 8;
}
switch(OutType)
{
case 1:
OutId = Num;
break;
case 2:
OutId = 7-(Num - 8);
break;
case 3:
OutId = Num - 15;
break;
case 4:
OutId = 7-(Num - 23);
break;
case 5:
OutId = Num - 30;
break;
case 6:
OutId = 7-(Num - 38);
break;
case 7:
OutId = Num - 45;
break;
case 8:
OutId = 7-(Num - 53);
break;
}
TRACE3("Num = %d,Type=%d,ID=%d\n",Num,OutType,OutId);
}
void CClockCtl::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
//Only For Test
// if (m_Second >= 59)
// {
// m_Second = 0;
// if (m_Minute >= 59)
// {
// m_Minute = 0;
// if (m_Hour >= 11)
// m_Hour =0;
// else m_Hour++;
//
// }
// else
// m_Minute++;
// }
// else
// m_Second++;
//For Real Display
GetLocalTime(&RealTime);
if (RealTime.wHour>11)
{
m_Hour = RealTime.wHour-12;
DayOrAfter = FALSE;
}
else
{
m_Hour = RealTime.wHour;
DayOrAfter = TRUE;
}
m_Minute = RealTime.wMinute;
m_Second = RealTime.wSecond;
this->Invalidate();
CWnd::OnTimer(nIDEvent);
}
void CClockCtl::DrawPointer(CDC* pdc,UINT nBitmapID,int nTime)
{
//paint second
CBitmap bitmapPointer;
bitmapPointer.LoadBitmap(nBitmapID);
CDC dcPointer;
dcPointer.CreateCompatibleDC(pdc);
CBitmap* pOldBitmap = dcPointer.SelectObject(&bitmapPointer);
CPoint pos;
//paint second
CPoint posFrom;
// CPoint posColFrom; //add by Jade for color of second pointer
int width,height;
//caculate type
int Type,Id;
CalulateDisplayType(nTime,Type,Id);
int i = Id;
//begin paint
posFrom.x = rtSecond[i].left;
posFrom.y = rtSecond[i].top;
width = rtSecond[i].right - rtSecond[i].left;
height = rtSecond[i].bottom - rtSecond[i].top;
//for rotate
HDC hdcDes;
HBITMAP hBitmap;
hdcDes = CreateCompatibleDC(pdc->m_hDC);
hBitmap = CreateCompatibleBitmap(pdc->m_hDC,height,width);
// HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcDes,hBitmap);
SelectObject(hdcDes,hBitmap);
CPoint point(posFrom.x,posFrom.y);
CSize size(width,height);
// RotateBitblt(pdc,dcPointer.m_hDC,hdcDes,hBitmap,size,point);
RotateBitblt(pdc,dcPointer.m_hDC,hdcDes,size,point);
switch(Type)
{
case 2:
//Type1
pos.x = Center.x - 12;
pos.y = Center.y + (ptSecond[i].y - rtSecond[i].top);
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,width,-height,dcPointer.m_hDC,posFrom.x,posFrom.y,width,height,RGB(8,83,182));
break;
case 3:
//Type2
pos.x = Center.x - 12;
pos.y = Center.y - (ptSecond[i].y - rtSecond[i].top);
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,width,height,dcPointer.m_hDC,posFrom.x,posFrom.y,width,height,RGB(8,83,182));
break;
case 6:
//Type3
pos.x = Center.x + 12;
pos.y = Center.y - (ptSecond[i].y - rtSecond[i].top);
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,-width,height,dcPointer.m_hDC,posFrom.x,posFrom.y,width,height,RGB(8,83,182));
break;
case 7:
//Type4
pos.x = Center.x + 12;
pos.y = Center.y + (ptSecond[i].y - rtSecond[i].top);
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,-width,-height,dcPointer.m_hDC,posFrom.x,posFrom.y,width,height,RGB(8,83,182));
break;
case 5:
//rotate type1
pos.y = Center.y - 12;
pos.x = Center.x - (height -(ptSecond[i].y - rtSecond[i].top));
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,height,width,hdcDes,0,0,height,width,RGB(8,83,182));
break;
case 8:
//rotate type2
pos.y = Center.y + 12;
pos.x = Center.x - (height -(ptSecond[i].y - rtSecond[i].top));
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,height,-width,hdcDes,0,0,height,width,RGB(8,83,182));
break;
case 4:
//rotate type3
pos.y = Center.y - 12;
pos.x = Center.x + (height -(ptSecond[i].y - rtSecond[i].top));
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,-height,width,hdcDes,0,0,height,width,RGB(8,83,182));
break;
case 1:
//rotate type4
pos.y = Center.y + 12;
pos.x = Center.x + (height -(ptSecond[i].y - rtSecond[i].top));
::TransparentBlt(m_MemDC.m_hDC,pos.x,pos.y,-height,-width,hdcDes,0,0,height,width,RGB(8,83,182));
break;
}
//end paint delete rotate DC
// #ifdef JACK_DEFINE
// dcSecond.SelectObject(pOldBitmap);
// hBitmap = (HBITMAP)SelectObject(hdcDes,hBitmapOld);
// // DeleteObject(hBitmapOld);
// #else
// SelectObject(hdcDes,hBitmapOld);
// #endif
dcPointer.SelectObject(pOldBitmap);
dcPointer.DeleteDC();
DeleteDC(hdcDes);
DeleteObject(hBitmap);
bitmapPointer.DeleteObject();
}
void CClockCtl::DrawHalfDayStatus(/*CDC* pdc,*/BOOL half_DayStatus)
{
// CDC dcStatus;
// dcStatus.CreateCompatibleDC(pdc);
// CBitmap Status_Bitmap;
// Status_Bitmap.CreateCompatibleBitmap(pdc,70,50);
// dcStatus.SelectObject(&Status_Bitmap);
CString sz_String;
if (half_DayStatus)
{
sz_String = _T("AM");
}
else
{
sz_String = _T("PM");
}
CRect rect_Text;
rect_Text.left = 110;
rect_Text.top = 139;
rect_Text.right = 170;
rect_Text.bottom = 189;
CFont font;
VERIFY(font.CreateFont(
20, //nHeight
0, //nWidth
0, //nEscapement
0, //nOrientation
FW_NORMAL, //nWeight
FALSE, //bItalic
FALSE, //bUnderline
0, //cstikeOut
ANSI_CHARSET, //nCharSet
OUT_DEFAULT_PRECIS, //nOutPrecision
CLIP_DEFAULT_PRECIS, //nClipPrecision
DEFAULT_QUALITY, //nQuality
DEFAULT_PITCH|FF_SWISS, //nPitchAndFamily
_T("Arial"))); //lpszFacename
CFont* def_font=m_MemDC.SelectObject(&font);
// dcStatus.SetBkMode(TRANSPARENT);
// dcStatus.SetTextColor(RGB(255,255,255));
// dcStatus.DrawText(sz_String,&rect_Text,DT_LEFT);
// m_MemDC.BitBlt(100,139,70,50,&dcStatus,0,0,SRCCOPY);
m_MemDC.SetBkMode(TRANSPARENT);
m_MemDC.SetTextColor(RGB(255,255,255));
m_MemDC.DrawText(sz_String,&rect_Text,DT_LEFT);
m_MemDC.SelectObject(def_font); //need to test for tomorrow
// dcStatus.DeleteDC();
font.DeleteObject();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -