实验一:绘图与文本操作

一、实验目的和要求

1、掌握Windows应用程序的基本结构;

2、使用API函数建立窗口、消息循环,编制窗口函数;

3、使用API函数在用户区绘图、输出文本,理解Windows的图形设备接口。

二、实验内容 :

实现下图给定的绘图与文

本操作。

// 2019339901078_李海龙__实验壹.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "2019339901078_李海龙__实验壹.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY2019339901078, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY2019339901078));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY2019339901078));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_MY2019339901078);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
           // PAINTSTRUCT ps;
          //  HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
        PAINTSTRUCT ps;
        HDC hDC = BeginPaint(hWnd, &ps);
        // TODO: 在此处添加使用 hdc 的任何绘图代码...
        HPEN hPen = (HPEN)GetStockObject(NULL_PEN); //获取系统定义的空画笔
        SelectObject(hDC, hPen);    	//选择画笔
        HBRUSH hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH); //获取系统定义的画刷
        SelectObject(hDC, hBrush);  	//选择画刷
        LineTo(hDC, 50, 50);  		//画线
        DeleteObject(hPen); 		//删除画笔
        hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); //创建画笔
        SelectObject(hDC, hPen);  	//选择画笔
        //画一个三角形
        LineTo(hDC, 150, 50);
        LineTo(hDC, 100, 137);
        LineTo(hDC, 50, 50);
        //定义一个POINT数组,包括6个点
        POINT points[6] = { {100,212},{70,227},{70,250},{130,250},{130,227},{100,212} };
        Polyline(hDC, points, 6);   		 	//画一个五边形
        Arc(hDC, 63, 137, 138, 212, 100, 137, 100, 137); 	//画一个圆
        Pie(hDC, 213, 137, 288, 212, 240, 137, 260, 137); 	//画一个圆饼
        Rectangle(hDC, 213, 212, 287, 250);           	//画一个长方形
        RoundRect(hDC, 213, 100, 287, 137, 20, 20);  //画一个圆角长方形
        DeleteObject(hPen);    //删除画笔
        DeleteObject(hBrush);  //删除画刷
        static long nXChar, nCaps, nYChar;
        int pointx, pointy;
        int i, j;
        static LPCWSTR textbuf[4] = { {_T("故人西辞黄鹤楼")},
                                        {_T("烟花三月下扬州")},
                                        {_T("孤帆远影碧空尽")},
                                        {_T("唯见长江天际流")} };
        TEXTMETRIC tm;
        GetTextMetrics(hDC, &tm); 			//获取字体信息
        nXChar = tm.tmAveCharWidth;  			//获取字符宽度
        nYChar = tm.tmHeight + tm.tmExternalLeading;	//字符高度
        nCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * nXChar / 2;	//字间距

        for (i = 4; i > 0; i--)
        {
            for (j = 0; j < 7; j++)				//输出文本
            {
                pointx = 300 + i * nXChar * 5;
                pointy = 50 + j * (nYChar + nCaps);
                TextOut(hDC, pointx, pointy, textbuf[4 - i] + j * 1, 1);
            }
        }
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值