为什么鼠标点击和显示的位置不在同一位置呢?附调试代码
我用下面的事件处理鼠标的动作,可是鼠标按下移动的时候,鼠标显示的位置总是不在移动的点上,请问应该怎么修改下面的代码,才能使鼠标点击和显示的位置一致呢?多谢
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if (Button != mbLeft) return;
// initialize point variables
AnchorPoint.x=CurrentPoint.x=X;
AnchorPoint.y=CurrentPoint.y=Y;
Bounding=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if (Bounding == true)
{
// first, erase the old bounding box
Canvas->Pen->Mode = pmNot;
Canvas->Pen->Width = 1;
Canvas->Brush->Style=bsClear;
Canvas->Rectangle(AnchorPoint.x,AnchorPoint.y,
CurrentPoint.x,CurrentPoint.y);
// update location and redraw the box
CurrentPoint.x=X;
CurrentPoint.y=Y;
Canvas->Rectangle(AnchorPoint.x,AnchorPoint.y,
CurrentPoint.x,CurrentPoint.y);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if ((Bounding==true) && (Button == mbLeft))
{
// turn off bounding, and then erase the bounding box
// by drawing it again using pmNot.
Bounding=false;
Canvas->Pen->Mode = pmNot;
Canvas->Pen->Width = 1;
Canvas->Brush->Style=bsClear;
Canvas->Rectangle(AnchorPoint.x,AnchorPoint.y,
CurrentPoint.x,CurrentPoint.y);
}
}
//---------------------------------------------------------------------------
问题点数:20、回复次数:6Top
1 楼xuv2002(XuWei)回复于 2003-06-04 10:03:19 得分 20
Canvas->Pen->Mode = pmNot;
Canvas->Pen->Width = 1;
Canvas->Brush->Style=bsClear;
???
老大你是要在哪里画图呀
假如是在Image的mousemove 里面
自然用
Image1->Canvas->Pen->Mode
.....
你这里的画图都画在Form上了吧,当然会不一样了Top
2 楼slhuang(★Shawn★)回复于 2003-06-04 10:03:20 得分 0
应该是 关于坐标转换的问题:
ScreenToClient()试一下。……Top
3 楼neighbornet(Matlab)回复于 2003-06-04 10:35:53 得分 0
To: xuv2002
我把相应的Canvas前都加上Image1->,可是还是不行的啊,坐标都跑到右下方去了,在鼠标的右下方的一个固定的相应位置,请帮忙看看应该如何修改,多谢Top
4 楼xuv2002(XuWei)回复于 2003-06-04 11:31:47 得分 0
没有呀
我试了你的代码,将Canvas 改为Image1->Canvas 之后运行良好呀
Top
5 楼neighbornet(Matlab)回复于 2003-06-04 12:37:05 得分 0
我的Image1->Strech=true;就出问题了
若Image1->Strech=false;
或Image1->AutoSize=true都是没问题的
请问若Image1->Strech=true;应该怎么实现呢?多谢Top
6 楼xuv2002(XuWei)回复于 2003-06-04 12:48:49 得分 0
那就得算图像比例了
需要将事件里面的X,Y 通过比例变换
在MouseDown里面
float scalex = (float)Image1->Width/Image1->Picture->Bitmap->Width;
float scaley = (float) Image1->Height/Image1->Picture->Bitmap->Height;
// initialize point variables
AnchorPoint.x=CurrentPoint.x=X/scalex;
AnchorPoint.y=CurrentPoint.y=Y/scaley;
其他地方相似处理Top




