本文实例讲述了C++实现图形界面时钟表盘代码,分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:
//POINT的数组可以这么用
POINT pt[]={
0, 450,
225,390,
390,225,
450,0,
390,-225,
225,-390,
0,-450,
-225,-390,
-390,-225,
-450,0,
-390,225,
-225,390
};
POINT pt[]={
0, 450,
225,390,
390,225,
450,0,
390,-225,
225,-390,
0,-450,
-225,-390,
-390,-225,
-450,0,
-390,225,
-225,390
};
SetIsotropic函数:设置坐标系
//改变坐标系就用这四个函数:
void SetIsotropic(HDC hdc, int cx, int cy){
::SetMapMode(hdc, MM_ISOTROPIC); //设置坐标映射方式
::SetWindowExtEx(hdc, 1000, 1000, NULL); //设置坐标系的逻辑单位
::SetViewportExtEx(hdc, cx, -cy, NULL); //设置坐标系方向和坐标系包含的范围,即定义域和值域
::SetViewportOrgEx(hdc, cx/2, cy/2, NULL); //设置坐标系原点坐标}
复制代码 代码如下:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
//画钟表盘
int cxClient, cyClient;
RECT rect;
::GetClientRect(hWnd, &rect);
cxClient = rect.right - rect.left;
cyClient = rect.bottom - rect.top;
SetIsotropic(hdc, cxClient, cyClient);
#define SQUARESIZE 10
::SelectObject(hdc, ::GetStockObject(BLACK_BRUSH));
for(int i=0;i<12;i++)
{
::Ellipse(hdc, pt[i].x-SQUARESIZE,pt[i].y+SQUARESIZE,pt[i].x+SQUARESIZE, pt[i].y-SQUARESIZE);
}
EndPaint(hWnd, &ps);
break;
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
//画钟表盘
int cxClient, cyClient;
RECT rect;
::GetClientRect(hWnd, &rect);
cxClient = rect.right - rect.left;
cyClient = rect.bottom - rect.top;
SetIsotropic(hdc, cxClient, cyClient);
#define SQUARESIZE 10
::SelectObject(hdc, ::GetStockObject(BLACK_BRUSH));
for(int i=0;i<12;i++)
{
::Ellipse(hdc, pt[i].x-SQUARESIZE,pt[i].y+SQUARESIZE,pt[i].x+SQUARESIZE, pt[i].y-SQUARESIZE);
}
EndPaint(hWnd, &ps);
break;
希望本文所述对大家的C++程序设计有所帮助。