dialogbox in a MFC program - c++

i have written the following application using MFC in visual c++ that includes two resources (a menu and a dialogbox) (created using the resource editor)...the program works absolutely fine except that it displays only one resource ie. it displays only the menu but it does not display the dialogbox...
what to do??
this is the code...
#include<afxwin.h>
#include"stdafx.h"
#include"resource.h"
class mydialog:public CDialog
{
private:
int id;
public:
mydialog(int n):CDialog(n)
{
id=n;
}
int OnInitDialog()
{
CDialog::OnInitDialog();
if(id==IDD_DIALOG1)
CenterWindow(GetDesktopWindow());
else
CenterWindow();
return TRUE;
}
void OnOK()
{
CDialog::OnOK() ;
MessageBox(TEXT("You have Pressed the OK Button"),TEXT("OnOK handler"));
}
};
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,TEXT("Simple Dialog Box"),WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}
void about()
{
mydialog d(IDD_DIALOG1);
d.DoModal();
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND(101,about)
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *p;
p=new myframe;
p->ShowWindow(3);
m_pMainWnd=p;
return 1;
}
};
myapp a;

Hey, without compiling the code and running it I can see a problem here:
myframe()
{
Create(0,TEXT("Simple Dialog Box"),WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}
Where you are creating a window using IDR_MENU1 resource which is a menu. This means that the main window of your app is the menu.
Also, the whole code does not look MFC-ish at all. I would suggest creating an MFC app from Visual Studio template - it will set up the main window properly for you.

The dialog will only be displayed when the command with id 101 is executed. Presumably this would be a menu item which is associated with the main window. If your menu is defined as:
IDR_MENU1 MENU
BEGIN
POPUP "HELP"
BEGIN
MENUITEM "About", ID_HELP_ABOUT
END
END
And ID_HELP_ABOUT is defined with the value 101, then your about function will get called when you select that menu item, showing the dialog.
I'm not sure exactly what you are trying to achieve here, and would echo the other suggestions here by saying to start out with the MFC wizard generated code and take it from there.

Related

Gtkmm 3/C++, closing the program with a button instead of the window "X"

everybody.
I am working on a gtkmm app and need some help getting a "Close" button to work. As suggested by the gtkmm documentation, I derived a class for the main window object, created some members, and left the main() function mostly for reading the glade UI file, instantiating the form and starting the main loop.
There are 3 files, named conveniently for explanation: Declarations.h, Declarations.cpp, Program.cpp
In "Declarations.h" I have the class inherited from the Gtk Window:
#include <gtkmm.h>
class MainWindowClass : public Gtk::ApplicationWindow
{
protected:
Gtk::Button *button_close;
// other buttons here
protected:
void on_button_close_clicked();
// other callback functions here
public:
MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade); // Constructor
// Destructor, other public members
};
In "Declarations.cpp" I have the implementations:
#include "Declarations.h"
using namespace Gtk;
// Implementing the constructor
MainWindowClass::MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade) :
Gtk::Window(cobject), builder(refGlade)
{
builder->get_widget("button_close", button_close);
// Getting other widgets from the glade file
button_close->signal_clicked().connect(sigc::mem_fun(*this, &MainWindowClass::on_button_close_clicked));
// Connecting other callback functions here
}
// Implementing the callback for the "Close" button, ** PROBLEM IS HERE **
void MainWindowClass::on_button_close_clicked()
{
//gtk_main_quit(); Apparently GTK+/C only, compiler doesn't complain but causes a segfault when clicking the button
//Gtk::Application::quit(); Won't compile
}
The Program.cpp reads the UI from a file and starts the main program loop:
#include <gtkmm.h>
#include "Declarations.h"
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "Damn this close button");
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("Program_UI.glade");
MainWindowClass our_main_window;
return app->run(our_main_window);
}
I am omitting some non-relevant code (of other objects and callbacks) because they work, it is the close procedure that is causing me trouble, though closing the app with "X" works.
I have also thought about trying to call a quit() or destroy() function (if they exist) of "app", but then the callback function doesn't know "app" exists.
What do you guys suggest?
Thanks a lot.
** Edit: fixed this using FormMain::hide(), which is inherited from GtkWindow.
I thought the static procedure Gtk::Main::hide() would do it, but the compiler says that hide() is not a member of Gtk::Main...
Well, moving forward one step at a time.
Used FormMain::hide() (inherited from GtkWindow). The static procedure Gtk::Main::hide() was not being recognized by the compiler.

C++ can't display ActiveX control in the dialog CFormView, control variable is NULL

I use a SmartGraph ActiveX control in my project (Visual Studio 2015, MFC, C++). It has been registered successfully.
I try to fit a dialog with this control into CFormView
MyAppView.h:
#pragma once
#include "SmartGraph.h"
#include "afxwin.h"
class CMyAppView : public CFormView
{
protected: // create from serialization only
CMyAppView();
DECLARE_DYNCREATE(CMyAppView)
enum { IDD = IDD_DIALOG1 };
CSmartGraph m_Graph; //!!!!! ActiveX control variable
CButton m_ctrlOK;
....
}
MyAppView.cpp:
....
void CMyAppView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDOK, m_ctrlOK);
DDX_Control(pDX, IDC_SMARTGRAPH1, m_Graph);
}
void CMyAppView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
ResizeParentToFit();
m_Graph.SetParentWnd(this->m_hWnd);
m_Graph.SetPlotType(0);
m_Graph.put_xLable(_T("Time"));
m_Graph.put_yLable(_T("Amplitude"));
m_Graph.put_Title(_T("Graph Test"));
}
...
So the m_Graph is NULL and SmartGraph isn't displayed in the dialog. At the same time the OK button variable isn't NULL and it is displayed correctly.
What I do wrong?
You need to create an instance of this object. m_Graph.CreateControl(...);

Debug Assertion Failed CDialog

I have an error in my c++ project. If I clicked "Cancel" or "OK" or "X" buttons program is crashing and display an error like the screenshot.
What could be problem? Here is full code ; http://pastebin.com/54DfqrDb
void CSettingDlg::OnBnClickedCancel()
{
CDialog::OnCancel();
}
void CSettingDlg::OnBnClickedOk()
{
CDialog::OnOK();
}
I fixed this problem. If I declare already don't working a class variable this problem is occurred.
Solution :
//CZipArchive m_ZipArchive; //this class variable is don't working for me

Dialog in MFC programming

I am learning MFC programming. In the part of dialog design, I entered a problem.
The resource is IDD_PEN_WIDTH, which is the ID of the dialog. And there is a piece of automatically generated code related to this:
class PenWidthDlg : public CDialogEx
{
DECLARE_DYNAMIC(PenWidthDlg)
public:
PenWidthDlg(CWnd* pParent = NULL); // standard constructor
virtual ~PenWidthDlg();
// Dialog Data
enum { IDD = IDD_PEN_WIDTH };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
int m_nPenWidth;
};
Before I run my program, the IDD_PEN_WIDTH shows "#define IDD_PEN_WIDTH 301" while hovering mouse on IDD_PEN_WIDTH in the code. But when I run it, there is one error says IDD_PEN_WIDTH is an undefined identifier. Then I hover mouse on "IDD_PEN_WIDTH", it also says it's undefined.
I am usually confused in MFC studying, and I will appreciate very much for your detailed explanations. Thanks.
It has to be defined in every cpp file that uses it. Add #include "Resource.h" in those cpp files. Do that #include before the #include of the dialog .h file.

wxWidgets Seg fault on close

My files look something like so:
main.cpp
...
bool SyncApp::OnInit(){
SetTopWindow(new syncWindow(_("(S)FTP Automatic Sync")));
GetTopWindow()->Show(true);
return true;
}
...
syncwindow.h
#include <wx/wx.h>
class syncWindow : public wxFrame {
public:
syncWindow(wxString title) : wxFrame(NULL, -1, title) { initialize(); }
private:
void initialize();
wxTextCtrl * serverEntry;
};
syncwindow.cpp
void syncWindow::initialize(){
serverEntry = new wxTextCtrl(this, wxID_ANY);
this->AddChild(serverEntry);
}
For whatever reason whenever I close the window I get a segfault. If I don't add the serverEntry as a child to the window I don't get a segfault. I don't see why this is doing such a thing. I'm on CentOS 6, g++ 4.7 and wxGTK 2.8. Does anyone have any idea or a hint as to why this is happening?
Since you specified the parent window when constructing your child, the link is already present and calling this->AddChild(serverEntry); will cause double free or similar error when you close the window. http://docs.wxwidgets.org/2.8/wx_wxwindow.html#wxwindowaddchild
wxWindow::AddChild
Adds a child window. This is called automatically by window creation
functions so should not be required by the application programmer.
Notice that this function is mostly internal to wxWidgets and
shouldn't be called by the user code.