Disable event handler in Visual Studio MFC based application - c++

I am working on a project. It has GUI and i add Start button on it , which is handled by some function. and after clicking on start ,that Gui shows the output. I want to disable that handler function. Whenever i debug that project, start button should automatically started and GUI shows the output.
This is the code of that handler. What should i change or move that function ?
void CServerSocketDlg::OnBtnStart()
{
UpdateData();
StartX();
}
Need your suggestion. Thanks

So add something like this in an appropriate location. For example, in a dialog, you could put it in OnInitDialog:
#ifdef _DEBUG
if(IsDebuggerPresent())
{
/* code here to automatically do whatever you need when a debugger is attached */
}
#endif
This code will only be compiled in the "Debug" versions of the application and will execute only if the application is running under the debugger.

Related

What is the entry point of this basic MFC Dialogue Box Application?

Starting out with GUI programming with C++. So, following some tutorials, I 'wrote' the following code to display a dialogue box. To be honest, the Visual Studio 2015 Wizard did most of the job, but here is the code file. It correctly displays the dialogue box pointed by the identifier, but I really cannot figure out how it works. To begin with, what is the entry point, of the code? There is not even a function, so what exactly executes when I build and run it?
#include<afxwin.h>
#include"resource.h"
class CExampleDlg :public CDialog
{
public:
CExampleDlg():CDialog(IDD_EXAMPLE_DLG){}
~CExampleDlg(){}
};
class CExample:public CWinApp
{public:
BOOL InitInstance()
{
CExampleDlg myDlg;
m_pMainWnd = &myDlg;
myDlg.DoModal();
return TRUE;
}
};
CExample MyApp;
Unlike normal c/c++ application where the entry point is main and you have full control over the flow of execution. MFC applications are event driven. The code you write is executed based on the events that occur due to user interaction with the application like, clicking on the button, entering text in the text box etc. When there is no interaction the application sits idle.
1) The best place to is OnInitDialog to place your initialization code. You can initialize all the member variables in OnInitDialog. (Remember winMain is the entry point for windows application. But in MFC this is embedded deep down in the boilerplate code.)
2) Add message handlers to handle the user actions to execute your core logic later. For e.g.: If you have a button on the dialog then you need to add the message handler function for the button which will get invoked when the user clicks on that button. This can be done easily using the class wizard (https://msdn.microsoft.com/en-us/library/ee748520.aspx).

Qt Mac - App not restoring after closing

In Mac when we press the close button apps by default hide to dock. The same happens with my Qt app too, but it does not restore afterwards as expected. There are many posts which provide code to detect click on dock icon.
I don't need to detect, I just want the default functionality, that it should restore.
It restores if I minimize but not on close.
The fix I used is:
void myAPP::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_MAC
event->ignore();
this->setWindowState(Qt::WindowMinimized) ;
return;
#endif
}
It makes it minimize on close and than my app restores properly. But now the issue is that it is preventing shutdown on Mac unless I exit the application. Shutdown makes the app minimize.
I am using Qt 5.4
To fix the issue, I wanted to know the source of Close Event. If source is user than minimize, else if source is OS shutdown event than exit.
We can use event->spontaneous() to check that. Following is the working solution:
void myAPP::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_MAC
if(event->spontaneous())
{
event->ignore();
this->setWindowState(Qt::WindowMinimized) ;
return;
}
#endif
}

Copy paste in CTreeView labels

I have a control derived from CTreeView in MFC SDI application (containing splitter, CTreeView and CDetailsView basically). What is working for me is editing labels in nodes of tree view by processing the end of edit
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, &CNavigationView::OnTvnEndlabeledit)
I want to add copy/paste functionality with Ctrl+C and Ctrl+V. I think this deals with TVN_BEGINLABELEDIT and TVN_KEYDOWN but I can't figure out how to make this work correctly, may be some ideas or sample?
void CNavigationView::OnTvnEndlabeledit(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTVDISPINFO pTVDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);
if (pTVDispInfo && pTVDispInfo->item.pszText)
{
}
}
When I am trying to paste text from Notepad, pTVDispInfo->item.pszText is NULL according to debugger.
I am working in Visual Studio 2013, Windows 8.
I suppose you already have an accelerator defined in you application that also uses Ctrl-V. So inside the inplace edit control you press Ctrl+V but this causes a WM_COMMAND message to be generated from the accelerator. The accelerator executes something that aborts the inplace edit job.
In such a case You need a PreTranslateMessage handler that checks if keyboard input arrives with Ctrl+C/Ctrl+V and directs this input to the edit control that is open instead of letting the frame window accelerator to handle it.
Just set a breakpoint and look into the callstack and check what is executed when the inplace edit stops.

WM_WINDOWPOSCHANGING and main window visibility issue

I have a large C++/MFC application that can start in two modes: 1) regular GUI mode and 2) special mode when it was started with command line parameters in which case the GUI part of the program is not shown but instead I add an icon to the notification (system) tray.
To ensure that the main window is not shown for mode 2 I process the WM_WINDOWPOSCHANGING as follows:
void OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
CDialog::OnWindowPosChanging(lpwndpos);
// TODO: Add your message handler code here
//Prevent main window from showing
lpwndpos->flags &= ~SWP_SHOWWINDOW;
}
The issue happens if after processing this message the logic determines an error in a command line and wants to show the main window. But I can't seem to show the main window after processing the WM_WINDOWPOSCHANGING message like I showed above.
PS. The project is C++ MFC written for Visual Studio 2008.

How can I hold my running function until I click the button in c++ form?

I am using .net framework 4 and visual studio 2010.
I am working with c++ form.
My code is something like that:
int k = 0;
void writeFunction(int &k){
++k;
textbox1->text = Convert::toString(k);
//i want to suspend writeFunction in there, until i click the button1 which is on Form1
//because i don't want to stop function, it has to wait to click
//after i clicked the button1 , the program continue to run code here
writeFunciton(k);
}
Can you add more details about your program? What framework are you using to generate the form, and how do button presses interact with your code?
For example, if you're using Qt then you could use signals and slots to link pressing the button with your method. Depending on the framework there might be other appropriate answers.
Generally I would recommend using a threading library to synchronize your code.
It is not wise to suspend a GUI function..instead of that use operation state variable in your form class such as
enum OperationState
{
os_normal,
os_pointselection,
os_event1,...
}
After you call textbox->=...
swicth to selection state and by using this state track mouse states like click after mouseclick check for os_selection state,then continue you duty..