文号
t73955
阅读
4878
回复
3
收藏
1
【源码】GDI+画正弦
2015/10/12
IP:四川
ATL版:
#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>

WinSDK版:
#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>
acmilan作者
11年2个月前IP:四川
793273
1楼
虽说微软现在急着想要把GDI+换成Direct2D,但是实际上GDI+还是简单易用易上手。。。
引用
评论
acmilan作者
11年2个月前IP:四川
793309
2楼
个人感觉GDI+的原理应该是封装了AlphaBlend这个GDI函数→_→
引用
评论
acmilan作者
11年2个月前IP:四川
794380
3楼
GDI+只能在C++和.NET (C#/VB)中使用。

如果想在C或VB6中使用GDI+,需要调用GDI+ Flat API,但是这些GDI+ Flat API是没有文档的。

C或VB6中可以调用Windows GDI或Direct2D绘图,但是GDI比较简陋,而Direct2D可能会比较麻烦。
引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的