gpt4 book ai didi

c++ - 同步 : Detecting Pointer/Clicks

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:22 26 4
gpt4 key购买 nike

我正在尝试检测何时单击/指向/点击标签。我来自 Win32 C++ 和 Java Swing 编程,我知道这两种方法都采用不同的方法来注册事件/输入。

我看过教程,但找不到检测点击的示例。单击是否有常量,这样我就可以在 keyPressEvent 中检测到它(即,像 win32 和 WM_LBUTTONDOWN)?或者我是否需要先注册点击然后调用我自己的函数来处理点击(如 Java 和 .addActionListener())?

我尝试检测下面的点击不起作用:

#include <MAUtil/Moblet.h>
#include <MAUI/Layout.h>
#include <MAUI/ListBox.h>
#include <MAUI/Label.h>
#include <MAUI/EditBox.h>
#include <MAUI/Screen.h>
#include <MAUtil/Environment.h>
#include <madmath.h>
#include <conprint.h>


using namespace MAUtil;
using namespace MAUI;

class MouseScreen : public Screen, public PointerListener
{
private:
Label *testLabel;
public:
MouseScreen()
{
MAExtent screenDim = maGetScrSize();
Layout* mainLayout = new Layout( 0, 0, EXTENT_X(screenDim), EXTENT_Y(screenDim), NULL, 1, 3 );
ListBox* mainListBox = new ListBox( 0, 0, 100, 200, mainLayout,
ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR,
true );
mainListBox -> setPaddingLeft( 10 );
mainListBox -> setPaddingRight( 10 );
mainListBox -> setPaddingTop( 10 );
mainListBox -> setPaddingBottom( 10 );
mainListBox -> setBackgroundColor( 900 );
mainLayout -> setBackgroundColor( 300 );

testLabel = new Label( 10, 300, 50, 20, mainLayout );
//testLabel -> addPointerListener( this );
testLabel -> setCaption( "Click me" );

mainLayout -> add( testLabel );
}

void pointerPressEvent( MAPoint2d p )
{
printf( "clicked" ); // never occurs

// OR
if ( testLabel.contains((MouseScreen*)p) )
{
printf( "Label clicked" );
}
// Should I call parent function
// PointerListener :: pointerPressEvent( p );
}

void pointerMoveEvent( MAPoint2d p ) {}
void pointerReleaseEvent( MAPoint2d p ) {}
};

class MouseMoblet : public Moblet
{
public:
MouseMoblet()
{
instance = new MouseScreen();
instance -> show();
}

~MouseMoblet()
{
delete instance;
}

void keyPressEvent(int keyCode, int nativeCode)
{
// todo: handle key presses
printf( "Blah" ); // never occurs when I press the mouse, but other KEYS work
}

void keyReleaseEvent(int keyCode, int nativeCode)
{
// todo: handle key releases
}

private:
MouseScreen *instance;
};

extern "C" int MAMain()
{
Moblet::run(new MouseMoblet());
return 0;
};

最佳答案

我可以看到您需要做的一些事情。首先,您需要通过调用 setMain(mainLayout) 将 mainLayout 设置为 Screen 的主要 widget。这使屏幕知道 mainLayout 以便它可以绘制它。完成此操作后,您将能够在屏幕上看到您的小部件,您还应该获得点击事件。

在 pointerPressedEvent 中,您几乎是正确的,testLabel 的 contains 方法采用点而不是屏幕。你在这里应该做的是调用 testLabel->contains( p.x, p.y ) 来评估 testLabel 是否被点击。

完整修改后的代码如下所示:

#include <MAUtil/Moblet.h>
#include <MAUI/Layout.h>
#include <MAUI/ListBox.h>
#include <MAUI/Label.h>
#include <MAUI/EditBox.h>
#include <MAUI/Screen.h>
#include <MAUtil/Environment.h>
#include <madmath.h>
#include <conprint.h>


using namespace MAUtil;
using namespace MAUI;

class MouseScreen : public Screen
{
private:
Label *testLabel;
public:
MouseScreen()
{
MAExtent screenDim = maGetScrSize();
Layout* mainLayout = new Layout( 0, 0, EXTENT_X(screenDim), EXTENT_Y(screenDim), NULL, 1, 3 );
ListBox* mainListBox = new ListBox( 0, 0, 100, 200, mainLayout,
ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR,
true );
mainListBox -> setPaddingLeft( 10 );
mainListBox -> setPaddingRight( 10 );
mainListBox -> setPaddingTop( 10 );
mainListBox -> setPaddingBottom( 10 );
mainListBox -> setBackgroundColor( 900 );
mainLayout -> setBackgroundColor( 300 );

testLabel = new Label( 10, 300, 50, 20, mainLayout );
//testLabel -> addPointerListener( this );
testLabel -> setCaption( "Click me" );

mainLayout -> add( testLabel );

setMain( mainLayout );
}

void pointerPressEvent( MAPoint2d p )
{
if ( testLabel->contains( p.x, p.y ) )
{
printf( "Label clicked" );
}
}

void pointerMoveEvent( MAPoint2d p ) {}
void pointerReleaseEvent( MAPoint2d p ) {}
};

class MouseMoblet : public Moblet
{
public:
MouseMoblet()
{
instance = new MouseScreen();
instance -> show();
}

~MouseMoblet()
{
delete instance;
}

void keyPressEvent(int keyCode, int nativeCode)
{
// todo: handle key presses
printf( "Blah" ); // never occurs when I press the mouse, but other KEYS work
}

void keyReleaseEvent(int keyCode, int nativeCode)
{
// todo: handle key releases
}

private:
MouseScreen *instance;
};

extern "C" int MAMain()
{
Moblet::run(new MouseMoblet());
return 0;
};

注意:当前的 MoSync UI 系统是为非触摸手机设计的,因此处理指针事件有点做作,不过这将在即将发布的版本中得到改进。

关于是否调用父函数取决于是否要保留默认行为,目前 Screen 的默认实现不执行任何操作。

关于c++ - 同步 : Detecting Pointer/Clicks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5253485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com