移动一个矩形时遇到的问题

coolnick 2007-04-20 12:02:10
我在收到WM_LBUTTONDOWN事件时,记住了鼠标单击的坐标,如下:
xBegin = LOWORD(lParam);
yBegin = HIWORD(lParam);

然后在收到WM_MOUSEMOVE事件时,移动矩形,如下:
xEnd = LOWORD(lParam);
yEnd = HIWORD(lParam);
MoveRect(xEnd - xBegin, yEnd - yBegin);
InvalidateRect(hwnd, NULL, TRUE);
RedrawRect();
其中MoveRect函数是改变矩形的左上角与右下角的坐标值,如下:
void MoveRect(int iXOffset, int iYOffset)
{
m_iPositionBeginX += iXOffset;
m_iPositionEndX += iXOffset;
m_iPositionBeginY += iYOffset;
m_iPositionEndY += iYOffset;
}
RedrawRect()是重新画矩形。

现在的问题是,当我移动鼠标时,矩形的确也移动,但矩形移动的
速度远远大于鼠标移动的速度,一下子,矩形就移动到用户区之外,看不到了,
关于映射模式与坐标原点,我都是使用的默认值。
不知道问题出在什么地方,请知道的指点一下!
...全文
409 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolnick 2007-04-20
  • 打赏
  • 举报
回复
MSDN上说的不正是这个意思吗?
lParam
The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

我的程序不正是这个意思吗?
xBegin = LOWORD(lParam);
yBegin = HIWORD(lParam);
WingForce 2007-04-20
  • 打赏
  • 举报
回复
介个。。。
xBegin = LOWORD(lParam);
yBegin = HIWORD(lParam);
很明显WM_LBUTTONDOWN的2个参数不是这么定义的,见msdn:
wParam
Indicates whether various virtual keys are down. This parameter can be one or more of the following values.
MK_CONTROL
The CTRL key is down.
MK_LBUTTON
The left mouse button is down.
MK_MBUTTON
The middle mouse button is down.
MK_RBUTTON
The right mouse button is down.
MK_SHIFT
The SHIFT key is down.
MK_XBUTTON1
Windows 2000/XP: The first X button is down.
MK_XBUTTON2
Windows 2000/XP: The second X button is down.
lParam
The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
WingForce 2007-04-20
  • 打赏
  • 举报
回复
void DrawMyRect( HDC hdc, RECT *rt )
{
MoveToEx( hdc, rt->left, rt->top, NULL );
LineTo( hdc, rt->right, rt->top );
LineTo( hdc, rt->right, rt->bottom );
LineTo( hdc, rt->left, rt->bottom );
LineTo( hdc, rt->left, rt->top );

}


//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
POINT pt;

static BOOL blFlag = FALSE;
static RECT rtTar;
static POINT ptSrc, ptTar;

switch (message)
{
case WM_CREATE:
rtTar.left = 10;
rtTar.right = 300;
rtTar.top = 10;
rtTar.bottom = 300;

break;

case WM_MOUSEMOVE:
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if ( blFlag )
{
ptTar = pt;
OffsetRect( &rtTar, ptTar.x - ptSrc.x, ptTar.y - ptSrc.y );
InvalidateRect( hWnd, NULL, TRUE );
ptSrc = pt;
}
break;

case WM_LBUTTONDOWN:

pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);

if ( PtInRect( &rtTar, pt ) )
{
ptSrc = pt;
blFlag = TRUE;
}

break;

case WM_LBUTTONUP:
blFlag = FALSE;
break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
DrawMyRect( hdc, &rtTar );
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
WingForce 2007-04-20
  • 打赏
  • 举报
回复
在mousemove里移动矩形后,重新设置xBegin,
yBegin为当前的鼠标点
===================================================
恩,就是这个问题,因为iPositionBeginX 和iPositionBeginY 始终是WM_LBUTTONDOWN的值
每次调用这个函数都会将矩形坐标移动从当前点到这个原始点这么多位移动
也就是说每次触发WM_MOUSEMOVE都会触发一次,位移动会不断累积
T97102003 2007-04-20
  • 打赏
  • 举报
回复
在mousemove里移动矩形后,重新设置xBegin,
yBegin为当前的鼠标点
yoyo_alex_lw 2007-04-20
  • 打赏
  • 举报
回复
void MoveRect(int iXOffset, int iYOffset)
{
m_iPositionBeginX += iXOffset;
m_iPositionEndX += iXOffset;
m_iPositionBeginY += iYOffset;
m_iPositionEndY += iYOffset;
}
是不是这个函数使得移动太快?每次这里移动小幅度点,试试
WingForce 2007-04-20
  • 打赏
  • 举报
回复
sorry,没有看仔细

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧