How can I stop mfc dialog application closing by pressing ESC (Escape key).
After executing my application if I press ESC key then the window is closed.
How can this be stopped?
I am using VC++ 6.0.
You can override the OnCancel event and only move forward with the OnCancel call if IDCANCEL is the focused item.
void CMyDialog::OnCancel(void)
{
if(GetDlgItem(IDCANCEL) == GetFocus())
{
CDialog::OnCancel();
return;
}
}
There are different ways to do this. You can:
Create an OnCancel Handler and do whatever you want with the Cancel notification
You can Handle OnClose Event and do whatever you want.
You can override PreTranslateMessage and check Esc key there and do whatever you want.
Check this for code examples.
For a PreTranslateMessage example, see this
Override OnCancel and don't call the base class implementation.
Don't go near OnClose unless you know what you're doing, you risk breaking the behaviour for Alt-F4 and the X button.
I've always regarded PreTranslateMessage for things like this as using a thermo-nuclear weapon to crack a nut, but if it floats your boat...
Assuming we're dealing with a top-level window implemented as a CDialog subclass here, that window can receive two "kinds" of close events:
Application close events (WM_SYSCOMMAND with an ID of SC_CLOSE)
Window close events (WM_COMMAND with an ID of IDOK or IDCANCEL)
MFC, however, effectively routes the former class of events through CDialog::OnCancel by default when they are sent to a dialog, which means that overriding OnCancel also breaks Alt-F4 and the X button. This means that in order to distinguish between the two, you need to handle the former events in OnSysCommand while using overrides of OnOK and OnCancel to handle the latter.
The resulting code looks something like this:
class CTopLevelDlg: public CDialog
{
afx_win void OnSysCommand(UINT id, LPARAM lparam) override
{
if (id == SC_CLOSE)
CDialog::OnCancel();
}
void OnOK() override {}
void OnCancel() override {}
};
Related
I am trying to set up a MFC C++ App in Visual Studio 2019 such that modifies the user's text as they are typing.
Current layout is 2 radio buttons,
ID= rdbOn (set to Group = True, with Value int variable m_isOn = 1)
ID= rdbOff, m_isOn value would be = 0
and 1 Edit Control,
ID= txtInputBox, with Value CString variable m_inputString
Currently, for testing I can see how it would work for a button on click, it would take something like the following and just SetDlgItemText of the result. But that would be after they have typed, not WHILE they are typing.
void Onsomebtnclick()
{
//convert CString to String of m_inputString
//do some string manipulation
//convert back to CString
//SetDlgItemText(txtInputBox, result)
}
Update:
got EN_CHANGE to work
I was able to get EN_CHANGE working with the flag suggestion from user #GoGoWorx. However, now I just have a slight problem that the cursor is back to the beginning of the edit control txtInput.
I'm reading about using a CEdit::SetSel but don't know how to use that directly in my code. I tried
CEdit control MFC, placing cursor to end of string after SetWindowText
someDlg::someFunction()
{
//some logic stuff to get a result string
SetDlgItemText(txtInputBox, result);
//need it to set the cursor to the end
//I tried these, but it didn't recognize (expression must have class type?)
//txtInputBox.SetSel(0, -1);
//txtInputBox.SetSel(-1);
}
It sounds like you need to use the ON_EN_CHANGE message-map notification (called after the control has been updated due to typing or pasting for example)
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_EN_CHANGE(IDC_EDIT_CONTROL, &CMyDialog::OnEnChangeEditControl)
END_MESSAGE_MAP()
void CMyDialog::OnEnChangeEditControl()
{
// Copy or call your Onsomebtnclick() here
}
I'm not sure what you're using for the numeric identifier for the edit control, since these are typically upper case defines - replace IDC_EDIT_CONTROL above with your define (possibly txtInputBox, but again, these are normally upper case, so I'm not sure).
Also change CMyDialog for the name of your dialog class too.
Note that we're using the ON_EN_CHANGE message-map handler here instead of the ON_EN_UPDATE, since the ON_EN_CHANGE message is sent after the control has been updated, whereas ON_EN_UPDATE is called just before it's updated.
The message-map handlers are described in the Remarks section of the CEdit control documentation: https://learn.microsoft.com/en-us/cpp/mfc/reference/cedit-class?view=msvc-160
Regarding your concern about modifying things as the user types - this should be fine, since every change (keystroke or paste from clipboard, etc.) should trigger this handler to be called, where you can change whatever you need. Just be sure that when you're updating the control, you don't trigger the ON_EN_CHANGE again and end up in a recursive 'change' loop.
You might be able to do this with some sort of flag to indicate you're the one updating the control, as opposed to the user, however it's probably better to subclass the CEdit control to do what you're wanting. There are a few examples out there of how to do this (it's not as difficult as it might sound), for example:
https://www.codeproject.com/Articles/27376/Avoiding-EN-CHANGE-notifications
In WPF, there is a Closing event on <Window... tag, where we can hook up some dispose code in MainWindow_OnClosing.
But there is no such event in UWP app. The closing I guess is Unloaded, not even Unloading is there.
I just placed my disposing code there but still feel concerned. Is Unloaded event supposed for this type of work? Is there something I need to take note?
According to the MSDN, the Window class has a Closed event.
I'm mentioning this as you posted the Closing event of a window component, but keep in mind the remark of the event:
The Closed event occurs when a Window closes. However, Windows Store
apps typically use a single Window instance, and do not open and close
additional Window instances.
Now, when using the Frame navigation system of the main window with Pages, I advise you to use the OnNavigatedTo and OnNavigatedFrom events to manipulate all initialisation and dispose functionality of the class.
You may want to pay attention to the OnNavigationFrom as it is invoked immediately before the Page is unloaded and is no longer the current source of a parent Frame.
A really simple example:
Windows.ApplicationModel.Resources.ResourceLoader loader;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
loader = new Windows.ApplicationModel.Resources.ResourceLoader();
var navigationPageContentFormat = loader.GetString("NavigationPageContent");
var navigationPageContentWhenEmpty = loader.GetString("NavigationPageContentWhenEmpty");
this.ParameterTextBlock.Text = String.Format(navigationPageContentFormat, e.Parameter?.ToString() ?? navigationPageContentWhenEmpty);
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
loader = null;
}
Nowadays, I am working on a java swing application using DJ NativeSwing as my embed browser to do something automatic work. The scenario of my application is that a user click start button and the embed browser auto click some position of the current page and then redirect to another one and then execute some other operations like click or something other. Now here is my solution. First, I will define a webbrowser class (extends JWebBrowser) and webbrowser listener class (implements WebBrowserListener), which represents a webbrowser and contains loadingProgressChanged, windowOpening and so on separately. Second, I define a thread class to do some logic computing and execute my browser operations as mentioned above by webbrowser.executeJavascript. At last, I add mouseListener for start and stop button to start or stop the task. When I open my application, the JFrame add the browser and its listener class. I click the start button, the browser works and will click the target position as I expected and then the page will redirect to another one. As we all know, js code can’t be executed until the page was loaded completely. So I set a global flag to check whether the page has loaded completely or not in loadingProgressChanged (code:if(e.getWebBrowser().getLoadingProgress() == 100)globalflag = true;) within webbrowser listener class. And in the thread class, I use code( while(globalflag == false){Thread.sleep(500);}) after first click to detect if the current page was loaded completely. However, when browser first click the target position and the page redirects successfully, I find that the current page has changed but blocked. After some debugging, I find it. In my thread class, browser will execute js code by webbrowser.executeJavascript("document.getElementById(‘target’).click();") to click the target position and then java code (while(globalflag == false){Thread.sleep(500);}) to detect if the current page was loaded completely and then execute some other java code. I find that the globalflag will never change and the current page’s loadingProgressChanged listener will never work because the java code (while(globalflag == false)). Because after I remove all java code after the first webbrowser.executeJavascript("document.getElementById(‘target’).click();"), the current page’s loadingProgressChanged listener works and the page was not blocked. With the DJ NativeSwing demo, I could execute my js in the loadingProgressChanged. However, I want to do a series of operations with my browser and also want to stop the task whenever need. So, I prefer to my solution for my demand rather than the provided one by demo. So, I wonder that after webbrowser.executeJavascript the DJ NativeSwing thread will wait my thread? And, in my software architecture, does anyone could have any suggestions? Thanks a lot. Any suggestion is appreciated!
PS.my application works fine with jdic-0.9.5, but it supports IE7 only.
I paste my code here to demonstrate my problem:
After I click the start button in JFrame, I will new a thread as follow
public class MyVisit implements Runnable{
private void doClick1(){
webbrowser.executeJavascript("document.getElementById('n').value='test'");
webbrowser.executeJavascript("document.getElementById('f').submit()");
}
public void run() {
globalFlag=false;
webbrowser.executeJavascript("document.getElementById(‘t’).click();") ;
while(!globalFlag){
Thread.sleep(500);
}
this.doClick1();
}
}
listener:
public class MyListener implements WebBrowserListener {
…
public void loadingProgressChanged(WebBrowserEvent e) {
if (e.getWebBrowser().getLoadingProgress() == 100) {
globalFlag=true;
}
}
}
DJ Native Swing enforces the Swing approach to threading: manipulate the components in the AWT Event Dispatch thread (a.k.a. UI thread). But also, do not block that thread, because listeners are triggered in that thread too.
So, you should not have a "while" loop in the UI thread, but should instead spawn a new thread. I hope your global variable is volatile, or AtomicBoolean or all accesses to it protected by synchronized block, otherwise you might have interesting threading side effects.
I am developing an application that requires me to override keypressed and released methods in lwuit so as to map specific functons into gamekeys.
The gamekeys work fine when I do this but I'm having trouble adding more than two commands to application. Normally actionlistener would automatically handle the mapping of multiple commands but it does not.
Is it possible to map more than one command to a softkey (like with keycode -7) and have an if statement to dynamically check which command is pressed.
public void keyReleased(int keyCode) {
switch (keyCode) {
case -6: // left cmd key
function a();
return;
case -7: // right cmd key
//Need this to handle more than one command function
return;
}
//function to handle gamekeys
}
Hope I am clear enough with my issue. Please help
In my opinion what you wanna do is not possible. There are always a cancel command in a softkey and the menu (if there are more than one command added) in the other softkey.
To get the correct keyCode, to set another funtionallity to the softkey you should get the correct keyCode. Make a System.out.println("keycode " + keyCode); before the first line in the method keyReleased
You need to replace the MenuBar class if you want to do custom key/menu handling. Just subclass the MenuBar and define your new class within the LookAndFeel.
I have DLL which attaches to non-QT application. In DLL I create QThread and QApplication instances and run event loop. And I have QDialog and QTimer for hiding dialog after 4 sec.
Also I have overridden mousePressEvent but it's didn't work properly. Overridden function is called only when I resize dialog. If I don't do resizing, dialog is hiding and mousePressEvent function didn't call.
Thread:
void MThread::run()
{
MThread::_app = new QApplication(_argc, _argv);
_main_window = new SongTitle();
_main_window->SetTitle(QString::fromLocal8Bit("lalala"));
_main_window->ShowTitle();
MThread::_app->exec();
}
Mouse event:
void SongTitle::mouseReleaseEvent(QMouseEvent *event)
{
MessageBoxW(0, L"in mouse event",L"",0);
switch (event->button()){
case Qt::LeftButton:
MessageBoxW(0, L"left",L"",0);
break;
case Qt::RightButton:
MessageBoxW(0, L"right",L"",0);
break;
default:
QDialog::mousePressEvent(event);
break;
}
}
How to fix this behavior?
You seem to be overriding the mouse release event, not the press event. Also there's no reason to use windows APIs like MessageBox when you've got Qt equivalents like QMessageBox.
Most likely you miss the fact that a widget will only get the events that haven't been used up by other widgets that are on top of it. Your SongTitle dialog likely has child widgets, and you are clicking on those widgets, not on SongTitle itself.
If you want to get mouse events as they come to all of widget's children, you can install an event filter on all child widgets. That event filter will receive all events as they come in.