关于用VS2005 C/C++画点的问题
如何用VS2005画点,本人不会MFC,只懂纯写代码,目前新东西来不及学了,所以讲的越简单越好,最主要现在上课学计算机图形学要做练习,所以关键在看看效果对不对。请高手写一...
如何用VS2005画点,本人不会MFC,只懂纯写代码,目前新东西来不及学了,所以讲的越简单越好,最主要现在上课学计算机图形学要做练习,所以关键在看看效果对不对。
请高手写一段代码,实现画一个看得见的点
要黏贴上去直接就能通过编译的,OK?什么头文件的都要
Thanks 展开
请高手写一段代码,实现画一个看得见的点
要黏贴上去直接就能通过编译的,OK?什么头文件的都要
Thanks 展开
1个回答
展开全部
/* 如果能看懂,自己写好再调试,挺基础的 */
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct STNodeType
{
WORD x;
WORD y;
struct STNodeType *next;
} NodeType;
NodeType *poly_head;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain
(
HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[] = "basic";
char lpszTitle[]= "Drawing";
wndclass.style = 0;
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 = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// init poly list
poly_head = (NodeType *)malloc(sizeof(NodeType));
if (NULL == poly_head) return Msg.wParam;
poly_head->x = poly_head->y = 0;
poly_head->next = NULL;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while( GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
void BeforeRelease(HDC hdc)
{
HBRUSH hB;
NodeType *pNTA = NULL;
if (NULL == poly_head || NULL == poly_head->next)
return;
SetMapMode(hdc,MM_ANISOTROPIC);
pNTA = poly_head;
// one point can not be draw
while (NULL != pNTA->next)
{
hB=CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,hB); //update brush
Ellipse(hdc,pNTA->next->x-5,pNTA->next->y-5,pNTA->next->x+5,pNTA->next->y+5);
DeleteObject(hB);
pNTA = pNTA->next;
}
}
LRESULT CALLBACK WndProc
(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{ HDC hdc;
PAINTSTRUCT ps;
static BOOL LButtonDown = FALSE;
static WORD x,y;
switch(message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps); //取得设备环境句柄
BeforeRelease(hdc);
EndPaint(hwnd,&ps); //释放设备环境句柄
break;
case WM_LBUTTONDOWN:
LButtonDown = FALSE;
x=LOWORD(lParam); //mouse x.
y=HIWORD(lParam); //mouse y.
{
NodeType *pNTA;
pNTA = (NodeType *)malloc(sizeof(NodeType));
if (NULL != pNTA)
{
pNTA->x = x;
pNTA->y = y;
pNTA->next = poly_head->next;
poly_head->next = pNTA;
}
}
LButtonDown = TRUE;
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_DESTROY:
{
// release poly list
NodeType *pNTA = NULL;
if (NULL != poly_head)
{
pNTA = poly_head->next;
while (NULL != pNTA)
{
poly_head->next = pNTA->next;
free(pNTA);
pNTA = poly_head->next;
}
free(poly_head);
poly_head = NULL;
}
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct STNodeType
{
WORD x;
WORD y;
struct STNodeType *next;
} NodeType;
NodeType *poly_head;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain
(
HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[] = "basic";
char lpszTitle[]= "Drawing";
wndclass.style = 0;
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 = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// init poly list
poly_head = (NodeType *)malloc(sizeof(NodeType));
if (NULL == poly_head) return Msg.wParam;
poly_head->x = poly_head->y = 0;
poly_head->next = NULL;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while( GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
void BeforeRelease(HDC hdc)
{
HBRUSH hB;
NodeType *pNTA = NULL;
if (NULL == poly_head || NULL == poly_head->next)
return;
SetMapMode(hdc,MM_ANISOTROPIC);
pNTA = poly_head;
// one point can not be draw
while (NULL != pNTA->next)
{
hB=CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,hB); //update brush
Ellipse(hdc,pNTA->next->x-5,pNTA->next->y-5,pNTA->next->x+5,pNTA->next->y+5);
DeleteObject(hB);
pNTA = pNTA->next;
}
}
LRESULT CALLBACK WndProc
(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{ HDC hdc;
PAINTSTRUCT ps;
static BOOL LButtonDown = FALSE;
static WORD x,y;
switch(message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps); //取得设备环境句柄
BeforeRelease(hdc);
EndPaint(hwnd,&ps); //释放设备环境句柄
break;
case WM_LBUTTONDOWN:
LButtonDown = FALSE;
x=LOWORD(lParam); //mouse x.
y=HIWORD(lParam); //mouse y.
{
NodeType *pNTA;
pNTA = (NodeType *)malloc(sizeof(NodeType));
if (NULL != pNTA)
{
pNTA->x = x;
pNTA->y = y;
pNTA->next = poly_head->next;
poly_head->next = pNTA;
}
}
LButtonDown = TRUE;
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_DESTROY:
{
// release poly list
NodeType *pNTA = NULL;
if (NULL != poly_head)
{
pNTA = poly_head->next;
while (NULL != pNTA)
{
poly_head->next = pNTA->next;
free(pNTA);
pNTA = poly_head->next;
}
free(poly_head);
poly_head = NULL;
}
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询