VC++,谁帮我改成用不同颜色的线画线?
CPenpenRed;//声明画笔对象,并创建宽度为3的红色实线画笔penRed.CreatePen(PS_SOLID,1,RGB(255,0,0));//使用新的画笔,...
CPen penRed;//声明画笔对象,并创建宽度为3的红色实线画笔
penRed.CreatePen(PS_SOLID,1,RGB(255,0,0));
//使用新的画笔,保存原来的画笔以便恢复;
CPen *pOldPen;
pOldPen=pDC->SelectObject(&penRed);
for(t=0; t<=360;t=t++)
{//画出了李萨如图像部分
x=x0-a1*sin(w1*t*PI/180+u1);
y=y0-a2*sin(w2*t*PI/180+u2);
//这里最好用黑色的点把起始点给标志出来
if(t==0)
pDC-> MoveTo(x,y); //确定起始位置,从x0,y0开始
pDC-> LineTo(x,y);
pDC-> MoveTo(x,y);
}
最好能把起点用黑颜色或者一种颜色表示
画好了的用一种颜色,比如红色
正在画的用另外一种颜色,比如蓝色
谢谢
我追加给分,
画过的线颜色不一样也可以,只要能看出不一样和正在划线就可以 展开
penRed.CreatePen(PS_SOLID,1,RGB(255,0,0));
//使用新的画笔,保存原来的画笔以便恢复;
CPen *pOldPen;
pOldPen=pDC->SelectObject(&penRed);
for(t=0; t<=360;t=t++)
{//画出了李萨如图像部分
x=x0-a1*sin(w1*t*PI/180+u1);
y=y0-a2*sin(w2*t*PI/180+u2);
//这里最好用黑色的点把起始点给标志出来
if(t==0)
pDC-> MoveTo(x,y); //确定起始位置,从x0,y0开始
pDC-> LineTo(x,y);
pDC-> MoveTo(x,y);
}
最好能把起点用黑颜色或者一种颜色表示
画好了的用一种颜色,比如红色
正在画的用另外一种颜色,比如蓝色
谢谢
我追加给分,
画过的线颜色不一样也可以,只要能看出不一样和正在划线就可以 展开
2个回答
展开全部
你问的是MFC吧?我没学过,但我想道理差不多,你应该参考一下API绘图的技巧。其实这是个小动画!
以下程序,使用Windows GDI函数随机画直线,当前直线的起点是前一条直线的终点。一开始的起点使用白色表示,为了明显,我特意把它画得大一点。画过的直线使用红色表示。正在画的直线使用蓝色表示。
特别注意:为了让你看得更清楚,我特意延时1秒,换句话说,当你点击窗口右上角的“X”关闭按钮时,要等一段时间(不会太长!)窗口才会关闭,注意!
下面是源程序,要想尝试请创建Win32 Application工程测试。
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN // 跟 MFC 说Bye-Bye
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <time.h>
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
#define START_X 50
#define START_Y 50
// 全局变量 ////////////////////////////////////////////////
HWND main_window_handle = NULL;
HINSTANCE hinstance_app = NULL;
HDC hdc;
HPEN hpen;
int line_x = 0,line_y = 0;
int line_x1 = 0,line_y1 = 0;
char buffer[80];
// 窗口过程 //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
}
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
///////////////////////////////////////////////////////////
int mypcluna_Main(void *parms = NULL, int num_parms = 0)
{
hdc=GetDC(main_window_handle);
if(hpen)
DeleteObject(hpen);
hpen=CreatePen(PS_SOLID,1,RGB(255,0,0));
SelectObject(hdc,hpen);
MoveToEx(hdc,line_x,line_y,NULL);
LineTo(hdc,line_x1,line_y1);
DeleteObject(hpen);
hpen=CreatePen(PS_SOLID,1,RGB(0,0,255));
SelectObject(hdc,hpen);
line_x = line_x1;
line_y = line_y1;
MoveToEx(hdc,line_x,line_y,NULL);
line_x1 = rand()%640;
line_y1 = rand()%480;
LineTo(hdc,line_x1,line_y1);
SetPixel(hdc,START_X,START_Y,RGB(255,255,255));
SetPixel(hdc,START_X-1,START_Y-1,RGB(255,255,255));
SetPixel(hdc,START_X-1,START_Y+1,RGB(255,255,255));
SetPixel(hdc,START_X+1,START_Y-1,RGB(255,255,255));
SetPixel(hdc,START_X+1,START_Y+1,RGB(255,255,255));
SetPixel(hdc,START_X,START_Y+1,RGB(255,255,255));
SetPixel(hdc,START_X,START_Y-1,RGB(255,255,255));
SetPixel(hdc,START_X-1,START_Y,RGB(255,255,255));
SetPixel(hdc,START_X+1,START_Y,RGB(255,255,255));
ReleaseDC(main_window_handle,hdc);
Sleep(1000);
return(1);
}
////////////////////////////////////////////////////////////
int mypcluna_Init(void *parms = NULL, int num_parms = 0)
{
line_x = START_X;
line_y = START_Y;
line_x1 = line_x;
line_y1 = line_y;
srand( (unsigned)time( NULL ) );
return(1);
}
/////////////////////////////////////////////////////////////
int mypcluna_Shutdown(void *parms = NULL, int num_parms = 0)
{
if(hpen)
DeleteObject(hpen);
return(1);
}
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hinstance_app = hinstance;
if (!RegisterClassEx(&winclass))
return(0);
if (!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"T3D Game Console Version 1.0",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,
640,480,
NULL,
NULL,
hinstance,
NULL)))
return(0);
main_window_handle = hwnd;
mypcluna_Init();
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
mypcluna_Main();
}
mypcluna_Shutdown();
return(msg.wParam);
} // end WinMain
///////////////////////////////////////////////////////////
以下程序,使用Windows GDI函数随机画直线,当前直线的起点是前一条直线的终点。一开始的起点使用白色表示,为了明显,我特意把它画得大一点。画过的直线使用红色表示。正在画的直线使用蓝色表示。
特别注意:为了让你看得更清楚,我特意延时1秒,换句话说,当你点击窗口右上角的“X”关闭按钮时,要等一段时间(不会太长!)窗口才会关闭,注意!
下面是源程序,要想尝试请创建Win32 Application工程测试。
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN // 跟 MFC 说Bye-Bye
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <time.h>
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
#define START_X 50
#define START_Y 50
// 全局变量 ////////////////////////////////////////////////
HWND main_window_handle = NULL;
HINSTANCE hinstance_app = NULL;
HDC hdc;
HPEN hpen;
int line_x = 0,line_y = 0;
int line_x1 = 0,line_y1 = 0;
char buffer[80];
// 窗口过程 //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
}
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
///////////////////////////////////////////////////////////
int mypcluna_Main(void *parms = NULL, int num_parms = 0)
{
hdc=GetDC(main_window_handle);
if(hpen)
DeleteObject(hpen);
hpen=CreatePen(PS_SOLID,1,RGB(255,0,0));
SelectObject(hdc,hpen);
MoveToEx(hdc,line_x,line_y,NULL);
LineTo(hdc,line_x1,line_y1);
DeleteObject(hpen);
hpen=CreatePen(PS_SOLID,1,RGB(0,0,255));
SelectObject(hdc,hpen);
line_x = line_x1;
line_y = line_y1;
MoveToEx(hdc,line_x,line_y,NULL);
line_x1 = rand()%640;
line_y1 = rand()%480;
LineTo(hdc,line_x1,line_y1);
SetPixel(hdc,START_X,START_Y,RGB(255,255,255));
SetPixel(hdc,START_X-1,START_Y-1,RGB(255,255,255));
SetPixel(hdc,START_X-1,START_Y+1,RGB(255,255,255));
SetPixel(hdc,START_X+1,START_Y-1,RGB(255,255,255));
SetPixel(hdc,START_X+1,START_Y+1,RGB(255,255,255));
SetPixel(hdc,START_X,START_Y+1,RGB(255,255,255));
SetPixel(hdc,START_X,START_Y-1,RGB(255,255,255));
SetPixel(hdc,START_X-1,START_Y,RGB(255,255,255));
SetPixel(hdc,START_X+1,START_Y,RGB(255,255,255));
ReleaseDC(main_window_handle,hdc);
Sleep(1000);
return(1);
}
////////////////////////////////////////////////////////////
int mypcluna_Init(void *parms = NULL, int num_parms = 0)
{
line_x = START_X;
line_y = START_Y;
line_x1 = line_x;
line_y1 = line_y;
srand( (unsigned)time( NULL ) );
return(1);
}
/////////////////////////////////////////////////////////////
int mypcluna_Shutdown(void *parms = NULL, int num_parms = 0)
{
if(hpen)
DeleteObject(hpen);
return(1);
}
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hinstance_app = hinstance;
if (!RegisterClassEx(&winclass))
return(0);
if (!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"T3D Game Console Version 1.0",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,
640,480,
NULL,
NULL,
hinstance,
NULL)))
return(0);
main_window_handle = hwnd;
mypcluna_Init();
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
mypcluna_Main();
}
mypcluna_Shutdown();
return(msg.wParam);
} // end WinMain
///////////////////////////////////////////////////////////
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询