拖放时坐标问题(100分)

  • 主题发起人 主题发起人 lyh001
  • 开始时间 开始时间
L

lyh001

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的 BCB程序在运行时,确定图象位置后,实际图形在不改变窗体滚动条时

shap1位置正确,可改变窗体滚动条后,shap1位置不正确,恳请各位指教。

//---------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Child0form.h"
#include "dmform.h"
#include "Main.h"
//---------------------------------------------------------------------
#pragma resource "*.dfm"

TPoint StartPt,EndPt;
bool bDrawing = False, bDraging = False;

//---------------------------------------------------------------------
__fastcall TfrmChild0::TfrmChild0(TComponent *Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------
void __fastcall TfrmChild0::FormClose(TObject *Sender, TCloseAction &amp;Action)
{
Action = caFree;
}
//---------------------------------------------------------------------
void __fastcall TfrmChild0::FormCreate(TObject *Sender)
{
Image1->Left = 0;
Image1->Top = 0;
Image1->Height = 3000;
Image1->Width = 796;
}

//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if( bDrawing ) //松开鼠标
{
bDrawing = False;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);

Image1->Canvas->Pen->Mode = pmCopy;
Image1->Canvas->Brush->Style = bsClear;

Shape1->Height = abs(EndPt.y - StartPt.y);
Shape1->Width = abs(EndPt.x - StartPt.x);
Shape1->Left = EndPt.x>StartPt.x ? StartPt.x: EndPt.x;
Shape1->Top = EndPt.y>StartPt.y ? StartPt.y: EndPt.y;


if( (Shape1->Width > 4) || (Shape1->Height > 2) )
{
MainForm->tbLeft->Enabled = True;
MainForm->tbRight->Enabled = True;
MainForm->tbUp->Enabled = True;
MainForm->tbDown->Enabled = True;
Shape1->Visible = True;
}
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if( Y>320 ) // 有效选择区
{
bDrawing = True; // begin bDrawing
Shape1->Visible = False;

MainForm->tbLeft->Enabled = False;
MainForm->tbRight->Enabled = False;
MainForm->tbUp->Enabled = False;
MainForm->tbDown->Enabled = False;

StartPt = TPoint(X,Y);
EndPt = StartPt;

Image1->Canvas->Pen->Mode = pmNot;
Image1->Canvas->Pen->Color = clRed;
Image1->Canvas->Brush->Style =bsClear;
Image1->Canvas->Pen->Style = psDot;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if( bDrawing &amp;&amp; ((EndPt.x != X)||(EndPt.y != Y)) &amp;&amp; (Y>320) )
{
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, X,Y);
EndPt.x = X;
EndPt.y = Y;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Shape1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartPt.x = X;
StartPt.y = Y;
bDraging = True;
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Shape1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if( bDraging )
{
Shape1->Left = Shape1->Left +X - StartPt.x;
Shape1->Top = Shape1->Top + Y - StartPt.y;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Shape1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
bDraging = False;
}
//---------------------------------------------------------------------------
 
MainForm是干什么用的,能不能发个完整的程序
 
把前面的程序进行简化,如下:

//---------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Child0form.h"
//---------------------------------------------------------------------
#pragma resource "*.dfm"

TPoint StartPt,EndPt;
bool bDrawing = False, bDraging = False;

//---------------------------------------------------------------------
__fastcall TfrmChild0::TfrmChild0(TComponent *Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if( bDrawing ) //松开鼠标
{
bDrawing = False;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Pen->Mode = pmCopy;
Image1->Canvas->Brush->Style = bsClear;

Shape1->Height = abs(EndPt.y - StartPt.y);
Shape1->Width = abs(EndPt.x - StartPt.x);
Shape1->Left = EndPt.x>StartPt.x ? StartPt.x: EndPt.x;
Shape1->Top = EndPt.y>StartPt.y ? StartPt.y: EndPt.y;
Shape1->Visible = True;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
bDrawing = True; // begin bDrawing
Shape1->Visible = False;

StartPt = TPoint(X,Y);
EndPt = StartPt;

Image1->Canvas->Pen->Mode = pmNot;
Image1->Canvas->Pen->Color = clRed;
Image1->Canvas->Brush->Style =bsClear;
Image1->Canvas->Pen->Style = psDot;
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if( bDrawing &amp;&amp; ((EndPt.x != X)||(EndPt.y != Y)) )
{
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, X,Y);
EndPt.x = X;
EndPt.y = Y;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Shape1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartPt.x = X;
StartPt.y = Y;
bDraging = True;
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Shape1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if( bDraging )
{
Shape1->Left = Shape1->Left +X - StartPt.x;
Shape1->Top = Shape1->Top + Y - StartPt.y;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Shape1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
bDraging = False;
}
//---------------------------------------------------------------------------

请各位大侠指教。
 
在 Image 的 Up , Down, Move 中,x, y是image的
你应该转换成form 的,
在设置shape 的left, top时转化一下,
加上: endpt := ClientToScreen(endpt);
endpt := form1.ScreenToClient(endpt);

对startpt 也一样。
 
To :
SuperMMX


我按您说的作了, 但仍然没有成功。能否贴出相关代码?


多谢!!!
 
void __fastcall TfrmChild0::Image1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if( bDrawing ) //松开鼠标
{
bDrawing = False;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Pen->Mode = pmCopy;
Image1->Canvas->Brush->Style = bsClear;

Shape1->Height = abs(EndPt.y - StartPt.y);
Shape1->Width = abs(EndPt.x - StartPt.x);
int itop , ileft;
itop = Image1->Top ;
ileft = Image1->Left ;
Shape1->Top = StartPt.y-abs(itop);
Shape1->Left = StartPt.x-abs(ileft);
Shape1->Visible = True;
}
}
 
Thank you very much !!!
 
接受答案了.
 
上面的代码对 普通窗体有效,但对 mdi子窗体无效,愿再出100$
 

Similar threads

后退
顶部