下面是《Windows程序设计》例程中第七章鼠标那章的例程,程序实现的功能是在鼠标移动消息处理中,将收到的移动消息时鼠标的位置保存在数组中,并在客户区绘制一个点。在WM_PAINT消息处理中将这些点用绘线函数联接起来。在WM_PAINT消息中的注释掉的语句是我用于调试的。但是发现只要执行了swprintf,最后缓制联接出来的图形就是从第三个点开始,而不是从第一个点。如果注释掉那swprintf就是从第一个点开始绘线联接。我实在不知道原因,还请各位指点。
/*--------------------------------------------------
CONNECT.C -- Connect-the-Dots Mouse Demo Program
(c) Charles Petzold, 1998
--------------------------------------------------*/#include <windows.h>
#include <string.h>
#include <stdio.h>
#define MAXPOINTS 1000LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "Connect" ;
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, "Program requires Windows NT!",
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, "Connect-the-Points Mouse Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static POINT pt[MAXPOINTS], PtGo ;
static int iCount ;
HDC hdc ;
int i, j ;
PAINTSTRUCT ps ;
static char szBuffer[25]; switch (message)
{
case WM_LBUTTONDOWN:
iCount = 0 ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON && iCount < 1000)
{
pt[iCount ].x = LOWORD (lParam) ;
pt[iCount].y = HIWORD (lParam) ;
hdc = GetDC (hwnd) ;
SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0) ;
PtGo.x = pt[iCount].x ;
PtGo.y = pt[iCount++].y ;
/*
wsprintf(szBuffer, "MouseX=%d, MouseY=%d ==== P.X=%d, P.Y=%d",
LOWORD (lParam), HIWORD (lParam), PtGo.x,PtGo.y);
TextOut(hdc,5,364,szBuffer,lstrlen(szBuffer));
*/
ReleaseDC (hwnd, hdc) ;
}
return 0 ;
case WM_LBUTTONUP:
InvalidateRect (hwnd, NULL, FALSE) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
ShowCursor (TRUE) ;
for (i = 0 ; i < iCount - 1 ; i++)
for (j = i + 1 ; j < iCount ; j++)
{
MoveToEx (hdc, pt[i].x, pt[i].y, NULL) ;
LineTo (hdc, pt[j].x, pt[j].y) ;
}
ShowCursor (FALSE) ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
/*--------------------------------------------------
CONNECT.C -- Connect-the-Dots Mouse Demo Program
(c) Charles Petzold, 1998
--------------------------------------------------*/#include <windows.h>
#include <string.h>
#include <stdio.h>
#define MAXPOINTS 1000LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "Connect" ;
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, "Program requires Windows NT!",
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, "Connect-the-Points Mouse Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static POINT pt[MAXPOINTS], PtGo ;
static int iCount ;
HDC hdc ;
int i, j ;
PAINTSTRUCT ps ;
static char szBuffer[25]; switch (message)
{
case WM_LBUTTONDOWN:
iCount = 0 ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON && iCount < 1000)
{
pt[iCount ].x = LOWORD (lParam) ;
pt[iCount].y = HIWORD (lParam) ;
hdc = GetDC (hwnd) ;
SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0) ;
PtGo.x = pt[iCount].x ;
PtGo.y = pt[iCount++].y ;
/*
wsprintf(szBuffer, "MouseX=%d, MouseY=%d ==== P.X=%d, P.Y=%d",
LOWORD (lParam), HIWORD (lParam), PtGo.x,PtGo.y);
TextOut(hdc,5,364,szBuffer,lstrlen(szBuffer));
*/
ReleaseDC (hwnd, hdc) ;
}
return 0 ;
case WM_LBUTTONUP:
InvalidateRect (hwnd, NULL, FALSE) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
ShowCursor (TRUE) ;
for (i = 0 ; i < iCount - 1 ; i++)
for (j = i + 1 ; j < iCount ; j++)
{
MoveToEx (hdc, pt[i].x, pt[i].y, NULL) ;
LineTo (hdc, pt[j].x, pt[j].y) ;
}
ShowCursor (FALSE) ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
解决方案 »
- 现在还要不要学COM
- windows系统中SACK补发机制很奇怪的问题。
- 关于窗体视图CFormView::OnTimer()的消息映射BEGIN_MESSAGE_MAP
- WINCE 文件路径问题
- MFC使用ado连接数据库,还需要使用ODBC配置dsn吗
- 一个数据转换的问题???
- win32 dll中include "afx.h", 出现莫名其妙的连接问题,提示DllMain重复定义,如何解决,谢谢!
- LINK : fatal error LNK1104: cannot open file "mfc42u.lib"怎么处理?
- 如何在应用程序中显示网页
- MFC输出文本的宽度控制问题
- MFC扩展DLL和STL的map问题
- 请问一个COM+编译错误的问题,谢谢!
static char szBuffer[25];
改成
static char szBuffer[50];
由于你定义的变量数据区太小,wsprintf函数导致数据缓冲区益处,覆盖了pt数据被覆盖,也就是数据丢失,这样就画不出来了
太感谢了,确实是这个问题。我虽然有注意变量大小,但没有发现有溢出。习惯了在非C语言下的字符串变量不存在这个问题,对字符长度没有感觉。随意指定了个数字。实在感谢!