(20分)我想在vs2005中,用c语言画一条直线或其它图形,应该怎么办

2024-11-30 06:19:41
推荐回答(1个)
回答1:

用纯C比较麻烦,用WIN32程序就比较简单了:

#include "stdafx.h"

//�办跑计��
HINSTANCE hInst;
HPEN hPen[7];
HBRUSH hBru[7];
int sPen[7] = {PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};
int sBru[6] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL};

//ㄧΑ��
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);

//****�祘Α**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

//磅《�﹍てㄧΑ
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

//瘪�癹伴
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

//****)竡の爹�跌怠摸�ㄧΑ*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;

return RegisterClassEx(&wcex);
}

//****�﹍ㄧΑ*************************************
// �ミ 7 贺ぃ��礶掸の礶��ン
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HDC hdc;
int i;

hInst = hInstance;

hWnd = CreateWindow("canvas", "酶瓜跌怠" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,10,10,650,350,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

for(i=0;i<=6;i++)
{
hPen[i] = CreatePen(sPen[i],1,RGB(255,0,0));
if(i==6)
hBru[i] = CreateSolidBrush(RGB(0,255,0));
else
hBru[i] = CreateHatchBrush(sBru[i],RGB(0,255,0));
}

hdc = GetDC(hWnd);
MyPaint(hdc);
ReleaseDC(hWnd,hdc);

return TRUE;
}

//****�璹酶瓜ㄧΑ*********************************
// ��Α礶掸の礶�酶籹絬兵籔恶�痻�
void MyPaint(HDC hdc)
{
int i,x1,x2,y;

//� 7 贺ぃ�礶掸酶籹絬兵
for(i=0;i<=6;i++)
{
y = (i+1) * 30;

SelectObject(hdc,hPen[i]);
MoveToEx(hdc,30,y,NULL); //簿笆絬兵癣翴
LineTo(hdc,100,y); //礶絬
}

x1 = 120;
x2 = 180;

//� 7 贺ぃ�礶�恶�痻�
for(i=0;i<=6;i++)
{
SelectObject(hdc,hBru[i]);
Rectangle(hdc,x1,30,x2,y); //礶�超痻�
x1 += 70;
x2 += 70;
}
}
//****瘪�矪瞶ㄧΑ***********************************
// 1.跌怠�酶瘪�只ネ�㊣�MyPaint()
// 2.跌怠挡�瘪�只ネ��埃GDI�ン
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
int i;

switch (message)
{
case WM_PAINT: //跌怠�酶瘪�
hdc = BeginPaint(hWnd, &ps);
MyPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY: //跌怠挡�瘪�
for(i=0;i<=6;i++)
{
DeleteObject(hPen[i]); //�埃礶掸
DeleteObject(hBru[i]); //�埃礶�
}
PostQuitMessage(0);
break;
default: //ㄤウ瘪�
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}