虽说微软现在急着想要把GDI+换成Direct2D,但是实际上GDI+还是简单易用易上手。。。
引用
评论
#pragma comment(lib, "gdiplus.lib")
#include <windows.h>
#include <gdiplus.h>
#include <math.h>
#include <atlbase.h>
#include <atlwin.h>
class CMainWnd : public CWindowImpl<cmainwnd, cwindow, cframewintraits>
{
BEGIN_MSG_MAP(CMainWnd)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()
LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(&ps);
DrawAxises(hDC);
DrawCurve(hDC);
EndPaint(&ps);
return 0;
}
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 0;
}
void DrawAxises(HDC hDC)
{
using namespace Gdiplus;
Graphics g(hDC);
Pen blue(Color(255, 0, 0, 255), 1);
PointF x_axis[] = { PointF(10, 155), PointF(310, 155),
PointF(305, 150), PointF(305, 160), PointF(310, 155) };
g.DrawLines(&blue, x_axis, 5);
PointF y_axis[] = { PointF(155, 310), PointF(155, 10),
PointF(150, 15), PointF(160, 15), PointF(155, 10) };
g.DrawLines(&blue, y_axis, 5);
}
static const int curve_points = 281;
void DrawCurve(HDC hDC)
{
using namespace Gdiplus;
Graphics g(hDC);
Pen red(Color(255, 255, 0, 0), 1);
PointF curve[curve_points];
for (int j = 0; j < curve_points; j++)
{
double x = j - curve_points / 2;
double y = - sin(x / 10) * 50;
curve[j] = PointF(x + 155, y + 155);
}
g.DrawCurve(&red, curve, curve_points);
}
};
int __stdcall wWinMain(HINSTANCE hinst, HINSTANCE hpi, wchar_t *cl, int ns)
{
ULONG_PTR gdiplus_token;
Gdiplus::GdiplusStartup(&gdiplus_token, &Gdiplus::GdiplusStartupInput(), NULL);
CMainWnd wnd;
wnd.Create(NULL, NULL, L"GDI+ Sine");
wnd.ShowWindow(ns);
wnd.UpdateWindow();
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Gdiplus::GdiplusShutdown(gdiplus_token);
return (int)msg.wParam;
}</cmainwnd,></atlwin.h></atlbase.h></math.h></gdiplus.h></windows.h>
#pragma comment(lib, "gdiplus.lib")
#include <windows.h>
#include <gdiplus.h>
#include <math.h>
HWND hMainWnd;
void DrawAxises(HDC hDC)
{
using namespace Gdiplus;
Graphics g(hDC);
Pen blue(Color(255, 0, 0, 255), 1);
PointF x_axis[] = { PointF(10, 155), PointF(310, 155),
PointF(305, 150), PointF(305, 160), PointF(310, 155) };
g.DrawLines(&blue, x_axis, 5);
PointF y_axis[] = { PointF(155, 310), PointF(155, 10),
PointF(150, 15), PointF(160, 15), PointF(155, 10) };
g.DrawLines(&blue, y_axis, 5);
}
static const int curve_points = 281;
void DrawCurve(HDC hDC)
{
using namespace Gdiplus;
Graphics g(hDC);
Pen red(Color(255, 255, 0, 0), 1);
PointF curve[curve_points];
for (int j = 0; j < curve_points; j++)
{
double x = j - curve_points / 2;
double y = - sin(x / 10) * 50;
curve[j] = PointF(x + 155, y + 155);
}
g.DrawCurve(&red, curve, curve_points);
}
LRESULT __stdcall WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
PAINTSTRUCT ps;
HDC hDC;
switch (msg)
{
case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
DrawAxises(hDC);
DrawCurve(hDC);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wp, lp);
}
int __stdcall wWinMain(HINSTANCE hinst, HINSTANCE hpi, wchar_t *cl, int ns)
{
ULONG_PTR gdiplus_token;
Gdiplus::GdiplusStartup(&gdiplus_token, &Gdiplus::GdiplusStartupInput(), NULL);
WNDCLASSEX wc = {
sizeof wc,
CS_VREDRAW|CS_HREDRAW,
WndProc,
0, 0,
NULL,
LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
(HBRUSH)GetStockObject(WHITE_BRUSH),
NULL,
L"MyWndClass",
LoadIcon(hinst, IDI_APPLICATION)
};
if (!RegisterClassEx(&wc))
return 1;
hMainWnd = CreateWindowEx(0, L"MyWndClass", L"GDI+ Sine", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, NULL, NULL);
ShowWindow(hMainWnd, ns);
UpdateWindow(hMainWnd);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Gdiplus::GdiplusShutdown(gdiplus_token);
return (int)msg.wParam;
}</math.h></gdiplus.h></windows.h>