C++中怎么用API作图啊 ,要包含什么头文件吗? 在先急等
vc6.0C++中怎么用API作图啊,要包含什么头文件吗?不是用MFC哦。希望有例子,画个圆矩形等等...
vc6.0 C++中怎么用API作图啊 ,要包含什么头文件吗?
不是用MFC哦。希望有例子,画个圆 矩形等等 展开
不是用MFC哦。希望有例子,画个圆 矩形等等 展开
展开全部
MFC实际上也是调用了API来作图。你说的问题实际上是过去没MFC时,用C/C++和WIN API开发Windows程序的技术。这技术已经淘汰了!但是越老的技术貌似越强大!
MSDN上有WIN32程序开发的例子。 你用向导添加一个WIN32项目,再自己重写OnPaint()函数,最后用InvalidateRect(),来激发一个重画事件。
做图要用到API:
void OnPaint(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps); //GetDC本质上调用了这个
//在这里加画图代码。如:
Ellipse(hdc,0,0,100,100); //画圆
Rectangle(hdc,0,100,100,200);//画矩形
TextOut(hdc,50,50,_T("Hello, World"),6); //文字也是图
EndPaint(hWnd,&ps); //释放DC handle
}
MSDN上有WIN32程序开发的例子。 你用向导添加一个WIN32项目,再自己重写OnPaint()函数,最后用InvalidateRect(),来激发一个重画事件。
做图要用到API:
void OnPaint(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps); //GetDC本质上调用了这个
//在这里加画图代码。如:
Ellipse(hdc,0,0,100,100); //画圆
Rectangle(hdc,0,100,100,200);//画矩形
TextOut(hdc,50,50,_T("Hello, World"),6); //文字也是图
EndPaint(hWnd,&ps); //释放DC handle
}
展开全部
MFC的绘图类其实是对Windows GDI API的封装,目前发展到GDI+,其实直接使用GDI+也是相当强大的,我给你介绍一下怎么使用windows的GDI+吧:
(1)准备使用GDI+ 的API
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
(2)列子:
VOID Example_DrawLine(HDC hdc){ // 函数参数为当前绘图设备上下文的句柄
Graphics graphics(hdc); // 构造一个graphics对象,然后绘图就引用此对象方法即可,厉害啊
Pen blackPen(Color(255, 0, 0, 0), 3); // 创建画笔
Point point1(100, 100); // 两个点
Point point2(500, 100);
graphics.DrawLine(&blackPen, point1, point2); // 画一条线
}
VOID Example_DrawRectangle(HDC hdc)
{
Graphics graphics(hdc);
Pen blackPen(Color(255, 0, 0, 0), 3);
Rect rect(0, 0, 200, 200); // 矩形
graphics.DrawRectangle(&blackPen, rect);
}
VOID Example_DrawEllipse(HDC hdc
{
Graphics graphics(hdc);
Pen bluePen(Color(255, 0, 0, 255));
Rect ellipseRect(0, 0, 200, 100);
graphics.DrawEllipse(&bluePen, ellipseRect);
}
相信你看了这几个列子应该知道怎么使用GDI+ 的API来作图了
(1)准备使用GDI+ 的API
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
(2)列子:
VOID Example_DrawLine(HDC hdc){ // 函数参数为当前绘图设备上下文的句柄
Graphics graphics(hdc); // 构造一个graphics对象,然后绘图就引用此对象方法即可,厉害啊
Pen blackPen(Color(255, 0, 0, 0), 3); // 创建画笔
Point point1(100, 100); // 两个点
Point point2(500, 100);
graphics.DrawLine(&blackPen, point1, point2); // 画一条线
}
VOID Example_DrawRectangle(HDC hdc)
{
Graphics graphics(hdc);
Pen blackPen(Color(255, 0, 0, 0), 3);
Rect rect(0, 0, 200, 200); // 矩形
graphics.DrawRectangle(&blackPen, rect);
}
VOID Example_DrawEllipse(HDC hdc
{
Graphics graphics(hdc);
Pen bluePen(Color(255, 0, 0, 255));
Rect ellipseRect(0, 0, 200, 100);
graphics.DrawEllipse(&bluePen, ellipseRect);
}
相信你看了这几个列子应该知道怎么使用GDI+ 的API来作图了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一个不停地显示随机矩形的程序
#include <windows.h>
#include <stdlib.h>// for the rand function
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void DrawRectangle (HWND) ;
int cxClient, cyClient ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("RandRect") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc= WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName= NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (TRUE)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break ;
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
else
DrawRectangle (hwnd) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
void DrawRectangle (HWND hwnd)
{
HBRUSHhBrush ;
HDC hdc ;
RECT rect ;
if (cxClient == 0 || cyClient == 0)
return ;
SetRect (&rect, rand () % cxClient, rand () % cyClient,
rand () % cxClient, rand () % cyClient) ;
hBrush = CreateSolidBrush (
RGB (rand () % 256, rand () % 256, rand () % 256)) ;
hdc = GetDC (hwnd) ;
FillRect (hdc, &rect, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
#include <windows.h>
#include <stdlib.h>// for the rand function
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void DrawRectangle (HWND) ;
int cxClient, cyClient ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("RandRect") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc= WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName= NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (TRUE)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break ;
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
else
DrawRectangle (hwnd) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
void DrawRectangle (HWND hwnd)
{
HBRUSHhBrush ;
HDC hdc ;
RECT rect ;
if (cxClient == 0 || cyClient == 0)
return ;
SetRect (&rect, rand () % cxClient, rand () % cyClient,
rand () % cxClient, rand () % cyClient) ;
hBrush = CreateSolidBrush (
RGB (rand () % 256, rand () % 256, rand () % 256)) ;
hdc = GetDC (hwnd) ;
FillRect (hdc, &rect, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
参考资料: Windows程序设计中文版(CHM)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-04-13
展开全部
#include "windows.h"
#include "winuser.h"
#include "wingdi.h"
HWND hwnd = 0; //如果是0,则表示在屏幕上画
HDC hdc = GetDC(hwnd);
Ellipse(hdc,0,0,100,100); //画圆
Rectangle(hdc,0,100,100,200);//画矩形
ReleaseDC(hwnd,hdc);
#include "winuser.h"
#include "wingdi.h"
HWND hwnd = 0; //如果是0,则表示在屏幕上画
HDC hdc = GetDC(hwnd);
Ellipse(hdc,0,0,100,100); //画圆
Rectangle(hdc,0,100,100,200);//画矩形
ReleaseDC(hwnd,hdc);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先包含windows.h再说。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询