正确例题
#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;
}