拖动 TPaintBox 同时保持其内容静态
我们有一个用于显示视频的 VCL TPaintBox。它的父级是 TScrollbox。
缩放时,PaintBox 变得比滚动框大,因此部分图像被隐藏。
我们已经实现了使用鼠标在滚动框中拖动绘画框的功能,效果很好。
然而,只要移动paintBox,即更改其left 或top 属性,其内容就会被擦除。
问题是,有没有办法防止油漆盒在移动时擦除其内容?
我创建了一个简单的 VCL Forms 示例,其中滚动框内有一个绘画框,代码如下。 单击键盘时,会在颜料盒上绘制一条线。
该示例表明,如果没有持续绘画,颜料盒的内容会在移动时被擦除。问题是,如何避免,或者应对?
#ifndef Unit1H
#define Unit1H
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include "RzButton.hpp"
class TForm1 : public TForm
{
__published:
TPaintBox *PB;
TScrollBox *ScrollBox1;
void __fastcall ObjMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall ObjMouseMove(TObject *Sender, TShiftState Shift, int X, int Y);
void __fastcall ObjMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall PBPaint(TObject *Sender);
void __fastcall FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
public:
__fastcall TForm1(TComponent* Owner);
bool mIsDragging;
TPoint mMouseDownLocation;
TPoint mDragStartingLocation;
};
extern PACKAGE TForm1 *Form1;
#endif
和来源
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RzButton"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{}
//---------------------------------------------------------------------------
void __fastcall TForm1::ObjMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
int X, int Y)
{
mIsDragging = true;
mMouseDownLocation = Mouse->CursorPos;
mDragStartingLocation = TPoint(PB->Left, PB->Top);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ObjMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(mIsDragging)
{
PB->Left = mDragStartingLocation.X + (Mouse->CursorPos.X - mMouseDownLocation.X);
PB->Top = mDragStartingLocation.Y + (Mouse->CursorPos.Y - mMouseDownLocation.Y);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ObjMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
mIsDragging = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PBPaint(TObject *Sender)
{
// PB->Canvas->Pen->Color = clRed;
// PB->Canvas->Pen->Width = 3;
// PB->Canvas->MoveTo(50,50);
// PB->Canvas->LineTo(150, 150);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
PB->Canvas->Pen->Color = clRed;
PB->Canvas->Pen->Width = 3;
PB->Canvas->MoveTo(50,50);
PB->Canvas->LineTo(150, 150);
}
We are having a VCL TPaintBox on which we display video. Its parent is a TScrollbox.
When zooming, the PaintBox gets larger than the scrollbox, so parts of the image is hidden.
We have implemented the ability to drag the paintbox, using the mouse, within the scrollbox, which works fine.
However, whenever the paintBox is moved, i.e. its left or top property is changed, its content gets erased.
Question is, is there a way to prevent the paintbox to erase its content while moving it?
I created a simple VCL Forms example with a paintbox inside a scroll box, code below.
When clicking on the keyboard, a line is painted on the paintbox.
The example shows, that without constant painting, the content of the paintbox gets erased upon moving itself. Question is, how to avoid, or deal with?
#ifndef Unit1H
#define Unit1H
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include "RzButton.hpp"
class TForm1 : public TForm
{
__published:
TPaintBox *PB;
TScrollBox *ScrollBox1;
void __fastcall ObjMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall ObjMouseMove(TObject *Sender, TShiftState Shift, int X, int Y);
void __fastcall ObjMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall PBPaint(TObject *Sender);
void __fastcall FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
public:
__fastcall TForm1(TComponent* Owner);
bool mIsDragging;
TPoint mMouseDownLocation;
TPoint mDragStartingLocation;
};
extern PACKAGE TForm1 *Form1;
#endif
and source
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RzButton"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{}
//---------------------------------------------------------------------------
void __fastcall TForm1::ObjMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
int X, int Y)
{
mIsDragging = true;
mMouseDownLocation = Mouse->CursorPos;
mDragStartingLocation = TPoint(PB->Left, PB->Top);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ObjMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(mIsDragging)
{
PB->Left = mDragStartingLocation.X + (Mouse->CursorPos.X - mMouseDownLocation.X);
PB->Top = mDragStartingLocation.Y + (Mouse->CursorPos.Y - mMouseDownLocation.Y);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ObjMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
mIsDragging = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PBPaint(TObject *Sender)
{
// PB->Canvas->Pen->Color = clRed;
// PB->Canvas->Pen->Width = 3;
// PB->Canvas->MoveTo(50,50);
// PB->Canvas->LineTo(150, 150);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
PB->Canvas->Pen->Color = clRed;
PB->Canvas->Pen->Width = 3;
PB->Canvas->MoveTo(50,50);
PB->Canvas->LineTo(150, 150);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论