简单画图程序(windows程序设计)

1 ) 按 “ L ” 后 , 用 鼠 标 拖 拽 能 在 屏 幕 上 画 直 线 ; 1)按“L”后,用鼠标拖拽能在屏幕上画直线; 1L线
2 ) 按 “ E ” 后 , 用 鼠 标 托 拽 能 在 屏 幕 上 画 矩 形 2)按“E”后,用鼠标托拽能在屏幕上画矩形 2E,
3 ) 按 “ C ” 后 , 用 鼠 标 拖 拽 能 在 屏 幕 上 画 圆 3)按“C”后,用鼠标拖拽能在屏幕上画圆 3C
按 “ R ” “ G ” “ B ” 能 够 改 变 所 画 图 形 为 红 、 绿 、 蓝 色 按“R”“G”“B”能够改变所画图形为红、绿、蓝色 RGB绿

效果:

简单画图程序(windows程序设计)_第1张图片
简单画图程序(windows程序设计)_第2张图片
简单画图程序(windows程序设计)_第3张图片

code

/*SiberianSquirrel*/
/*CuteKiloFish*/
#include "framework.h"
#include "ex4_5.0.h"
#include
#include
#include
#include
#include
#include

using namespace std;
#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);

double dis(POINT a, POINT b) {
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

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

    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_EX450, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }

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

    MSG msg;

    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int)msg.wParam;
}

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_EX450));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_EX450);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

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;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    HPEN hp = CreatePen(BS_SOLID, 5, RGB(0, 0, 0));
    HBRUSH hbrush = CreateSolidBrush(RGB(255, 255, 255));
    HCURSOR hCursor;
    static BOOL nLKeyDown = FALSE, nEKeyDown = FALSE, nCKeyDown = FALSE;
    static BOOL nRKeyDown = FALSE, nGKeyDown = FALSE, nBKeyDown = FALSE;
    static BOOL nLButtonKeyDown = FALSE, nEButtonKeyDown = FALSE, nCButtonKeyDown = FALSE;
    static bool isLButtonDown = FALSE;
    static RECT rect;
    GetClientRect(hwnd, &rect);
    static POINT LeftUp = { 0, 0 }, RightDown = { 0, 0 }, point = { rect.left, rect.top }, org = { 0, 0 };
    char str[50];
    static int x, y, op = 0;
    static double R = 0;
    
    switch (message)
    {
    case WM_KEYDOWN:
    {
        switch (wParam)
        {
        case 'L':
        case 'l':
            nLKeyDown = TRUE;
            break;
        case 'E':
        case 'e':
            nEKeyDown = TRUE;
            break;
        case 'C':
        case 'c':
            nCKeyDown = TRUE;
            break;
        case 'R':
        case 'r':
            nRKeyDown = TRUE;
            nGKeyDown = nBKeyDown = FALSE;
            break;
        case 'G':
        case 'g':
            nGKeyDown = TRUE;
            nRKeyDown = nBKeyDown = FALSE;
            break;
        case 'B':
        case 'b':
            nBKeyDown = TRUE;
            nRKeyDown = nGKeyDown = FALSE;
            break;
        default:
            break;
        }
    }
    break;
    case WM_KEYUP:
    {
        InvalidateRect(hwnd, NULL, TRUE);
        nLKeyDown = nLButtonKeyDown = FALSE;
        nEKeyDown = nEButtonKeyDown = FALSE;
        nCKeyDown = nCButtonKeyDown = FALSE;
    }
    break;
    case WM_LBUTTONDOWN:
    {
        InvalidateRect(hwnd, NULL, TRUE);
        UpdateWindow(hwnd);
        op = 0;
        nRKeyDown = nGKeyDown = nBKeyDown = FALSE;
        if (!isLButtonDown) {
            if (nLKeyDown) {
                op = 1;
                nLButtonKeyDown = TRUE;
                LeftUp = RightDown = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
            }
            else if (nEKeyDown) {
                op = 2;
                nEButtonKeyDown = TRUE;
                LeftUp = RightDown = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
            }
            else if (nCKeyDown) {
                op = 3;
                nCButtonKeyDown = TRUE;
                R = 0;
                org = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
            }
        }
        isLButtonDown = TRUE;
    }
    break;
    case WM_LBUTTONUP:
    {
        if (nLButtonKeyDown) {
            RightDown = { GET_X_LPARAM(lParam) , GET_Y_LPARAM(lParam) };
            nLButtonKeyDown = FALSE;
        }
        else if (nEButtonKeyDown) {
            RightDown = { GET_X_LPARAM(lParam) , GET_Y_LPARAM(lParam) };
            nEButtonKeyDown = FALSE;
        }
        else if (nCButtonKeyDown) {
            RightDown = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
            R = dis(org, RightDown);
            nCButtonKeyDown = FALSE;
        }
        if (LeftUp.x > RightDown.x || LeftUp.y > RightDown.y) swap(LeftUp, RightDown);

        isLButtonDown = FALSE;
    }
    break;
    case WM_MOUSEMOVE:
    {
        if (isLButtonDown) {
            if (nLButtonKeyDown) {
                op = 1;
                InvalidateRect(hwnd, NULL, TRUE);
                //UpdateWindow(hwnd);
                RightDown = { GET_X_LPARAM(lParam) , GET_Y_LPARAM(lParam) };
            }
            else if (nEButtonKeyDown) {
                op = 2;
                InvalidateRect(hwnd, NULL, TRUE);
                //UpdateWindow(hwnd);
                RightDown = { GET_X_LPARAM(lParam) , GET_Y_LPARAM(lParam) };
            }
            else if (nCButtonKeyDown) {
                op = 3;
                InvalidateRect(hwnd, NULL, TRUE);
                //UpdateWindow(hwnd);
                R = dis(org, POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) });
            }
        }
    }
    break;
    case WM_PAINT:
    {        
        hdc = BeginPaint(hwnd, &ps);
        if (op) {
            sprintf_s(str, "%d,%d\0", x, y);
            TextOut(hdc, 9, 7, str, lstrlen(str));

            if (nRKeyDown) {
                hp = CreatePen(BS_SOLID, 5, RGB(255, 0, 0));
                hbrush = CreateSolidBrush(RGB(255, 0, 0));
                SelectObject(hdc, hp);
                SelectObject(hdc, hbrush);
//                nRKeyDown = FALSE;
            }
            else if (nGKeyDown) {
                hp = CreatePen(BS_SOLID, 5, RGB(0, 255, 0));
                hbrush = CreateSolidBrush(RGB(0, 255, 0));
                SelectObject(hdc, hp);
                SelectObject(hdc, hbrush);
//                nGKeyDown = FALSE;
            }
            else if (nBKeyDown) {
                hp = CreatePen(BS_SOLID, 5, RGB(0, 0, 255));
                hbrush = CreateSolidBrush(RGB(0, 0, 255));
                SelectObject(hdc, hp);
                SelectObject(hdc, hbrush);
//                nBKeyDown = FALSE;
            }

            if (op == 1) {
                hCursor = LoadCursor(NULL, IDC_CROSS);
                SetCursor(hCursor);
                MoveToEx(hdc, LeftUp.x, LeftUp.y, NULL);
                LineTo(hdc, RightDown.x, RightDown.y);
                sprintf_s(str, "%d,%d    %d,%d\0", LeftUp.x, LeftUp.y, RightDown.x, RightDown.y);
                TextOut(hdc, 9, 37, str, lstrlen(str));
            }
            else if (op == 2) {
                hCursor = LoadCursor(NULL, IDC_CROSS);
                SetCursor(hCursor);
                Rectangle(hdc, LeftUp.x, LeftUp.y, RightDown.x, RightDown.y);
                sprintf_s(str, "%d,%d    %d,%d\0", LeftUp.x, LeftUp.y, RightDown.x, RightDown.y);
                TextOut(hdc, 9, 37, str, lstrlen(str));
            }
            else if (op == 3) {
                hCursor = LoadCursor(NULL, IDC_CROSS);
                SetCursor(hCursor);
                Ellipse(hdc, org.x - R, org.y - R, org.x + R, org.y + R);
                sprintf_s(str, "%d,%d    %d,%d\0", org.x, org.y, RightDown.x, RightDown.y);
                TextOut(hdc, 9, 37, str, lstrlen(str));
            }
        }
        else {
            hp = CreatePen(BS_SOLID, 5, RGB(0, 0, 0));
            hbrush = CreateSolidBrush(RGB(255, 255, 255));
            SelectObject(hdc, hp);
            SelectObject(hdc, hbrush);
            hCursor = LoadCursor(NULL, IDC_ARROW);
            SetCursor(hCursor);
        }
        DeleteObject(hbrush);
        DeleteObject(hp);
        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;
}

你可能感兴趣的:(Windows,程序设计)