逐梦旅程---实现画刷、位图绘制、文本显示

画刷的实现

//---------------------全局变量---------------------
HDC g_hdc = NULL;    //全局设备环境句柄
HPEN g_hPen[7] = { 0 };    //画笔句柄
HBRUSH g_hBrush[7] = { 0 };    //画刷句柄
int g_iPenStyle[7] = { PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME };    //画笔样式数组
int g_iBrushStyle[6] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL};
//---------------------Game_Init()函数---------------------
//描述:初始化函数 进行简单的初始化
BOOL Game_Init(HWND hwnd)
{
    g_hdc = GetDC(hwnd);
    srand((unsigned)time(NULL)); //初始化时间种子

    //随机初始化画笔和笔刷的颜色
    //i=6是画刷生成实心 否则生成阴影
    //画笔在样式数组中生成对应下标的样式 颜色随机
    for (int i = 0; i <= 6; i++) {
        g_hPen[i] = CreatePen(g_iPenStyle[i], 1, RGB(rand() % 256, rand() % 256, rand() % 256));
        if (i == 6)
            g_hBrush[i] = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256));
        else
            g_hBrush[i] = CreateHatchBrush(g_iBrushStyle[i], RGB(rand() % 256, rand() % 256, rand() % 256));
    }

    Game_Paint(hwnd);
    ReleaseDC(hwnd, g_hdc);
    return true;
}

//---------------------Game_CleanUp()函数---------------------
//描述:绘制函数 在这个函数中进行绘制操作
VOID Game_Paint(HWND hWnd)
{
    //y坐标
    int y = 0;

    //使用7种不同的画笔绘制
    for (int i = 0; i < 7; i++) {
        y = (i + 1) * 70;
        SelectObject(g_hdc, g_hPen[i]);        //选择画笔
        MoveToEx(g_hdc, 30, y, NULL);        //移动光标至(30,y)
        LineTo(g_hdc, 100, y);                //从(30,y)到(100,y)
    }
    int x1 = 120;
    int x2 = 190;

    for (int i = 0; i < 7; i++) {
        SelectObject(g_hdc, g_hBrush[i]);        //选择画刷
        Rectangle(g_hdc, x1, 70,x2,y);        //画矩形 左上角:(x1,70) 右下角(x2,y)
        x1 += 90;    //调整x1的值
        x2 += 90;    //调整x2的值
    }
}

//---------------------Game_CleanUp()函数---------------------
//描述:资源清理函数 在这个函数中进行退出窗口前的资源清理
BOOL Game_CleanUp(HWND hWnd)
{
    for (int i = 0; i < 7; i++) {
        DeleteObject(g_hPen[i]);
        DeleteObject(g_hBrush[i]);
    }
    return true;
}

文字显示

//---------------------Game_Init()函数---------------------
//描述:初始化函数 进行简单的初始化
BOOL Game_Init(HWND hwnd)
{
    g_hdc = GetDC(hwnd);
    Game_Paint(hwnd);
    ReleaseDC(hwnd, g_hdc);
    return true;
}

//---------------------Game_CleanUp()函数---------------------
//描述:绘制函数 在这个函数中进行绘制操作
VOID Game_Paint(HWND hWnd)
{
    HFONT hFont = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, L"微软雅黑");    //字体样式
    SelectObject(g_hdc, hFont);    //选择字体
    SetBkMode(g_hdc, TRANSPARENT);    //设置文字背景色为透明

    wchar_t text1[] = L"Good Night";
    wchar_t text2[] = L"My Dear";
    wchar_t text3[] = L"Alice";
    wchar_t text4[] = L"key / kei";

    /*
        TextOut(
            HDC hdc,
            int nXstart,    //开始的x轴位置
            int nYstart,    //开始的y轴位置
            LPCTSTR lpString,    //输出的字符串指针
            int cbString        //字符数
        )
    */


    SetTextColor(g_hdc, RGB(50, 255, 50));
    TextOut(g_hdc, 30, 150, text1, wcslen(text1));

    SetTextColor(g_hdc, RGB(255, 150, 50));
    TextOut(g_hdc, 30, 200, text2, wcslen(text2));

    SetTextColor(g_hdc, RGB(50, 50, 255));
    TextOut(g_hdc, 30, 250, text3, wcslen(text3));

    SetTextColor(g_hdc, RGB(255, 0, 0));
    TextOut(g_hdc, 200, 300, text4, wcslen(text4));

    //释放字体对象
    DeleteObject(hFont);
}

//---------------------Game_CleanUp()函数---------------------
//描述:资源清理函数 在这个函数中进行退出窗口前的资源清理
BOOL Game_CleanUp(HWND hWnd)
{
    return true;
}

位图

//---------------------全局变量---------------------
HDC g_hdc = NULL, g_mdc = NULL;    //全局设备环境句柄
HBITMAP g_hBitmap = NULL;    //位图句柄

//---------------------Game_Init()函数---------------------
//描述:初始化函数 进行简单的初始化
BOOL Game_Init(HWND hwnd)
{
    g_hdc = GetDC(hwnd);

    //位图绘制1.0    加载位图
    g_hBitmap = (HBITMAP)LoadImage(NULL, L"Naruto.bmp", IMAGE_BITMAP, 800, 600, LR_LOADFROMFILE);
    //位图绘制2.0   建立兼容DC 用于缓存下一个显示的位图
    g_mdc = CreateCompatibleDC(g_hdc);

    Game_Paint(hwnd);

    //释放设备环境
    ReleaseDC(hwnd, g_hdc);
    return true;
}

//---------------------Game_CleanUp()函数---------------------
//描述:绘制函数 在这个函数中进行绘制操作
VOID Game_Paint(HWND hWnd)
{
    //位图绘制3.0 选用位图对象
    SelectObject(g_mdc, g_hBitmap);
    //位图绘制4.0 贴图  从源句柄(g_mdc)中复复制到目标句柄(g_hdc)
    BitBlt(g_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, g_mdc, 0, 0, SRCCOPY);

    //文本字体样式
    HFONT hFont = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, L"微软雅黑");
    SelectObject(g_hdc, hFont);
    SetBkMode(g_hdc, TRANSPARENT);

    wchar_t text1[] = L"Good Night";
    wchar_t text2[] = L"My Dear";
    wchar_t text3[] = L"Alice";

    SetTextColor(g_hdc,RGB(153,204,255));
    TextOut(g_hdc, 30, 300, text1, wcslen(text1));
    TextOut(g_hdc, 30, 350, text2, wcslen(text2));
    TextOut(g_hdc, 30, 400, text3, wcslen(text3));

    DeleteObject(hFont);
}

//---------------------Game_CleanUp()函数---------------------
//描述:资源清理函数 在这个函数中进行退出窗口前的资源清理
BOOL Game_CleanUp(HWND hWnd)
{

    DeleteObject(g_hBitmap);
    DeleteDC(g_mdc);

    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值