How to add images when creating Visual C++ MFC project from scratch? - c++

I am try to create very simple c++ MFC project. Since it is very simple one I need to create it form scratch.
my code so far is shown below. But now I need to add picture control and thus my Intent use CImage class. But to use CImage class I need to add altimage.h header to my project. But When I do so it gives a error that cannot open source file altimage.h. So
How can I overcome this problem.
How to add the file I needed when I creating the MFC projects form the scratch.
please help me solvethis.
thanks
#include <afxwin.h> //MFC core and standard components
//#include <altimage>
#include "resource.h" //main symbols
//-----------------------------------------------------------------------------------------
//Globals
//CEdit * TEST;
CEdit * RECOG_CHARS;
CButton * BTN_CONVERT;
CButton * BTN_QUIT;
CStatic * IMG_IMAGE;
class HWCR_FORM : public CDialog
{
public:
HWCR_FORM(CWnd* pParent = NULL) : CDialog(HWCR_FORM::IDD, pParent)
{ }
// Dialog Data, name of dialog form
enum{ IDD = ID_MAIN_INTERFACE };
protected:
virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }
//Called right after constructor. Initialize things here.
virtual BOOL OnInitDialog()
{
CDialog::OnInitDialog();
RECOG_CHARS = (CEdit *)GetDlgItem(CE_ID_TEXT);
BTN_CONVERT = (CButton *)GetDlgItem(CB_ID_START);
BTN_QUIT = (CButton *)GetDlgItem(CB_ID_QUIT);
IMG_IMAGE = (CStatic *)GetDlgItem(CS_ID_IMAGE);
HBITMAP image = (HBITMAP)LoadImage(NULL,L"C:\\Users\\Kasun\\Desktop\\image.bmp",IMAGE_BITMAP,150,120,LR_LOADFROMFILE);
IMG_IMAGE->SetBitmap(image);
RECOG_CHARS->SetWindowTextW(L"Hi there");
return true;
}
public:
DECLARE_MESSAGE_MAP()
};
//-----------------------------------------------------------------------------------------
class HWCR : public CWinApp
{
public:
HWCR() { }
public:
virtual BOOL InitInstance()
{
CWinApp::InitInstance();
SetRegistryKey(_T("Hills Of Darkness"));
HWCR_FORM dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
return FALSE;
} //close function
};
//-----------------------------------------------------------------------------------------
//Need a Message Map Macro for both CDialog and CWinApp
BEGIN_MESSAGE_MAP(HWCR_FORM, CDialog)
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------------------
HWCR theApp;

First at all, in order to use CImage, you need to include header file
#include <atlimage.h>
instead of
#include <altimage>
Secondly, be sure that this file's directory is included into MSVC paths...
Normally it should be included as this header file is part of MFC / Win32 SDK....check the directories in VS properties.
Z.

Related

How to make a MFC application to DLL?

I am new to MFC programming. I have a very simple MFC application that shows a plain dialog. I want to make a DLL that do the same as this piece of code.
The working dialog code looks like this:
class MyForm : public CDialog
{
public:
MyForm(CWnd* pParent = NULL) : CDialog(MyForm::IDD, pParent) {};
enum { IDD = DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); };
virtual BOOL OnInitDialog()
{
CDialog::OnInitDialog();
return true;
}
public:
DECLARE_MESSAGE_MAP()
};
//Actural App
class MyApp: public CWinApp
{
public:
MyApp() {};
virtual BOOL InitInstance()
{
CWinApp::InitInstance();
MyForm dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
return false;
}
};
BEGIN_MESSAGE_MAP(MyForm, CDialog)
END_MESSAGE_MAP()
//start the app
MyApp theApp;
I use visual studio MFC DLL project to make a new DLL project. I make a separate *.h file to declare the dll class, and a *.cpp file to define the class.
This is the *.h file looks like:
//header file for dll project
#include "stdafx.h"
#include <afxwin.h>
#include "resource.h"
class CMyDllApp : public CDialog
{
public:
__declspec(dllexport) CMyDllApp();
enum { IDD = DLL_Dialog };
protected:
__declspec(dllexport) virtual void DoDataExchange(CDataExchange* pDX);
__declspec(dllexport) virtual BOOL OnInitDialog();
public:
__declspec(dllexport) afx_msg void myFunction();
DECLARE_MESSAGE_MAP()
};
//Actural App
class MyDllMain : public CWinApp
{
public:
__declspec(dllexport) MyDllMain() {};
__declspec(dllexport) virtual BOOL InitInstance();
};
Now I include the *.h file in my client application, but I don't know how to call the dll class correctly.
Even if I only include the *.h file and do not add any other code, the dialog of dll class just replace my client dialog, if have MyDllMain theApp; in the cpp file of dll project.
//in *.cpp of the dll project
...
MyDllMain theApp;
If I put this line in my client application, I just get a "Assertion Failed" error when running.
But if I don't put the line MyDllMain theApp; in dll project, how can I call my dll class when I want?

Cannot create main window?

I tried to make a simple MFC GUI application with C++, but it seems to have problem at generating the main window(dialog?). When I'm trying to compile the code, following message shows :
Unhandled exception at 0x00E7A9DC in GUI_Employee_0501.exe:
0xC0000005: Access violation reading location 0xFEFEFF66.
and the break point stops inside winmain.cpp, at pThread->m_pMainWnd->DestroyWindow();. The value of pThread->m_pMainWnd is NULL, which I suspect as the cause of the problem.
Can you specify what is the problem here? I have a sample code and that is very similar to mine but that works, so I truly have no idea what is happening!
#include <afxwin.h>
#include "resource.h"
#include <iostream>
using namespace std;
#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")
CEdit *pFNAME;
CEdit *pLNAME;
CEdit *pSALARY;
CEdit *pDDAY;
CEdit *pMMONTH;
CEdit *pYYEAR;
CComboBox *pGENDER;
CButton *pADD;
CButton *pDELETE;
CButton *pSAVE;
CButton *pLOAD;
class ENTRY_FORM : public CDialog
{
public:
ENTRY_FORM(CWnd* pParent = NULL) : CDialog(ENTRY_FORM::IDD, pParent) { }
enum { IDD = dialog_main };
protected:
virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }
virtual BOOL OnInitDialog()
{
CDialog::OnInitDialog();
SetUpInterfacePointers();
return true;
}
void SetUpInterfacePointers(){ ... does its own thing ... }
public:
afx_msg void add() { PRESS_ADD(); }
void PRESS_ADD() { ... does its own thing ... }
DECLARE_MESSAGE_MAP()
};
class Program : public CWinApp
{
public:
Program(){ }
public:
virtual BOOL InitInstance()
{
CWinApp::InitInstance();
cout << "CWinAPP:initInstance" << endl;
ENTRY_FORM dlg;
m_pMainWnd = &dlg;
cout << "mpMainWnd" << endl;
INT_PTR nResponse = dlg.DoModal();
return FALSE;
}
};
//
BEGIN_MESSAGE_MAP(ENTRY_FORM, CDialog)
ON_COMMAND(button_add, add)
END_MESSAGE_MAP()
//
Program theApp;
Change in InitInstance():
return FALSE;
to
return TRUE;
This is because CWinApp::InitInstance should return FALSE only in case of failure, and TRUE if all initialization went OK. In case of failure, AfxWinMain will try to destroy window pointed by m_pMainWnd, which is not possible (it causes Undefined Behaviour) because you assign to m_pMainWnd a local object (which is destroyed once InitInstance ends).
[edit]
S.B Bae - to investigate it further and find a root cause, you will need to debug place where m_pMainWnd should be set to NULL once your dialog ends. This should be in CWnd::OnNcDestroy() in wincore.cpp. There is a line pThread->m_pMainWnd = NULL; which apparently is not being executed in your application.
Late to the party, but I saw the exact problem when creating an MFC Dialog application. I found I had accidentally changed the application dialog Style (in Properties) from 'Popup' to 'Child'. Toggling this can consistently toggle this error. 'Overlapped' seems to work just as well as 'Popup'.
https://learn.microsoft.com/en-us/cpp/mfc/reference/styles-used-by-mfc?view=vs-2019#window-styles

How to display a MFC dialog from a console application in C++?

I have a simple MFC dialog.
class CMessageBoxWithCustomTextDlg : public CDialogEx
{
// Construction
public:
CMessageBoxWithCustomTextDlg(CWnd* pParent = NULL); // standard constructor
__declspec(dllexport) void SetData(std::string& data);
// Dialog Data
enum { IDD = IDD_MESSAGEBOXWITHCUSTOMTEXT_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedShowMessagebox();
};
I would like to export it as dll and call it from a simple console application. Is it possible?
It is possible; here is how I did it:
For your console application have it be simply this:
#include <Windows.h>
typedef void (*EntryFunc)();
int main()
{
HMODULE hMod = LoadLibrary(L"MFCDll.dll");
EntryFunc func = (EntryFunc)GetProcAddress(hMod, "entrypoint");
func();
}
The name of the DLL is MFCDll.dll and there is an exported function called entrypoint in that DLL.
For the DLL I created a New MFC DLL project. And other than the dialog code and the dialog in the resources add this code:
extern "C" __declspec(dllexport) void entrypoint()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CMessageBoxWithCustomTextDlg dlg;
dlg.DoModal();
}
And the console program will load the DLL, call into the DLL and the dialog shows.

How to get handler(HWND) for dialog box

Hi I have created a dialog box and it woks.
My question is: how do you retreive the handle for it?
Also, if you get the handle, how would you change the static text control text inside it?
class CStatisticsDlg : public CDialogEx
{
public:
CStatisticsDlg();
// Dialog Data
enum { IDD = IDD_STATISTICS };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
};
CStatisticsDlg::CStatisticsDlg() : CDialogEx(CStatisticsDlg::IDD)
{
}
void CStatisticsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()
Assuming you're using MFC (as indicated by the tag), then presumably you have a CDialog class instance. CDialog is a subclass of CWnd, so you can retrieve the window handle by one of 3 ways:
Directly accessing its m_hWnd member
Casting it to an HWND with operator HWND()
Calling GetSafeHwnd() on it
Here is how to do it.
First create a member function to the main application class.
Then use the following code (Assuming the class name is CGenericApp, and your Dialog class is CGenericDlg.
CWnd* CGenericApp::GetDlg()
{
return m_pMainWnd;
}
Then when you want to get a handler to the main Dialog box, use:
CGenericApp* app = (CGenericApp*)AfxGetApp();
CGenericDlg* pDlg = (CGenericDlg*)(app->GetDlg());
HWND win = pDlg->GetSafeHwnd();
'win' will hold the HWND you are looking for.

Set focus on a modeless dialogs hosted in an MFC view

I have an MFC view, and I have another project which implements and MFC dialog.
I want to host the dialog in my view.
My view is of class CFormView.
I did it that way in my view code:
m_myDialog->Create(myDialog::IDD, this);
Now, I see my dialog, but I can't set focus on it and can't use it.
What do I have to change in order to host my dialog in my view, and be able to use it and set focus to it, just as part of the view?
Thanks
I know this is a few weeks old, but you need to provide more code or a better context of what is taking place.
I had a similar problem to yourself & found this hard to find info on when I first tried this. Following is an abstract of something I've used. I'm sure there's probably a better way to do this, but I find it works the way I want;
//MyApp.h
class MyDialogClass;
class MyApp : public CWinAppEx
{
public:
MyApp();
virtual BOOL InitInstance();
//code etc
MyDialogClass *p_myDlg;
};
//MyApp.cpp
#include "MyApp.h"
#include "CMyView.h"
#include "mydialogclass.h"
BOOL MyApp::InitInstance()
{
//code etc
p_myDlg = CMyView::GetView()->p_myDlg;
//can be used here or elsewhere. I have mine linked with a button
p_myDlg->ShowWindow(true);
};
//CMyView.h
class MyDialogClass;
class CMyView : public CFormView
{
protected: // create from serialization only
CMyView();
DECLARE_DYNCREATE(CMyView)
public:
enum{ IDD = IDD_CMyView_VIEW };
static CMyView* GetView();
MyDialogClass *p_myDlg;
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
//CMyView.cpp
#include "MyApp.h"
#include "CMyView.h"
#include "mydialogclass.h"
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;
p_myDlg = new MyDialogClass(this);
return 0;
}
void CMyView::DisplayDialogFoo()
{
//can be used here or elsewhere. I have mine linked with a button
p_myDlg->ShowWindow(true);
}
//mydialogclass.h
class MyDialogClass : public CDialog
{
DECLARE_DYNAMIC(MyDialogClass)
public:
MyDialogClass(CWnd* pParent /*= NULL*/);
enum { IDD = IDD_MyDialog_DLG };
};
//mydialogclass.cpp
#include "mydialogclass.h"
MyDialogClass::MyDialogClass(CWnd* pParent /*=NULL*/)
: CDialog(MyDialogClass::IDD, pParent)
{
Create(IDD, pParent);
}
There's also an article here I just found:
http://www.codeproject.com/Articles/1651/Tutorial-Modeless-Dialogs-with-MFC