Visual C++ Detect a Right Click on a Button - c++

I have a program where I want a right click on a button to do a completely different amount of code. I have the code display a messagebox for the example, but eventually it will just be a method call each. I'll show you the context in which I need it. Any and all help on how to detect a right click will help. Here's the snippet of code I have:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ buttonName = safe_cast<Button^>(sender)->Name;
safe_cast<Button^>(sender)->Enabled = false;
if(/*Insert Code Here To Detect a right click*/)
MessageBox::Show("Right Click");
else
MessageBox::Show("Left Click");
MessageBox::Show(buttonName);
}
};

You can use the MouseDown event and check if it is right
void button1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
{
// Update the mouse path with the mouse information
Point mouseDownLocation = Point(e->X,e->Y);
String^ eventString = nullptr;
switch ( e->Button )
{
case ::MouseButtons::Left:
eventString = "L";
break;
case ::MouseButtons::Right:
eventString = "R";
break;
case ::MouseButtons::Middle:
eventString = "M";
break;
case ::MouseButtons::XButton1:
eventString = "X1";
break;
case ::MouseButtons::XButton2:
eventString = "X2";
break;
case ::MouseButtons::None:
default:
break;
}
//Process Here...
}

Related

How can I tab away from a Fl_Button in FLTK?

I have a custom class, CustomButton that extends Fl_Button. On my screen there are a bunch of Fl_Input and CustomButton widgets that I want to be able to navigate between using a tab keypress. Tabbing between input fields works fine, but once a CustomButton gets focus, I can't seem to tab away from it.
Here's my handle function
int CustomButton::handle ( int event )
{
int is_event_handled = 0;
switch (event)
{
case FL_KEYBOARD:
// If the keypress was enter, toggle the button on/off
if (Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter)
{
// Do stuff...
}
is_event_handled = 1;
break;
case FL_FOCUS:
case FL_UNFOCUS:
// The default Fl_Button handling does not allow Focus/Unfocus
// for the button so mark the even as handled to skip the Fl_Button processing
is_event_handled = 1;
break;
default:
is_event_handled = 0;
break;
}
if ( is_event_handled == 1 ) return 1;
return Fl_Round_Button::handle ( event );
}
I am using fltk 1.1.10.
For a very simple, minimal example which demonstrates how to control the focus, please check the navigation.cxx test file.
Maybe your widget did get the focus (check this with Fl::focus()), but it does not show that up (you need to handle the FL_FOCUS and/or FL_UNFOCUS events)?
My problem was my CustomButton::handle() returning 1 after a FL_KEYBOARD event without actually using the tab key press.
Moving is_event_handled = 1 into the if statement lets me consume the FL_Enter keypress only and lets other widgets (i.e. the Fl_Group that controls navigation) take any other keypresses.
Alternatively get rid of the if and replace with something like
switch(Fl::event_key())
{
case FL_Enter:
case FL_KP_Enter:
// Do stuff
is_event_handled = 1;
break;
default:
is_event_handled = 0;
break;
}

Inserting a child Item into a row of QTreeWidgetItems

I have setup my QTreeWidget such that each cell is filled with comboboxes, however I would like to create a text edit widget next to the selected combobox (or overwrite the existing combobox), depending on which combobox item the user has selected.
I figured I could do this by adding the comboboxes parent as a property when its initially setup and then upon interaction with the combobox simply use setItemWidget to put a text edit Item in column after the selected combobox (using the same child). But it seems as though the parent is not being passed correctly or some other issue is causing the text edit widget to appear in the row below where it should be.
I've attached a photo and some code for clarification.
This is where I setup the comboboxes for the QTreeWidget (Specifically where rowType == 0)
void customMethodConstructorWindow::addChildRow(QTreeWidget *widgetParent,QTreeWidgetItem *itemParent,int rowType)
{
//widgetParent->setColumnCount(methodBlocks.at(rowType).size());
if(rowType == 0)
{
QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);
QVariant itemParentVariant;
itemParentVariant.setValue(itemParent);
for(uint cycleSetup = 0;cycleSetup < methodBlocks.at(rowType).size();cycleSetup++)
{
QComboBox *itemComboBox = new QComboBox;
itemComboBox->setProperty("rowType", rowType);
itemComboBox->setProperty("row", 0);
itemComboBox->setProperty("column",cycleSetup);
itemComboBox->setProperty("itemParent",itemParentVariant);
itemComboBox->addItems(methodBlocks.at(0).at(cycleSetup));
QObject::connect(itemComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
widgetParent->setItemWidget(childItem,cycleSetup,itemComboBox);
itemParent->addChild(childItem);
}
}
else
{
QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);
QComboBox *item0ComboBox = new QComboBox;
item0ComboBox->setProperty("rowType", rowType);
item0ComboBox->setProperty("row", 1);
item0ComboBox->setProperty("column", 0);
item0ComboBox->addItems(methodBlocks.at(1).at(0));
QObject::connect(item0ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
widgetParent->setItemWidget(childItem,0,item0ComboBox);
itemParent->addChild(childItem);
QComboBox *item1ComboBox = new QComboBox;
item1ComboBox->setProperty("rowType", rowType);
item1ComboBox->setProperty("row", 1);
item1ComboBox->setProperty("column", 1);
item1ComboBox->addItems(methodBlocks.at(1).at(rowType));
QObject::connect(item1ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
widgetParent->setItemWidget(childItem,1,item1ComboBox);
itemParent->addChild(childItem);
}
}
And this is where I try to create a text edit widget over one of the comboboxes
void customMethodConstructorWindow::OnComboIndexChanged(const QString& text)
{
QComboBox* combo = qobject_cast<QComboBox*>(sender());
if (combo)
{
int nRow = combo->property("row").toInt();
int nCol = combo->property("column").toInt();
switch (nRow) {
case 0:
{
switch (nCol)
{
case 0:
{
//combo->setVisible(false);
//testPoint = combo->pos();
}
break;
case 1:
{
if(combo->currentIndex() != 0)
{
QTreeWidgetItem *childItem = new QTreeWidgetItem(combo->property("itemParent").value<QTreeWidgetItem*>());
QTextEdit *textItemEdit = new QTextEdit;
//QObject::connect(item1ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
ui->methodSetupTreeWidget->setItemWidget(childItem,2,textItemEdit);
}
}
break;
case 2:
//customObjectAttributes.comparisonType.push_back(combo->currentIndex());
break;
case 3:
{
//customObjectAttributes.valueB.push_back(combo->currentIndex());
}
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
//ui->logicSetupTableWidget->setRowCount(ui->logicSetupTableWidget->rowCount()+1);
if(combo->currentIndex() != 3)
{
//addComboRow(ui->logicSetupTableWidget->rowCount()-1);
}
//customObjectAttributes.outputType.push_back(combo->currentIndex());
break;
}
}
break;
case 1:
{
switch (combo->property("column").toInt())
{
case 0:
break;
case 1:
{
addChildRow(ui->methodSetupTreeWidget,ui->methodSetupTreeWidget->itemAt(QPoint(2,1)),0);
addChildRow(ui->methodSetupTreeWidget,ui->methodSetupTreeWidget->itemAt(QPoint(3,1)),1);
}
break;
case 2:
{
}
break;
case 3:
{
}
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
{
if(combo->currentIndex() != 3)
{
//addComboRow(ui->logicSetupTableWidget->rowCount()-1);
}
}
break;
}
}
break;
}
}
}
ScreenShot of current situation
My issue here is that the text edit shows up below the less than (<) combobox rather than ontop (or replacing it).
I was able to find a solution using QStandardItem:setData() and QStackedWidget to get the functionality I was after.
This was done in the process of trying to solve a related issue here.

SFML/C++ Simulating user input

I'm trying to write some unit test methods. I want to test method which checks if key is pressed or released. And here is my question:
Is it possible in SFML with C++ to simulate random key press on keyboard?
Or I will just have to trust myself that this works?
The internals of sf::Keyboard::isKeyPressed would make simulation difficult, but if you are looking to simulate KeyPressed or KeyReleased events I'm pretty sure the following should work:
const sf::Event simulateKeypress(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system)
{
sf::Event::KeyEvent data;
data.code = key;
data.alt = alt;
data.control = control;
data.shift = shift;
data.system = system;
sf::Event event;
event.type = sf::Event::KeyPressed;
event.key = data;
return event;
}
//your handler here
handleEvent(simulateKeypress(sf::Keyboard::Key::A, false, false, false, false));
I'm unable to test this at the moment... If it works, then you should be able to make similar functions for other events.
Sure you can if you use a key manager of your own.
An example in C with SDL (but its exactly the same in C++ with SFML, just few name to change) :
typedef struct
{
char key[SDLK_LAST];
int mousex,mousey;
int mousexrel,mouseyrel;
char mousebuttons[8];
char quit;
} Input;
void UpdateEvents(Input* in)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
in->key[event.key.keysym.sym]=1;
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym]=0;
break;
case SDL_MOUSEMOTION:
in->mousex=event.motion.x;
in->mousey=event.motion.y;
in->mousexrel=event.motion.xrel;
in->mouseyrel=event.motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
in->mousebuttons[event.button.button]=1;
break;
case SDL_MOUSEBUTTONUP:
in->mousebuttons[event.button.button]=0;
break;
case SDL_QUIT:
in->quit = 1;
break;
default:
break;
}
}
}
int main()
{
Input in;
memset(&in,0,sizeof(in));
while(!in.key[SDLK_ESCAPE] && !in.quit)
{
UpdateEvents(&in);
if (in.mousebuttons[SDL_BUTTON_LEFT])
{
in.mousebuttons[SDL_BUTTON_LEFT] = 0;
}
if (in.key[SDLK_UP] && in.key[SDLK_LEFT])
{
}
}
return 0;
}
Edit : Found a key manager for C++ SFML : https://github.com/dabbertorres/SwiftInputManager
Can't give you my own code cause the code is not in english

How to change the state of button to clicked in cocos2d-x

I am pretty new to cocos2d-x.I have created a button and i wanted to change the state of the button when i tap the button . i am having trouble changing the state from play to pause similar to a music player.Below is the code.
void Gallery::buttonUI(Size visibleSize,Vec2 origin)
{
button = Button::create("play.png");
//button->loadTextures("pause.png","play.png","pause.png");
button->setPosition(Point((visibleSize.width/2)+origin.x,(visibleSize.height/2)+origin.y-80));
button->setContentSize(Size(100.0f, button->getVirtualRendererSize().height));
button->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
switch (type)
{
case Widget::TouchEventType::BEGAN:
break;
case Widget::TouchEventType::ENDED:
CCLOG("Characters: %c %c", 'a', 65);
if (!flag)
Gallery::pauseSong();
else
Gallery::resumeSong();
break;
default:
break;
}
});
this->addChild(button);
}
void Gallery::playSong()
{
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("1.mp3");
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("1.mp3");
flag = false;
}
void Gallery::pauseSong()
{
CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
flag = true;
}
void Gallery::resumeSong()
{
CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
flag = false;
}
I don’t know of such methods for the ui::Button. But I don’t see the use of specific ui::Button items (capinsets, different methods for different touch events etc.) in your method also.
So, I think the MenuItemImage is better in your case:
bool flag = true;
MenuItemImage *button = MenuItemImage::create("play.png", "play_pressed.png", CC_CALLBACK_0(Gallery::playSong, this));
button->setPosition(Vec2((visibleSize.width/2)+origin.x,(visibleSize.height/2)+origin.y-80)); // better is use Vec2, Point can be ambiguous
Menu* menu = Menu::create(button, NULL); // add created button on Menu
menu ->setPosition(0,0);
this->addChild(menu);
And then set the images in handler pressing:
void Gallery::playSong()
{
if(flag)
{
// preload better move to AppDelegate.cpp
// CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("1.mp3");
flag = false;
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("1.mp3");
button->setNormalImage(Sprite::create(“pause.png”));
button->setSelectedImage(Sprite::create(“pause_pressed.png”)); // if you use selected image
}
else
{
flag = true;
CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
button->setNormalImage(Sprite::create(“play.png”));
button->setSelectedImage(Sprite::create(“play_pressed.png”));
}
}
In cocos 3.x use property: setHighlighted(bool)
Example:
When press a key: button->setHighlighted(true);
When release the key:
button->setHighlighted(false);

.NET/WinForms Custom Form Border with Minimize Animation

I know I've seen this asked before, but the answers have been inconclusive or unsatisfactory (from what I could see), so I'd like to pose my situation here. I'm creating a program that has a custom form border (i.e. Form Border Style = None with our own controls around it). The program doesn't have the minimize/close animations, but just snaps, instead.
It is a .NET form (using C++/CLR) - is there something I can do? I've seen other programs do this (for example, Photoshop CS6/CC have the restore animation, but not the minimize one).
Is there a property/style I can apply by overriding the CreateParams? I'm game for "hacky" methods, but changing the form border style in a way that lets the user temporarily see the border isn't a viable option here.
Thanks in advance.
I ended up using a custom animation and figured I would share the relevant code. The following override of the WndProc catches minimizes/restores (including from clicking/double clicking in the taskbar). Be aware, setting the WindowState WILL NOT trigger this - you have to manually send the SC_MINIMIZE lpa with the WM_SYSCOMMAND to the window (or manually animate it). The entire animation code, timer included is below.
//Define a variable so it knows what animation is happening
short fade_mode = 0; //0 is fade in, 1 is minimize, 2 is close
short close_on_close = FALSE; //a variable to tell the close handler to re-animate or not - this allows this->Close(); to trigger the animation but avoids a loop.
//The WndProc
protected: virtual void WndProc(System::Windows::Forms::Message% msg) override {
switch (msg.Msg) {
case WM_SYSCOMMAND:
switch (msg.WParam.ToInt32()) {
case SC_MINIMIZE:
msg.Result = IntPtr::Zero;
fade_mode = 1;
fadetimer->Start();
return;
break;
}
break;
case WM_ACTIVATE: {
if (HIWORD(msg.WParam.ToInt32()) == 0) { //because non-zero wpa here means the form is minimized
this->WindowState = FormWindowState::Normal;
fade_mode = 0;
fadetimer->Start();
msg.Result = IntPtr::Zero;
return;
}
}
}
Form::WndProc(msg);
}
//The button event handlers
private: System::Void btn_close_Click(System::Object^ sender, System::EventArgs^ e) {
this->Close();
}
private: System::Void btn_minimize_Click(System::Object^ sender, System::EventArgs^ e) {
SendMessage(HWND(this->Handle.ToPointer()), WM_SYSCOMMAND, SC_MINIMIZE, NULL);
}
//The event animation code itself (set to a tick of 10ms) and the form closing handler:
private: System::Void fadetimer_Tick(System::Object^ sender, System::EventArgs^ e) {
if (this->IsDisposed == true) { //In the event that the form opened/closed quickly and has not stopped properly, clean up to avoid crashes.
fadetimer->Stop();
return;
}
switch (fade_mode) {
case 0: //fading in
if (this->Opacity < 1)
this->Opacity += 0.2;
else {
fade_mode = -1;
fadetimer->Stop();
}
break;
case 1: //minimizing
if (this->Opacity > 0)
this->Opacity -= 0.2;
else {
fade_mode = -1;
fadetimer->Stop();
this->WindowState = Windows::Forms::FormWindowState::Minimized;
}
break;
case 2: //closing
if (this->Opacity > 0)
this->Opacity -= 0.2;
else {
fade_mode = -1;
fadetimer->Stop();
close_on_close = TRUE;
this->Close();
}
break;
}
}
private: System::Void loginform_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
if (close_on_close == FALSE) {
e->Cancel = true;
fade_mode = 2;
fadetimer->Start();
}
}
Be sure to set your form's opacity to 0% by default - it should automatically fade in when it's first created/shown (mine does, I can't remember if I've done something else that makes it so).