Error C2065 when using two MFC projects - c++

I have a Dialog with a CListCtrl with some data, below is a button. My second project is a dialog with a tri-state checkbox. Now I want to couple both projects, so when selecting a list item and clicking the button the tristate dialog appers showing something.
Well, I added the second project to another one and changed its type to .dll, but receive a build error
error C2065: 'IDD_MFCAPP2_DIALOG' : undeclared identifier
IDD_MFCAPP2_DIALOG is the id of he checkbox dialog. When adding the project both id's had the same value, so I changed this in resource.h manually to a different one, but it did not solve the problem. The projects work very well separately. What do I miss?
UPDATE:
I have reduced the project to a minimum, the problem is the same
MFCApp1Dlg.h
class CMFCApp1Dlg : public CDialogEx
{
// Construction
public:
CMFCApp1Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MFCAPP1_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 OnBnClickedButtonChange();
CButton m_BtnChange;
};
Its cpp file only contains the standard things. Just the button handler calls other app
void CMFCApp1Dlg::OnBnClickedButtonChange()
{
CMFCApp2Dlg dlg;
if (dlg.DoModal())
{
// do something...
}
}
MFCApp2Dlg.h looks the same
class CMFCApp2Dlg : public CDialogEx
{
// Construction
public:
CMFCApp2Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MFCAPP2_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()
};
The .rc file are also not spectaculary
IDD_MFCAPP1_DIALOG DIALOGEX 0, 0, 315, 151
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "MFCApp1"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,76,119,50,14
PUSHBUTTON "Cancel",IDCANCEL,157,117,50,14
PUSHBUTTON "Push me!",IDC_BUTTON_CHANGE,121,44,50,14
END
and
IDD_MFCAPP2_DIALOG DIALOGEX 0, 0, 203, 101
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "MFCApp2"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,46,64,50,14
PUSHBUTTON "Cancel",IDCANCEL,109,65,50,14
CONTROL "Check1",IDC_CHECK1,"Button",BS_AUTO3STATE | WS_TABSTOP,51,17,39,10
CONTROL "Check2",IDC_CHECK2,"Button",BS_AUTO3STATE | WS_TABSTOP,51,39,39,10
END
Any suggestions would be helpful.
UPDATE2:
Resource.h of App1
#define IDD_MFCAPP1_DIALOG 102
#define IDR_MAINFRAME 128
#define IDC_BUTTON_CHANGE 1000
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
and Resource.h of App2 respectively
#define IDD_MFCAPP2_DIALOG 202
#define IDR_MAINFRAME 228
#define IDC_CHECK1 2000
#define IDC_CHECK2 2001
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 229
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 2002
#define _APS_NEXT_SYMED_VALUE 201
#endif
#endif

The message just tells you that the ID is an unknown identifier. So just not known. So usually:
Make sure that you included the correct resource.h file AND make sure that the ID is defined in the resource.h

Related

Showing MFC tooltips for controls nested inside another control

For some reasons I have a parent window. This window contains normal controls. But it also takes child controls that again have controls in it. The real application is more complex. In the real version I also use WS_EX_CONTROLPARENT to allow navigation in the dialog between the nested controls.
For simplicity I created a dialog base application. In there is a normal edit control and a static control with an edit control in it. I just created the edit control inside my code.
I enabled the tool tips. In the normal way. Tooltips are shown for the buttons and the normal edit control. But the control inside the static doesn't show a tooltip.
How to get the tools tips for the nested controls too inside my handler?
Here the code for the CPP.
// ToolTipTestDlg.cpp : implementation file
#include "pch.h"
#include "framework.h"
#include "ToolTipTest.h"
#include "ToolTipTestDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CToolTipTestDlg dialog
CToolTipTestDlg::CToolTipTestDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_TOOLTIPTEST_DIALOG, pParent)
{
EnableToolTips();
}
void CToolTipTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CONTAINER, m_container);
}
BEGIN_MESSAGE_MAP(CToolTipTestDlg, CDialogEx)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()
// CToolTipTestDlg message handlers
BOOL CToolTipTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
m_edit1.Create(WS_CHILD | WS_BORDER | WS_VISIBLE, CRect(10, 10, 110, 30), this, 1);
m_edit2.Create(WS_CHILD | WS_BORDER | WS_VISIBLE, CRect(10, 10, 110, 30), &m_container, 1);
return TRUE; // return TRUE unless you set the focus to a control
}
BOOL CToolTipTestDlg::OnToolTipText(UINT nId, NMHDR* pNMHDR, LRESULT* pResult)
{
// For all keyboard messages we need to know the target of the message
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);
TOOLTIPTEXTW* pTTTW = reinterpret_cast<NMTTDISPINFO*>(pNMHDR);
CString strTipText = _T("Test");
wcsncpy_s(pTTTW->szText, _countof(pTTTW->szText), CT2W(strTipText), _countof(pTTTW->szText));
*pResult = 0;
return TRUE; // message was handled
}
The header file:
// ToolTipTestDlg.h : header file
#pragma once
// CToolTipTestDlg dialog
class CToolTipTestDlg : public CDialogEx
{
// Construction
public:
CToolTipTestDlg(CWnd* pParent = nullptr); // standard constructor
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TOOLTIPTEST_DIALOG };
#endif
CEdit m_edit1, m_edit2;
CStatic m_container;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
// Generated message map functions
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);
};
And the RC file with the container:
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_TOOLTIPTEST_DIALOG DIALOGEX 0, 0, 321, 96
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,79,75,50,14
PUSHBUTTON "Cancel",IDCANCEL,134,75,50,14
LTEXT " ",IDC_CONTAINER,7,39,168,25,SS_NOTIFY
END
My investigation showed that this MFC tooltip implementation is only designed for controls that are direct children for the control that called EnabledToolTips.
I found an solution that works. It may not be the best but it is easy to implement.
I use an own container class (in my real world code this class already exists)
class CStaticContainer : public CStatic
{
public:
DECLARE_MESSAGE_MAP()
afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);
};
I made sure that this container class is used and subclassed the existing CStatic (here the code for the sample in the dialog class):
// In the dialog class
CStaticContainer m_container;
...
void CToolTipTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CONTAINER, m_container);
}
I enabled tooltips for the container too in OnInitDialog.
m_container.EnableToolTips();
For the container class I added a TTN_NEEDTEXT handler. It just forwards the message to the outer parent.
BEGIN_MESSAGE_MAP(CStaticContainer, CStatic)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()
BOOL CStaticContainer::OnToolTipText(UINT nId, NMHDR* pNMHDR, LRESULT* pResult)
{
GetParent()->SendMessage(WM_NOTIFY, nId, reinterpret_cast<LPARAM>(pNMHDR));
return TRUE; // message was handled
}
Now the tool tips show up for all controls.

Reduced label is displayed in c++ mfc application

I develop some C++ MFC application. In my dialog box there is a progress bar and one label with constant text (cyrillic symbols).
On Windows 7,XP this text is displayed good but on Windows 8,10 it is displayed in reduced form.
Why?
This is on Windows 7:
And this is on Windows 8:
This is a source code of the class related to this Dialog Form.
*.cpp file:
// Progress.cpp : implementation file
//
#include "stdafx.h"
#include "LybidLoader.h"
#include "Progress.h"
#include "afxdialogex.h"
// Progress dialog
IMPLEMENT_DYNAMIC(Progress, CDialogEx)
Progress::Progress(CWnd* pParent /*=NULL*/)
: CDialogEx(Progress::IDD, pParent)
{
}
Progress::~Progress()
{
}
void Progress::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS1, m_ProgressBar);
}
BEGIN_MESSAGE_MAP(Progress, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON1, &Progress::OnBnClickedForceExit)
END_MESSAGE_MAP()
// Progress message handlers
BOOL Progress::OnInitDialog()
{
CDialogEx::OnInitDialog();
ModifyStyle( WS_SYSMENU, 0);
m_ProgressBar.SetMarquee(TRUE, 10);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void Progress::OnBnClickedForceExit()
{
if (::MessageBoxW(this->m_hWnd, (LPCWSTR)_T("Ви впевнені? Буде здійснено аварійний вихід"), (LPCWSTR)_T("Підтвердіть дію"), MB_ICONEXCLAMATION | MB_YESNO) == IDYES)
{
PostQuitMessage(0);
}
}
BOOL Progress::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if( pMsg->message == WM_KEYDOWN )
{
if(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
{
return TRUE; // Do not process further
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
And *.h file:
#pragma once
#include "afxcmn.h"
// Progress dialog
class Progress : public CDialogEx
{
DECLARE_DYNAMIC(Progress)
public:
Progress(CWnd* pParent = NULL); // standard constructor
virtual ~Progress();
// Dialog Data
enum { IDD = IDD_PROGRESSBAR };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
CProgressCtrl m_ProgressBar;
afx_msg void OnBnClickedForceExit();
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
And this is a portion of resources file:
IDD_PROGRESSBAR DIALOGEX 0, 0, 369, 105
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Будь-ласка, зачекайте!"
FONT 8, "Microsoft Sans Serif", 400, 0, 0x1
BEGIN
CONTROL "",IDC_PROGRESS1,"msctls_progress32",PBS_MARQUEE | WS_BORDER,7,47,355,14
LTEXT "Триває обмін даними з опціональною платою! НЕ ВИМИКАЙТЕ РАДІОСТАНЦІЮ",IDC_STATIC,25,19,267,8
PUSHBUTTON "Примусово завершити роботу",IDC_BUTTON1,101,84,118,14
END
Disregard the garbage characters in the following images, it's just a codepage issue.
The space you provide for the static text controls too small:
The first dialog is yours, modify it so that it looks like the second one.
It could be font DPI, or it could be fonts themselves. It could be "Microsoft Sans Serif" maps to a different font. I have worked enough localization to know that some fonts are not available on all systems. I would think that "Microsoft Sans Serif" would probably be, but I have seen systems in the Far East where "Arial" was not on the system.
If I were you, I would change "Microsoft Sans Serif" to "MS Shell Dlg" in your dialog resource. "MS Shell Dlg" is sort of a virtual font that maps roughly to the default GUI font.

error LNK2005: "already defined in SkinHeaderCtrl.obj"

I am Getting this error "Error 2 error LNK2005: "public: virtual __thiscall CMemDC::~CMemDC(void)" (??1CMemDC##UAE#XZ) already defined in SkinHeaderCtrl.obj C:\Users\anthonyd\Desktop\ASPX\STP\nafxcwd.lib(afxglobals.obj) STP
"
I have read through many similar questions but can not find what fits. Could someone help please?
// SkinHeaderCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "STP.h"
#include "SkinHeaderCtrl.h"
#include "memdc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BACKGROUND RGB(218,218,218)// was 218,218,218
#define HEIGHT 16
/////////////////////////////////////////////////////////////////////////////
// CSkinHeaderCtrl
CSkinHeaderCtrl::CSkinHeaderCtrl()
{
}
CSkinHeaderCtrl::~CSkinHeaderCtrl()
{
}
BEGIN_MESSAGE_MAP(CSkinHeaderCtrl, CHeaderCtrl)
//{{AFX_MSG_MAP(CSkinHeaderCtrl)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSkinHeaderCtrl message handlers
void CSkinHeaderCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
}
void CSkinHeaderCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect, rectItem, clientRect;
GetClientRect(&rect);
GetClientRect(&clientRect);
CMemDC memDC(&dc, rect);
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(&dc);
// memDC.FillSolidRect(&rect, RGB(76,85,118));
memDC.FillSolidRect(&rect, BACKGROUND);
CBitmap bitmapSpan;
bitmapSpan.LoadBitmap(IDB_COLUMNHEADER_SPAN);
CBitmap* pOldBitmapSpan = bitmapDC.SelectObject(&bitmapSpan);
// memDC.StretchBlt(rect.left+2, 0, rect.Width(), 12, &bitmapDC, 0, 0, 1, 12, SRCCOPY);
memDC.StretchBlt(rect.left+2, 0, rect.Width(), HEIGHT, &bitmapDC, 0, 0, 1, 12, SRCCOPY);
bitmapDC.SelectObject(pOldBitmapSpan);
bitmapSpan.DeleteObject();
int nItems = GetItemCount();
CBitmap bitmap;
CBitmap bitmap2;
CBitmap bitmap3;
Here is the skinheaderctrl.h
#if !defined(AFX_SKINHEADERCTRL_H__8B0847B1_B4E6_4372_A62D_038582FFEA5C__INCLUDED_)
#define AFX_SKINHEADERCTRL_H__8B0847B1_B4E6_4372_A62D_038582FFEA5C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// SkinHeaderCtrl.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CSkinHeaderCtrl window
class CSkinHeaderCtrl : public CHeaderCtrl
{
// Construction
public:
CSkinHeaderCtrl();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSkinHeaderCtrl)
//}}AFX_VIRTUAL
// Implementation
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual ~CSkinHeaderCtrl();
// Generated message map functions
protected:
//{{AFX_MSG(CSkinHeaderCtrl)
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SKINHEADERCTRL_H__8B0847B1_B4E6_4372_A62D_038582FFEA5C__INCLUDED_)
Update
On the skinheaderctrl.cpp I have a includes #include "memdc.h" do I want to update this to CMemDC?
CMemDC is an internal helper class in MFC (see Internal Classes). You've apparently defined your own CMemDC class, with the result that the linker is seeing two destructors with the same name. You'll either have to rename your class to something else or put it in a namespace to avoid the conflict.

VC++ create Cdialog Class Assertion error

I'm a complete noob with Visual Studio, it's probably a stupid mistake.
I'm only trying to instantiate a Cdialog class, I'm making a dll that in the end will need to had some activeX commands in the Window.
I'm tried several ways to instantiate, but I always getting an assertion error.
So in my calldllclass.h I had:
#pragma once
__declspec(dllexport) long init();
in my class that extend Cdialog:
//{{AFX_INCLUDES()
//}}AFX_INCLUDES
class CMyclass: public CDialog{
#include "resource.h"
public:
CMyclass(CWnd* pParent = NULL)
: CDialog(100, pParent){
//{{AFX_DATA_INIT(CMyclass)
//}}AFX_DATA_INIT
}; // standard constructor
virtual ~CMyclass(){};
long calc();
// Dialog Data
//{{AFX_DATA(CMyclass)
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyclass)
protected:
//}}AFX_VIRTUAL
protected:
// Generated message map functions
//{{AFX_MSG(CMyclass)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
I created an Resource Class:
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE MOVEABLE PURE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE MOVEABLE PURE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE MOVEABLE PURE
BEGIN
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
"#ifdef _WIN32\r\n"
"LANGUAGE 9, 1\r\n"
"#pragma code_page(1252)\r\n"
"#endif //_WIN32\r\n"
"#include ""res\\SimpleVC6SampleEvent.rc2"" // non-Microsoft Visual C++ edited resources\r\n"
"#include ""afxres.rc"" // Standard components\r\n"
"#endif\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 0, 0
STYLE DS_MODALFRAME
FONT 8, "MS Sans Serif"
BEGIN
END
#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////
and finally in my cpp I had:
// SIMPLEDIALOGDLL.cpp : Defines the exported functions for the DLL application.
//
#pragma once
#include "stdafx.h"
#include "myclass.h"
#include "resource.h"
#include "dll2export.h"
BEGIN_MESSAGE_MAP(CMyclass, CDialog)
//{{AFX_MSG_MAP(CMyclass)
//}}AFX_MSG
END_MESSAGE_MAP()
long init(){
AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CMyclass * obj = new CMyclass();
//obj->ShowWindow(SW_HIDE);
obj->ShowWindow(SW_SHOW);
long res = obj->calc();
return (long) obj;
}
long CMyclass::calc(){
return 1+1;
}
When obj->ShowWindow(SW_SHOW); is called I get an
Debug Assertion Failed!
winocc.cpp Line 329
BOOL CWnd::ShowWindow(int nCmdShow)
{
ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
if (m_pCtrlSite == NULL)
return ::ShowWindow(m_hWnd, nCmdShow);
else
return m_pCtrlSite->ShowWindow(nCmdShow);
}
a little update I put after showWindow it returns 1400
DWORD dw = GetLastError();
add answer sugenstion
I had this to init method
AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
CMyclass *obj = new CMyclass();
if(obj->Create(100)){
obj->ShowWindow(SW_HIDE);
DWORD dw = GetLastError();
}else{
DWORD dw = GetLastError();
}
Now I'm having in obj->Create(100)); objcore.cpp Line: 40
#ScottMcP-MVP is right, modeless dialog boxes need a call to the Create method.
Also, according to the MSDN docs on the constructor,
To construct a modeless dialog box, use the protected form of the
CDialog constructor. The constructor is protected because you must
derive your own dialog-box class to implement a modeless dialog box.
Construction of a modeless dialog box is a two-step process. First
call the constructor; then call the Create member function to create a
resource-based dialog box...
So declare your class like CMyclass() : CDialog() {...} and use the following code to display it:
CMyclass * obj = new CMyclass();
if (obj->Create(100))
obj->ShowWindow(SW_SHOW);
else
{ << error >>
}
P.S.: don't forget to delete the variable at the end of your program!
My code was right,
after many many hours, I went to change the compiler options of the Visual Studio and in Debug Information Format put Program Database (/Zi)
That fix it for me

MFC- receiving Button-Click-Message failed

I've created a new dialog in my MFC dialog based application. the new dialog contains 5 control buttons.
the following happens and I don't understand why?
click on buttonX. (result ok, OnBnClicked message is sent)
click on on any place of the application, but not on the dialog.(removing focus from dialog)
click again on buttonX (FAILED, OnBnClicked message is NOT sent). but if instead I click on any other button in the dialog (result ok, OnBnClicked message is sent).
and when I do:
...
...
click on the dialog area just to set focus on the dialog again
click again on buttonX. (result ok, OnBnClicked message is sent)
**I need to do step 3 only if I want to click again on the buttonX! why??
I think it related to SetFocus() but I m not sure how.
the buttons IDC are:
IDC_BACK_MEDIA_PRESS_BUTTON 1180
IDC_TOOLS_LEFT_RIGHT 1024
IDC_MEDIA_FOREWARD_BUTTON 1103
IDC_MEDIA_BACKWARD_BUTTON 1104
IDC_TOOLS_HOOD_BUTTON 2346
the dialog properties is:
IDD_TOOLS_DIALOG DIALOGEX 0, 0, 51, 218
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Tools"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
PUSHBUTTON "Media &Foreward",IDC_MEDIA_FOREWARD_BUTTON,7,79,37,36,BS_MULTILINE
PUSHBUTTON "&Media &BackWard",IDC_MEDIA_BACKWARD_BUTTON,7,43,37,36,BS_MULTILINE
PUSHBUTTON "Back Media Press",IDC_BACK_MEDIA_PRESS_BUTTON,7,127,37,36,BS_MULTILINE | NOT WS_VISIBLE
PUSHBUTTON "Hood",IDC_TOOLS_HOOD_BUTTON,7,7,37,36
PUSHBUTTON "Left Right",IDC_TOOLS_LEFT_RIGHT,7,175,37,36
END
I've tried different style like, tool windows, overlapped, popup. it happens in all the cases.
Thanks for the help.
.h
class CToolsDlg : public CBDialog
{
DECLARE_DYNAMIC(CToolsDlg)
public:
CToolsDlg(CWnd* pParent = NULL); // standard constructor
virtual ~CToolsDlg();
CToolTipCtrl m_ToolsTips;
// Dialog Data
enum { IDD = IDD_TOOLS_DIALOG };
protected:
virtual void OnCancel();
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
CMFCButton m_HoodButton;
CMFCButton m_MediaForewardButton;
CMFCButton m_MediaBackwardButton;
CMFCButton m_LeftRightButton;
virtual BOOL OnInitDialog();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnBnClickedCancel();
afx_msg void OnBnClickedToolsHoodButton();
afx_msg void OnBnClickedMediaForewardButton();
afx_msg void OnBnClickedMediaBackwardButton();
afx_msg void OnBnClickedLeftRightButton();
afx_msg void OnBnClickedBackMediaPressButton();
};
.cpp
IMPLEMENT_DYNAMIC(CToolsDlg, CBDialog)
CToolsDlg::CToolsDlg(CWnd* pParent /*=NULL*/)
: CBDialog(CToolsDlg::IDD, pParent)
{
}
CToolsDlg::~CToolsDlg()
{
}
void CToolsDlg::DoDataExchange(CDataExchange* pDX)
{
CBDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TOOLS_HOOD_BUTTON, m_HoodButton);
DDX_Control(pDX, IDC_MEDIA_FOREWARD_BUTTON, m_MediaForewardButton);
DDX_Control(pDX, IDC_MEDIA_BACKWARD_BUTTON, m_MediaBackwardButton);
DDX_Control(pDX, IDC_TOOLS_LEFT_RIGHT, m_LeftRightButton);
}
BEGIN_MESSAGE_MAP(CToolsDlg, CBDialog)
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_TOOLS_HOOD_BUTTON, &CToolsDlg::OnBnClickedToolsHoodButton)
ON_BN_CLICKED(IDC_MEDIA_FOREWARD_BUTTON, &CToolsDlg::OnBnClickedMediaForewardButton)
ON_BN_CLICKED(IDC_MEDIA_BACKWARD_BUTTON, &CToolsDlg::OnBnClickedMediaBackwardButton)
ON_BN_CLICKED(IDC_TOOLS_LEFT_RIGHT, &CToolsDlg::OnBnClickedLeftRightButton)
ON_BN_CLICKED(IDC_BACK_MEDIA_PRESS_BUTTON, &CToolsDlg::OnBnClickedBackMediaPressButton)
END_MESSAGE_MAP()
// CToolsDlg message handlers
BOOL CToolsDlg::OnInitDialog()
{
CBDialog::OnInitDialog();
// Window position
//////////////////////////////////////////////////////////////////////////
CMainFrame* mf = (CMainFrame*)AfxGetMainWnd();
RECT MFwinRect;
RECT ThiswinRect;
CWnd* fv = mf->m_wndSplitter.GetView( mf->m_wndSplitter.GetCurrentViewIndex(0,0) );
fv->GetWindowRect(&MFwinRect);
GetWindowRect(&ThiswinRect);
MoveWindow(
MFwinRect.right - (ThiswinRect.right - ThiswinRect.left) - 14, // X
MFwinRect.top + 14, // Y
(ThiswinRect.right - ThiswinRect.left), // nWidth
(ThiswinRect.bottom - ThiswinRect.top) ); // nHeight
// Set controls state
//////////////////////////////////////////////////////////////////////////
m_ToolsTips.Create(this);
m_ToolsTips.AddTool(&m_HoodButton, TOOLTIP_HOOD_BUTTON);
m_ToolsTips.AddTool(&m_MediaForewardButton, TOOLTIP_MEDIA_FOREWARD_BUTTON);
m_ToolsTips.AddTool(&m_MediaBackwardButton, TOOLTIP_MEDIA_BACKWARD_BUTTON);
m_ToolsTips.AddTool(&m_LeftRightButton, TOOLTIP_LEFT_RIGHT_BUTTON);
m_ToolsTips.SetDelayTime(1000);
m_ToolsTips.Activate(BARAK_PREFS->m_Params.m_bShowToolTips);
// Main timer loop (no need for now)
// SetTimer( 1, 1000, NULL );
return TRUE;
}
BOOL CToolsDlg::PreTranslateMessage(MSG* pMsg)
{
m_ToolsTips.RelayEvent(pMsg);
return CBDialog::PreTranslateMessage(pMsg);
}
void CToolsDlg::OnCancel()
{
// When closing the window, destroy it and not only hide (its a floating window).
DestroyWindow();
}
void CToolsDlg::OnTimer(UINT_PTR nIDEvent)
{
CBDialog::OnTimer(nIDEvent);
}
void CToolsDlg::OnBnClickedToolsHoodButton()
{
...
}
void CToolsDlg::OnBnClickedMediaForewardButton()
{
...
}
void CToolsDlg::OnBnClickedMediaBackwardButton()
{
...
}
void CToolsDlg::OnBnClickedLeftRightButton()
{
...
}
void CToolsDlg::OnBnClickedBackMediaPressButton()
{
...
}
I see that you are filling view with dialog content. Have you put focusing to your dialog? I think this mystic behavior happening only on first mouse click in your dialog (then dialog gets the focus). I'm just guessing :)