I'm doing a demo project on Windows Presentation Foundation with C++, and I'm trying to invoke a callback function when the user clicks the mouse anywhere on the screen.
I just have an empty main page:
public ref class MainPage sealed
{
public:
MainPage();
};
I know that I must override OnMouseLeftButtonDown(). However I can't find a sample code anywhere, especially on Microsoft's page about it. In particular, I can't find how to import MouseButtonEventArgs on C++.
Can you help me? Thanks!
Try this to hook up the event handler:
this->MouseLeftButtonDown += gcnew MouseButtonEventHandler(this, &TheEventHandler);
The MouseButtonEventArgs class is in the System.Windows.Input namespace:
using namespace System.Windows.Input;
So the problem was that my project was configured for UWP, not WPF. Silly me...
Thanks for the tips!
Related
I'm working on the WinUI3 desktop application in C++. I was checking how we can get an event when the network to which system is connected to changes. I came across NetworkInformation.NetworkStatusChanged event. But I was not able to find any example of its implementation in C++.
NetworkStatusChangedEventHandler says it has one parameter which is IInspectable object.
i tried
static void NetworkStatusChange(winrt::Windows::Foundation::IInspectable const& sender);
But it gave this error
*\Generated Files\winrt\Windows.Networking.Connectivity.h(2213,81): error C2297: '.*': not valid as right operand has type 'const M'
Can you please help me with help me with how to implement NetworkStatusChanged event in WinUI3 C++ desktop application correctly.
Thank you.
This is how you can do it with C++/WinRT:
add this into the pch.h:
#include <winrt/Windows.Networking.Connectivity.h>
add this somewhere in your code:
// you can instead add a "using namespace Windows::Networking::Connectivity;"
// and use NetworkInformation directly if you prefer
Windows::Networking::Connectivity::NetworkInformation info{};
info.NetworkStatusChanged([=](auto&&...) // sender is not super interesting in this type of event so I've not declared it
{
// do your stuff here
MessageBox(nullptr, L"Something Changed!", L"Network", 0);
});
If you prefer the "raw" C/C++ way, there's an example here: How to detect network change events asynchronously using c++ WinRT
In the Button component we have CustomEventData. In my understanding the purpose it to send extra parameter when user tap the button.
I have tried to use it like this:
use_powerUp(par: string){
console.log("MapScreenManager::use_powerUp() = ", par);
}
The Button will call the function and I thought the CustomEventData will be automatically become 1 parameter that will fill the par parameter. But turns out it didn’t.
Can anyone guide me how to properly use the CustomEventData?
I am using Cocos Creator 3.5.0 with Typescript.
I just found the solution, it's very simple
use_powerUp(event: Event, CustomEventData)
{
}
Hey everyone I am trying to create a Button List in the parent of the buttons but when I wrote;
public List<Button> buttonList;
I get an error how can I fix that ? I want to loop through my buttons and give them listener but like I said I can't write it this way and when I try to write it with GameObject instead of Button I can't onClick and Listener to it.
Did you try to import UI library from Unity Engine. At the top of your script you should add:
using UnityEngine.UI;
I added using UnityEngine.UI and it worked
Add
using UnityEngine.UI;
replace
public List<Button> buttonList;
to
public List<Button> buttonList = new List<Button>();
I am using the library gtkmm. My code is almost perfect, I think because it compiles and I can execute it. But in the terminal when I clicked on open a file in my software that I made with gtkmm I can read this message :
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
So I looked for on this forum how can I solve it and I understood I have to use this method : gtk_window_set_transient_for().
Actually I have a Gtk::Window and a Gtk::FileChooserDialog. Can you put an example which use gtk_window_set_transient_for() ?
Thank you very much !
Gtk::FileChooserDialog and other GTK+ dialogues are derived from Gtk::Window. Gtk::Window has the method set_transient_for(Gtk::Window &parent); which if not set gives you the message you have seen.
To fix this set_transient_for(Gtk::Window &) needs to be used. Note that this takes a reference and not a pointer. So you would use it something like this.
{
Gtk::Window first_window;
...
Gtk::FileChooserDialog file_dialog("Title");
...
file_dialog.set_transient_for(first_window);
...
}
It is also possible to set the transient window for the dialog with the constructor. like so.
{
Gtk::Window first_window;
...
Gtk::FileChooserDialog file_dialog(first_window, "Title");
...
}
If first_window is a pointer the you would need to do something like so.
file_dialog.set_transient_for(*first_window);
Having read through various posts and threads that lead me nowhere I need your help.
I do have a Qt Application for Mac OS X that at some point of use will be in the background and not active. When this is the case I want to add a global hotkey so that the user can easily turn certain features on or off by clicking pre-defined hotkeys.
The following isn't working while the app is in the background and not focused.
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_F12), parent);
shortcut->setContext(Qt::ApplicationShortcut);
So far I found Qxt which happens to be outdated for Qt 5.5.
Then there is DDHotkey which requires a certain compiler which we can not use for various reasons.
Lastly, I found the solution of adding a global AppleScript which registers an event, again, not what I am looking for.
tell application "System Events" to tell process "myApp"
click menu item "myButton" of menu 1 of menu bar item "Menu" of menu bar 1
end tell
Is there a way to use objective-c or cocoa to accomplish exactly what I am looking for?
Please lead me in the right direction if I may have missed something.
Thanks in advance!
To those who seek a more Qt way, check the following repository:
https://github.com/ddqd/qxtglobalshortcut5
It makes use of the outdated qxt library but gets it working again.
The person tested it until Qt 5.4, we use it successfully under Qt 5.5.
This might be what you're looking for
https://github.com/jaz303/JFHotkeyManager
You could also look at this example from Apple, using the RegisterEventHotKey API call which I think will point you in the right direction.
https://developer.apple.com/library/prerelease/mac/samplecode/FunkyOverlayWindow/Listings/FunkyOverlayWindow_OverlayWindow_m.html#//apple_ref/doc/uid/DTS10000391-FunkyOverlayWindow_OverlayWindow_m-DontLinkElementID_8
Or you could try this code
#import <Carbon/Carbon.h>
EventHandlerUPP hotKeyFunction;
pascal OSStatus hotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData)
{
Notify *obj = userData;
[obj foo];
return noErr;
}
#implementation Notify
- (id)init
{
self = [super init];
if (self) {
//handler
hotKeyFunction = NewEventHandlerUPP(hotKeyHandler);
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyReleased;
InstallApplicationEventHandler(hotKeyFunction,1,&eventType,self,NULL);
//hotkey
UInt32 keyCode = 80; //F19
EventHotKeyRef theRef = NULL;
EventHotKeyID keyID;
keyID.signature = 'FOO '; //arbitrary string
keyID.id = 1;
RegisterEventHotKey(keyCode,0,keyID,GetApplicationEventTarget(),0,&theRef);
}
return self;
}
- (void)foo
{
}
#end
And the header
#include "notify.mm"
#interface Notify
- (id)init;
- (void)foo;
#end
Simply this is just a object with a method and a constructor, in objective-c this is called init, or initialize, and variants. Calling it should be straight forward with "new".
E.x
#include "notify.h"
int main(){
Notify* object = new Notify();
}
However, some basic understanding of Objective-C is needed. It's mostly syntax differences in my opinion. But I'm no Objective-C expert myself. Anyway, there is a lot of ways to solve it, this might not be the best idea. You can also call Objective-C code from inside of a C++ class of yours. Take a look at the links bellow for a great example of how that's done.
https://el-tramo.be/blog/mixing-cocoa-and-qt/
https://github.com/remko/mixing-cocoa-and-qt/
http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++