为什么鼠标按下会阻止组件的重新绘制?

时间:2018-06-28 21:21:14

标签: c++ c++builder vcl c++builder-10.2-tokyo

我正在尝试创建一个自定义组件来捕获鼠标事件,尤其是MouseMove。

我来自TWinControl,但我也尝试过使用TGraphicControlTCustomControlTTrackBar等。

我的问题是当我在组件上按住鼠标时,它没有被重新粉刷。

即使我调用Paint(),直到释放鼠标按钮,Invalidate()方法才被调用。

TrackBar是我要制作的最接近的组件。您选择刻度,并用鼠标将其移动。但是您不必释放鼠标即可同时看到刻度线移动(再次绘制组件)。

如果我直接致电Paint(),它可以工作,但背景不会被擦除。

我想念什么?

编辑: 我再次尝试,并确认是否按住了鼠标Invalidate();。仅在释放鼠标时才考虑通话。 用下面的代码尝试一下自己,油漆仅在释放时调用:

__fastcall TMyCustomComponent::TMyCustomComponent(TComponent* Owner)
    : TCustomTransparentControl(Owner)
{
    mValue = 0;
}

void __fastcall TMyCustomComponent::MouseDown(System::Uitypes::TMouseButton Button, System::Classes::TShiftState Shift, int X, int Y)
{
    if (Button == mbLeft)
        {
        mValueStart = 0;        
        }
}

void __fastcall TMyCustomComponent::MouseMove(System::Classes::TShiftState Shift, int X, int Y)
{       
    Invalidate();       
}

void __fastcall TMyCustomComponent::Paint(void)
{
    TGraphicControl::Paint();   
    Canvas->Font->Name = "Arial";
    Canvas->Font->Size = 8;
    Canvas->Font->Style = TFontStyles() << fsBold;
    Canvas->Font->Color = clInfoText;
    Canvas->Brush->Color = clInfoBk;
    Canvas->FillRect(TRect(0, 0, 104, 21));
    mValue++;
    Canvas->TextOut(0, 2, AnsiString(mValue));
    Canvas->Brush->Color = clBtnShadow; 
}

1 个答案:

答案 0 :(得分:0)

以下对我来说很好:

&

按下鼠标左键会将__fastcall TMyCustomComponent::TMyCustomComponent(TComponent* Owner) : TCustomTransparentControl(Owner) { mValue = 0; InterceptMouse = true; // <-- needed for TCustomTransparentControl to trigger Mouse...() methods! } void __fastcall TMyCustomComponent::MouseDown(System::Uitypes::TMouseButton Button, System::Classes::TShiftState Shift, int X, int Y) { if (Button == mbLeft) { mValue = 0; Invalidate(); } TCustomTransparentControl::MouseDown(Button, Shift, X, Y); } void __fastcall TMyCustomComponent::MouseMove(System::Classes::TShiftState Shift, int X, int Y) { ++mValue; Invalidate(); TCustomTransparentControl::MouseMove(Shift, X, Y); } void __fastcall TMyCustomComponent::Paint() { TCustomTransparentControl::Paint(); Canvas->Font->Name = "Arial"; Canvas->Font->Size = 8; Canvas->Font->Style = TFontStyles() << fsBold; Canvas->Font->Color = clInfoText; Canvas->Brush->Color = clInfoBk; Canvas->FillRect(TRect(0, 0, ClientWidth, ClientHeight)); Canvas->TextOut(0, 2, String(mValue)); Canvas->Brush->Color = clBtnShadow; } 重置为0并将其绘制。并在控件周围移动鼠标会递增mValue并进行绘制,无论是否按住鼠标按钮。