PC版C++画图程序代码

正确例题

#include <graphics.h>
#include <stdio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, “”);

// 绘制矩形
rectangle(100, 100, 300, 200);

// 绘制圆形
circle(400, 250, 100);

getch();
closegraph();
return 0;

}

正确例题
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
HWND hWnd;
MSG msg;

// 注册窗口类
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(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "MyWindowClass";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wcex))
{
    MessageBox(NULL, "注册窗口类失败!", "错误", MB_ICONERROR);
    return 1;
}

// 创建窗口
hWnd = CreateWindow(
    "MyWindowClass",
    "GDI 画图示例",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    800, 600,
    NULL,
    NULL,
    hInstance,
    NULL
);

if (!hWnd)
{
    MessageBox(NULL, "创建窗口失败!", "错误", MB_ICONERROR);
    return 1;
}

// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// 消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return (int)msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    // 绘制一个红色矩形
    Rectangle(hdc, 100, 100, 300, 200);
    SetDCPenColor(hdc, RGB(255, 0, 0));

    // 绘制一个蓝色圆形
    Ellipse(hdc, 400, 300, 600, 500);
    SetDCPenColor(hdc, RGB(0, 0, 255));

    EndPaint(hWnd, &ps);
    break;

case WM_DESTROY:
    PostQuitMessage(0);
    break;

default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;

}

正确例题
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
HWND hWnd;
MSG msg;

// 注册窗口类
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(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "MyWindowClass";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wcex))
{
    MessageBox(NULL, "注册窗口类失败!", "错误", MB_ICONERROR);
    return 1;
}

// 创建窗口
hWnd = CreateWindow(
    "MyWindowClass",
    "GDI 画图示例",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    800, 600,
    NULL,
    NULL,
    hInstance,
    NULL
);

if (!hWnd)
{
    MessageBox(NULL, "创建窗口失败!", "错误", MB_ICONERROR);
    return 1;
}

// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// 消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return (int)msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    // 绘制一个红色矩形
    Rectangle(hdc, 100, 100, 300, 200);
    SetDCPenColor(hdc, RGB(255, 0, 0));

    // 绘制一个蓝色圆形
    Ellipse(hdc, 400, 300, 600, 500);
    SetDCPenColor(hdc, RGB(0, 0, 255));

    // 绘制一条绿色直线
    MoveToEx(hdc, 10, 400, NULL);
    LineTo(hdc, 200, 400);
    SetDCPenColor(hdc, RGB(0, 255, 0));

    // 绘制一个黄色三角形(通过三条线组成)
    MoveToEx(hdc, 500, 100, NULL);
    LineTo(hdc, 600, 200);
    LineTo(hdc, 400, 200);
    LineTo(hdc, 500, 100);
    SetDCPenColor(hdc, RGB(255, 255, 0));

    EndPaint(hWnd, &ps);
    break;

case WM_DESTROY:
    PostQuitMessage(0);
    break;

default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;

}

正确例题
#include<graphics.h>
using namespace std;
int main(){
initgraph(600,600);
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
fillellipse(300,300,100,100);
getch();
closegraph();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EasySoft易软

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值