拖动 TPaintBox 同时保持其内容静态

发布于 2025-01-13 08:50:07 字数 3311 浏览 5 评论 0原文

我们有一个用于显示视频的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文