where is the exit function of a MFC application? - c++

i can't seem to find any exit function in my mfc application codes.
i want my application to actually save some settings when i click on the red cross on the top right of the application. anyone knows where is this exit function located at? thanks

Depends on what type of application it is. Check CWinApp::ExitInstance which will be called always. You may also look into CDialog::OnCancel, CWnd::OnClose, CWnd::OnDestroy, CFrameWnd::OnNcDestroy

If it is a dialog based application, I prefer the following code
void CMFC_dialogDlg::OnOK()
{
}
void CMFC_dialogDlg::OnCancel()
{
}
void CMFC_dialogDlg::OnClose()
{
//Call Save Function
CDialog::OnOK();
}
After adding the following code i will remove the Ok and Cancel button from the dialog so that i will prevent the application from closing while pressing Esc/Enter key.

Related

C++ Global Hotkeys with platform APIs

I'm working on an application for taking screenshots on Windows, OSX and Linux in C++/Qt. Now I need to set global hotkeys, so the user can take screenshots when the application is running in the background. I tried with Qxt and UGlobalHotkey, which are both Qt libraries, but neither of them seemed to work.
I tried to implement it for OSX with Carbon (tutorial), but I need to call a class member function, which just doesn't work. Could someone provide me with an example? You can find my code here. The function i need to call is new_screenshot().
Or is there any other way to achieve something like this? I really need my application to take a screenshot from the background, otherwise it's pretty useless (yes, I should probably have implemented it at the very beginning to see if it even works). Would it maybe be better to have a separate client for every platform (Cocoa Swift for OSX, GTK for Linux, C# client for Windows)? I have often thought about this the past few days.
Do I understand correctly that you want to call new_screenshot from the hot key event handler? If so, InstallApplicationEventHandler lets you pass a pointer to user data in 4th argument. Pass a pointer to your MainWindow instance (based on code from the tutorial):
MainWindow *mainWindow = ... // get main window somehow
InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,mainWindow,NULL);
Then you can use it in the event handler.
OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData)
{
//Do something once the key is pressed
static_cast<MainWindow*>(userData)->new_screenshot();
return noErr;
}
I did something in the past with MFC and WIN32 API....so it only works on Windows...but pressing ALT+F10 was able to hide/show a window...
void CWinHideDlg::OnButtonActive()
{
CString tmp;
GetDlgItemText(IDC_BUTTON_ACTIVE,tmp);
if(0 == strcmp(tmp.GetBuffer(tmp.GetLength()),"Activate"))
{
m_myAtom=GlobalAddAtom("MY_GLOBAL_HOT_HIDE_KEY");
int err=RegisterHotKey(this->GetSafeHwnd(),m_myAtom,MOD_ALT,VK_F10);
SetDlgItemText(IDC_BUTTON_ACTIVE,"Stop");
CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_UNHIDE);
pBtn->EnableWindow(TRUE);
SetDlgItemText(IDC_STATIC_INFO,"Set the mouse over the window \nand press ALT + F10 to hide it...");
}
else
{
UnregisterHotKey(this->GetSafeHwnd(),m_myAtom);
GlobalDeleteAtom(m_myAtom);
CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_UNHIDE);
pBtn->EnableWindow(FALSE);
SetDlgItemText(IDC_BUTTON_ACTIVE,"Activate");
}
}
Basically this code activates/deactivates the hot key ALT+F10, once it activates you can hide/unhide a running window on the system by setting the mouse pointer over the window and press ALT+F10...
This is from the WindowProc function:
if(message == WM_HOTKEY)
{
CString tmp;
POINT pc;
GetCursorPos(&pc);
if(GetAsyncKeyState(VK_F10))
{
HWND hwnd=::WindowFromPoint(pc);
if(hwnd)
{
tmp.Format("%08Xh",hwnd);
m_HideWins.InsertString(m_HideWins.GetCount(),tmp);
::ShowWindow(hwnd,SW_HIDE);
}
}
}
You can use the code to register your own HOT Key and use it to take a screenshot...
Hope it helps...

MFC's CTabCtrl::HitTest function returns "1" for any tab clicked

Hi (although greeting usually gets deleted),
I'm using the MFC's CTabCtrl control and try to determine which tab was clicked (to drag & drop it later). Should be quite easy I thought - anyway got stuck with the HitTest function which returns "1" for whichever tab is clicked.
As I started the project very recently, it's literaly just a handful of lines. The mentioned HitTest function is used in Tdi.cpp file in CHlavniOkno::CTdi::OnLButtonDown function (full source code at http://nestorovic.hyperlink.cz/cpp_mfc.zip ):
afx_msg void CHlavniOkno::CTdi::OnLButtonDown(UINT flagy,CPoint bod){
if (::DragDetect(m_hWnd,bod)){
TCHITTESTINFO hti={bod};
if (int idZalozky=HitTest(&hti)>=0)
parametryTazeneZalozky=new TParametryTazeneZalozky(this,idZalozky);
}
CTabCtrl::OnLButtonDown(flagy,bod);
}
I definitely must have omitted something tiny, as is almost always the case...
Thanks for your time by having a look at the problem.
Tomas
The statement int idZalozky=HitTest(&hti)>=0 is setting idZalozky to the result of the test HitTest(&hti)>=0. As a boolean test this will always return either 0 or 1.
You probably want:
int idZalozky=HitTest(&hti);
if (idZalozky>=0)
{
...
}

Can't detect click in browser window to fire a function

Want to trigger function on mouse click in browser window via C++ plugin, written with FireBreath.
But with code below nothing happends on click.
Got the following code in TestPluginAPI.h:
BEGIN_PLUGIN_EVENT_MAP()
EVENTTYPE_CASE(FB::MouseDownEvent, onMouseDown, FB::PluginWindow)
END_PLUGIN_EVENT_MAP()
virtual bool onMouseDown(FB::MouseDownEvent *evt, FB::PluginWindow *);
And this code in testPluginAPI.cpp:
bool TestPluginAPI::onMouseDown(FB::MouseDownEvent *evt, FB::PluginWindow *)
{
if(evt->m_Btn == FB::MouseButtonEvent::MouseButton_Left)
{
fire_showcrd(FB::variant_list_of(evt->m_x)(evt->m_y));
}
return 0;
}
Fire_showcrd(...) was tested separately and it's working. It seems that something wrong with click detecter part, but what?
The PluginWindow events in FireBreath only apply to the region where the plugin lives, not elsewhere on the webpage. You'll only get events when the plugin itself is clicked on using this method, and if any DOM elements are hovering over your plugin (even if your plugin draws in front because it's windowed) you may end up losing the events to that element.
You can get click events for the whole page by using javascript.

CheckBox value in different file

I need to access the checkbox value in a different program. Check box is initiated in ToolDlg.cpp
DDX_Control(pDX, IDC_CalculateTBA, m_CalculateTBA);
in the oninitdialog initiated like this:
m_CalculateTBA.SetCheck(0);
CalculateAnalyticTBA = false;
void CToolDlg::OnBnClickedCheck3()
{
CalculateAnalyticTBA = m_CalculateTBA.GetCheck();
}
I need checkbox value in SetCal.cpp program. Here is the code i am trying in this program:
CToolDlg dialog;
if( dialog.CalculateAnalyticTBA )
{
Do some thing
}
But the dialog.CalculateAnalyticTBA is always tru even though i don't check the check box.
Plz let me know if you need any other info. Thanx for help.
It looks like you're creating a dialog, and never showing it. So CalculateAnalyticTBA has whatever value you gave it in the constructor of your CToolDlg class (or, if you didn't, whatever value the compiler gave it).
In order for this value to be set you must at least create the dialog so that the Data Exchange code (which invokes the DDX_Control and handles the binding of the checkbox and the variable) has a chance to run.
The correct way is to create and display the CToolDlg dialog and wait for the user to select his choices then only process the choices when the user finally click the "OK" button.
CToolDlg dialog;
// create and display the dialog
if (dialog.DoModal()==IDOK)
{ // user clicked the ok button, now do the work
....
}

Handling exit without saving in Qt

Im a student programmer using Qt to build and application for work and I am having some difficulty figuring out how to handle when a user has exited a Dialog without saving changes. The dialog is primarily purposed for data entry so being able to identify if changes have been made and then offering the user the option to save before quitting would be extremely usefull. I looked through Qt's Documentation on QDialog and didn't find anything in regards to the mode that is returned if the exit button is pressed. Having a means to just identify when the exit button is clicked is first priority. Also, being a student programmer I am also open to any ideas for best practices in regards to how I should compare before and after data. The data is entered into a table so I am guessing that I would have to make something like this
connect(some kinda exit handler, SIGNAL(clicked), this, SLOT(comparePreAndPostTable)
QVector<QString> prechanges = everything from the table
QVector<QString> postchanges = everything from table when exit is clicked
if(prechanges != postchanges)
{
Give oppertunity to save
}
Any assistance is appreciated! It would really be nice if Qt already had something for this!
You need to override the closeEvent() method of QWidget to handle the exit button of your dialog. The documentation even give an example that strangely looks like what you are looking for:
void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
where maybeSave() would compare to see if anything has changed.