help !!教教绘图(C++)
各位大哥大姐,能否教教我一些基本的绘图知识(C++下)呢?
就从画线,画框等教教吧!C++我没什么基础,但老师要我们用它来画空间立方体,好可怜啊!
还有,能推荐一些资料或书籍给我么?
[解决办法]
汗……空间立方体,不会是旋转的吧……如果是那样的话,只能用DirectX或者是OpenGL才最“简单”了。
如果是静止的话,用win32api里面的GDI函数还是可以搞定的,找找相关的资料,COPY一份基本的程序,然后往里面添加自己的画图命令吧。
看看下面的画矩形程序参考一下:(别害怕,只有标着中文注释的地方才是画图需要的,其它的东西可以暂不理会)
一个简单的窗口程序,在窗口里面画个矩形:
// DEMO2_3.CPP - A complete windows program
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
// DEFINES ////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1 "
// GLOBALS ////////////////////////////////////////////////
// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCTps;// used in WM_PAINT
HDChdc;// handle to a device context
RECTrect;
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;
case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// you would do all your painting here
//这里就是你通常画图的地方
//比如说,画个矩形吧
Rectangle(hdc, 10, 10, 300, 300);
// ok, end paint
EndPaint(hwnd,&ps);
// return success
return(0);
} break;
case WM_DESTROY:
{
// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);
// return success
return(0);
} break;
default:break;
} // end switch
// process any messages that we didn 't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
// first fill in the window class stucture
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(WHITE_BRUSH);
winclass.lpszMenuName= NULL;
winclass.lpszClassName= WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Your Basic Window ", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
400,400, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL)))// extra creation parms
return(0);
ShowWindow(hwnd, ncmdshow);
UpdateWindow(hwnd);
// enter main event loop
while(GetMessage(&msg,NULL,0,0))
{
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end while
// return to Windows like this
return(int)(msg.wParam);
} // end WinMain
///////////////////////////////////////////////////////////
[解决办法]
是C++呀,用的就是VC7.1编译通过的,不过是windows图形界面下的程序,所以可能看起来代码挺多挺复杂——其实也确实挺复杂。
也可以使用Borland C或者C++里面带的 <graphics.h> 库,编写另一种图形方式下的画图程序,不需要了解Windows图形界面的知识,可能会简单许多,相关的资料能够在网上找到不少。
比如这个:
http://www.diybl.com/bbs/viewthread.php?tid=85