【源码】GDI+画正弦
acmilan2015/10/12软件综合 IP:四川
ATL版:
<code class="lang-cpp">#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></code>

WinSDK版:
<code class="lang-cpp">#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></code>
来自:计算机科学 / 软件综合
3
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
acmilan 作者
8年8个月前 IP:四川
793273
虽说微软现在急着想要把GDI+换成Direct2D,但是实际上GDI+还是简单易用易上手。。。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
8年8个月前 IP:四川
793309
个人感觉GDI+的原理应该是封装了AlphaBlend这个GDI函数→_→
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
8年7个月前 IP:四川
794380
GDI+只能在C++和.NET (C#/VB)中使用。

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

C或VB6中可以调用Windows GDI或Direct2D绘图,但是GDI比较简陋,而Direct2D可能会比较麻烦。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年3个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}