这是用Windows API画的,但只是简单画一下,可以自己加一些功能进去。
程序代码:
/*
* FileName: App.cpp
* Author: ZhouFeng
* Date: 2011/04/05
*/
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
TCHAR szAppName[] = TEXT("pie");
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(hInstance, IDC_ARROW);
wndclass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szAppName;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("此程序要求在Windows NT 下运行"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("画饼图"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_NORMAL);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.lParam;
}
#define RADIUS 100 // 圆半径
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
static RECT rectClient;
static TEXTMETRIC tm;
static int cxEllipse = 200, cyEllipse = 200;
static HBRUSH hbrushRed, hbrushBlue;
static HPEN hPen;
POINT pntTmp;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
ReleaseDC(hwnd, hdc);
hbrushRed = CreateSolidBrush(RGB(255, 0, 0));
hbrushBlue = CreateSolidBrush(RGB(0, 0, 255));
hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
return 0;
case WM_SIZE:
GetClientRect(hwnd, &rectClient);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, hPen);
// 画椭圆
SelectObject(hdc, hbrushBlue);
Pie(hdc, cxEllipse, cyEllipse, cxEllipse + RADIUS, cyEllipse + RADIUS, cxEllipse, cyEllipse + RADIUS / 2,
cxEllipse + RADIUS / 2, cyEllipse);
//Ellipse(hdc, cxEllipse, cyEllipse, cxEllipse + RADIUS - 10, cyEllipse + RADIUS -10);
SelectObject(hdc, hbrushRed);
Pie(hdc, cxEllipse, cyEllipse, cxEllipse + RADIUS - 10, cyEllipse + RADIUS -10, cxEllipse + RADIUS / 2 - 5, cyEllipse,
cxEllipse, cyEllipse + RADIUS / 2 - 5);
MoveToEx(hdc, cxEllipse + RADIUS / 2 + 20, cyEllipse + RADIUS / 2, &pntTmp);
LineTo(hdc, cxEllipse + RADIUS / 2 + 120, cyEllipse + RADIUS / 2 + 100);
TextOut(hdc, cxEllipse + RADIUS / 2 + 120, cyEllipse + RADIUS / 2 + 100, "75%", 3);
MoveToEx(hdc, cxEllipse + 25, cyEllipse + 25, &pntTmp);
LineTo(hdc, cxEllipse - 65, cyEllipse - 65);
TextOut(hdc, cxEllipse - 65, cyEllipse - 65, "25%", 3);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
DeleteObject(hbrushRed);
DeleteObject(hbrushBlue);
DeleteObject(hPen);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
再VC下创建一个空的WIN32项目,编译即可。