- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 TPaintBox 创建一个新组件。新组件大小为 Rect(0,0,160,248)。我在新组件上画了两个矩形,我想为上面的每个矩形实现事件。
获取矩形位于 Rect(102,43,157,63),我想为这个矩形区域实现 OnGetClick 、 OnGetMouseDown 、 OnGetMouseUp 等事件。
设置矩形位于 Rect(102,69,157,89),我想为这个矩形区域实现 OnSetClick、OnSetMouseDown、OnSetMouseUp 等事件。
新组件区域的其余部分将显示与 VGauge 相关的值,我不包含在下面的代码中。
class PACKAGE TVGauge : public TPaintBox
{
public:
virtual void __fastcall Paint(void) ;
};
void __fastcall TVGauge::Paint(void)
{
int ind1, ind2 ;
TRect tempR ;
String str;
Canvas->Font->Size = 8 ;
//whole rect
Canvas->Pen->Color = clSilver ;
Canvas->Brush->Color = clBtnFace ;
Canvas->Rectangle(ClientRect) ; //Rect(0,0,160,248)
//----------
Canvas->Pen->Color = clMedGray ;
Canvas->Font->Color = clWindowText ;
Canvas->Brush->Color = clWhite ;
//Get Button ; draw a rectangle and it should act like a button
Canvas->Rectangle(102 , 43 , 157 , 63 ) ;
Canvas->TextOut(112, 48 ,L"Get");
//Set Button ; draw a rectangle and it should act like a button
Canvas->Rectangle(102 , 69 , 157 , 89 ) ;
Canvas->TextOut(112, 74 ,L"Set");
//display values related to the VGauge
//..
//..
if(OnPaint != NULL) OnPaint(this) ;
}
我不知道如何为新组件实现上述事件。因此,我希望获得实现此组件的建议。
最佳答案
重写虚拟的 MouseDown()
和 MouseUp()
方法,例如:
enum TVGaugeButton { tvgGetBtn, tvgSetBtn };
typedef void __fastcall (__closure *TVGaugeMouseEvent)(TObject *Sender, TVGaugeButton GaugeButton, TMouseButton MouseButton, TShiftState Shift, int X, int Y);
typedef void __fastcall (__closure *TVGaugeClickEvent)(TObject *Sender, TVGaugeButton GaugeButton);
class PACKAGE TVGauge : public TPaintBox
{
typedef TPaintBox inherited;
private:
bool FMouseDown[2];
TVGaugeMouseEvent FOnGaugeButtonMouseDown;
TVGaugeMouseEvent FOnGaugeButtonMouseUp;
TVGaugeClickEvent FOnGaugeButtonClick;
TRect __fastcall GetButtonRect(TVGaugeButton WhichButton);
protected:
DYNAMIC void __fastcall MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y);
virtual void __fastcall Paint();
public:
__fastcall TVGauge(TComponent *Owner);
virtual void __fastcall SetBounds(int ALeft, int ATop, int AWidth, int AHeight);
__published:
__property TVGaugeClickEvent OnGaugeButtonClick = {read=FOnGaugeButtonClick, write=FOnGaugeButtonClick};
__property TVGaugeMouseEvent OnGaugeButtonMouseDown ={read=FOnGaugeButtonMouseDown, write=FOnGaugeButtonMouseDown};
__property TVGaugeMouseEvent OnGaugeButtonMouseUp = {read=FOnGaugeButtonMouseUp, write=FOnGaugeButtonMouseUp};
};
__fastcall TVGauge::TVGauge(TComponent *Owner)
: TPaintBox(Owner)
{
}
void __fastcall TVGauge::SetBounds(int ALeft, int ATop, int AWidth, int AHeight)
{
inherited::SetBounds(ALeft, ATop, 160, 248);
}
TRect __fastcall TVGauge::GetButtonRect(TVGaugeButton WhichButton)
{
switch (WhichButton)
{
case tvgGetBtn:
return Rect(102, 43, 157, 63);
case tvgSetBtn:
return Rect(102, 69, 157, 89);
}
return Rect(0, 0, 0, 0);
}
void __fastcall TVGauge::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y)
{
inherited::MouseDown(Button, Shift, X, Y);
for (int i = tvgGetBtn; i <= tvgSetBtn; ++i)
{
TVGaugeButton myBtn = (TVGaugeButton) i;
TRect r = GetButtonRect(myBtn);
if (PtInRect(&r, Point(X, Y)))
{
if (Button == mbLeft)
FMouseDown[i] = true;
if (FOnGaugeButtonMouseDown)
FOnGaugeButtonMouseDown(this, myBtn, Button, Shift, X-r.Left, Y-r.Top);
break;
}
}
}
void __fastcall TVGauge::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y)
{
inherited::MouseDown(Button, Shift, X, Y);
for (int i = tvgGetBtn; i <= tvgSetBtn; ++i)
{
TVGaugeButton myBtn = (TVGaugeButton) i;
TRect r = GetButtonRect(myBtn);
if (PtInRect(&r, Point(X, Y)))
{
bool bClicked = false;
if (Button == mbLeft)
{
FMouseDown[i] = false;
bClicked = true;
}
if (FOnGaugeButtonMouseUp)
FOnGaugeButtonMouseUp(this, myBtn, Button, Shift, X-r.Left, Y-r.Top);
if ((bClicked) && (FOnGaugeButtonClick))
FOnGaugeButtonClick(this, myBtn);
break;
}
}
}
void __fastcall TVGauge::Paint()
{
TRect tempR;
Canvas->Font->Size = 8;
//whole rect
Canvas->Pen->Color = clSilver;
Canvas->Brush->Color = clBtnFace;
Canvas->Rectangle(ClientRect);
//----------
Canvas->Pen->Color = clMedGray;
Canvas->Font->Color = clWindowText;
Canvas->Brush->Color = clWhite;
//Get Button ; draw a rectangle and it should act like a button
tempR = GetButtonRect(tvgGetBtn);
Canvas->Rectangle(tempR);
Canvas->TextRect(tempR, tempR.Left + 10, tempR.Top + 5, _D("Get"));
//Set Button ; draw a rectangle and it should act like a button
tempR = GetButtonRect(tvgSetBtn);
Canvas->Rectangle(tempR);
Canvas->TextRect(tempR, tempR.Left + 10, tempR.Top + 5, _D("Set"));
//display values related to the VGauge
//..
//..
if (OnPaint)
OnPaint(this);
}
附带说明一下,您确实应该直接从 TGraphicControl
而不是从 TPaintBox
派生您的组件。 TPaintBox
和 TGraphicControl
之间的唯一区别是 TPaintBox
公开了一个 OnPaint
事件,它覆盖 Paint()
以触发 OnPaint
。您实际上根本不需要组件中的 OnPaint
事件,因为您正在完成所有自己的绘画(除非您真的希望用户在我们的绘画之上绘画)。
关于c++ - 如何实现从 TPaintBox 创建的新组件的 OnClick、OnMouseDown、OnMouseUp 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43467861/
我已经在 mouseUp 上设置了事件监听器。 我想在短时间内(只需单击)onmouseUp 和按住后的 onmouseUp 之间进行区分,让我们说超过一秒或 onmouseDown 与 mouseM
我有几个全局变量被设置为 $(this)在小 1 或 2 按钮的 mouseup 上。基于 mouseup 上全局变量的比较,我的代码应该做一些事情。当我在 Firebug 中单步执行代码时,变量被设
我正在开发一个 tilemap 构造器应用程序,它使用类似绘画的功能来选择一个图 block (来自图像元素的 src)并使用 mouseup 和 mousedown 绘制它事件处理程序。 无论出于何
我有一个包含 IFrame 的文档(所有源项目都来自同一个域),当我将 onmouseup 事件附加到我的 IFrame 中的一个 div 时,它会在我的鼠标抬起时触发,无论我是否还在我光标所在的元素
我有一个矩形,我正在尝试使用 javascript 移动它。我可以使用函数让它左右移动,但是当我按住鼠标时它不会继续移动。这是我的 html 代码: LEFT RIGHT 这是我的 JavaScrip
我有一个working tool (请参阅第一个代码块),我想在其中包含单击并按住功能。 我想在按住左按钮时快速加,在按住右按钮时快速减。就像它已经具有的单击功能一样,但是是一个更快的选项。 我是一个
我在 Delphi 7 中的 TStringGrid 有一个奇怪的行为。如果弹出菜单与网格关联,Delphi 不会调用 OnMouseUp 事件。基本上,当按下人民币时,菜单的弹出会以某种方式取消/延
我有一个矩形,我正在尝试使用 javascript 移动它。我可以使用函数让它左右移动,但是当我按住鼠标时它不会继续移动。这是我的 html 代码: LEFT RIGHT 这是我的 JavaScrip
我创建了一个简单的 JS slider ,它对 eventListener 的点击使用react onmousedown .但是如果你运行下面的代码,你会看到,toddler没有被 eventList
我想在不删除选项的情况下删除选择。代码如下所示: item 1 如果我在 HTML 中添加一个链接: Remove selection 并且使用 jQuery 一切正常: $('.r
我需要一些帮助: 当我点击最后一个 h2 元素时,我试图移动到另一个页面,但发生的情况是它正在处理所有 h2 元素。 事情是这样的: function ini(){ docu
我有以下 html 结构: high light me with mouse!! highlight me with mouse
我有一个问题,我还没有找到答案。 当我使用此方法捕获鼠标松开事件时: 这是否比这种新方法更有效: document.getElementById("Id").addEventListener("
我构建了一个 javascript 应用程序,用于在同一页面中将 div 从一个地方拖放到另一个地方。我已经制作了拖动部分。我有 div 的坐标,我必须在其中放下 div,但我被困在应该引入条件以匹配
我有一个 div,其中设置了一个 onMouseUp 事件(在 HTML 中)。在该 div 中有许多元素,其中一些包含我想要拖动的 handle 图标。图标有一个 onMouseDown 事件。 当
我使用 onmousedown 事件创建了一个按钮,单击该事件时会循环播放音轨。我试图在单击同一按钮时结束音频循环。我尝试过 onmouseup 事件,但没有运气,有什么想法吗? (我是 JavaSc
我在 JSP 中重构一些 html,我注意到代码有一个按钮的多个 onmouseup 属性。 这是有效的 html 吗,它似乎有效,但它是否有效? 最佳答案 Yes ,您可以有多个事件监听器,不,使用
这是我从 w3schools 获得的示例,我在其中单独使用 safari 浏览器时会出现奇怪的行为。 http://www.w3schools.com/jsref/tryit.asp?filename
我有一个 slider 组件,它应该在鼠标抬起后停止移动。我浏览过论坛,我的代码与 here 非常相似 const Slider = ({ mainColour }) => { const [cu
我只是想问一下在部署时触摸屏移动设备是否支持 onmouseup 和 onmousedown 事件。 我这里有这段代码: 而且我不确定该设备是否支持这些。 我无法测试它,因为我还没有设备。如果不是,
我是一名优秀的程序员,十分优秀!