[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

Need some help with my SDI and DialogBox 500 points !!!!

Hello,
I need some help with my SDI program.
When I start my SDI a smal DialogBox will show up to enter a password for the program.
I have deleted the ok and abort button, but also changed the code before (take a look) so the user
can not close the DialogBox (even with escape). I now want to add a single "EXIT" button to close the DialogBox and also the SDI !!! I allways get a debug error. Debug Assertion failed   File: wingdi.cpp line 1044

Please take a look at my code, thank you very much. 500 points with grade A when it works
Best regards,
Thomas

Here is my code:
Password.cpp
//

#include "stdafx.h"
#include "Password_SDI_Dialog.h"
#include "Password.h"
#include ".\password.h"


// CPassword

IMPLEMENT_DYNAMIC(CPassword, CDialog)
CPassword::CPassword(CWnd* pParent /*=NULL*/)
      : CDialog(CPassword::IDD, pParent)
{
}

CPassword::~CPassword()
{
}

void CPassword::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CPassword, CDialog)
     
     
      ON_BN_CLICKED(IDCANCEL, OnCancel)
     
      ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()


// CPassword
//############################################################################
BOOL CPassword::OnInitDialog()
{
      CDialog::OnInitDialog();

     

      return TRUE;  
     
}
//############################################################################

void CPassword::OnOK()
{
     
}

void CPassword::OnBnClickedOk()
{
     
}

void CPassword::OnCancel()
{
     
}

void CPassword::OnBnClickedCancel()
{
      CDialog::OnCancel();
}

void CPassword::OnBnClickedButton1()
{
     
      AfxGetMainWnd()->PostMessage(WM_CLOSE,0,0);
     
     
}


I changed my code above to:
I just had to close the DialogBox before I call WM_CLOSE with CDialog::OnCancel() and it works.

void CPassword::OnBnClickedButton1()
{
      CDialog::OnCancel();                                                       //Close DialogBox
      AfxGetMainWnd()->PostMessage(WM_CLOSE,0,0);     //Exit Program
           
}
I was to fast for the glory.... it works when I press the button shortly after the start of the SDI....but
when I wait for a minute or so and press the button again, I get the same error as above....ASSERT error

Please help...still a beginner in Visual C++
Thanks
Best regards,
Thomas
0
tsp2002
Asked:
tsp2002
  • 6
  • 5
1 Solution
 
tsp2002Author Commented:
P.S.
I call the DialogBox in OnDraw of my View.cpp ....... Is this okay???

void CPassword_SDI_DialogboxView::OnDraw(CDC* /*pDC*/)
{
      CPassword_SDI_DialogboxDoc* pDoc = GetDocument();
      ASSERT_VALID(pDoc);
    if (!pDoc)
          return;

      //--------------------------- Dialogbox to enter Password -------------------
      Sleep(100);
      CPassword m_passwDialog;
      if(m_passwDialog.DoModal()==IDOK)
      {
      }
      
      //-------------------------------------------------------------
}



The Error stops always at:
wingdi.cpp
CPaintDC::~PaintDC()
{
   ASSERT (m_hDC != NULL);
   ASSERT(::IsWindow(m_hWnd);    <======= here
0
 
mahesh1402Commented:
You processing that when your main window is not created and available fully...

You may check that using IsWindow()....have you tried what I suggested earlier to wait until window creation and then invoke ur password dialog ???

e.g.

while(!IsWindow(hWndMain))  //Wait for the Main window to create & come up
       Sleep(100);

// Do Password dialog invoke stuff......

-MAHESH
0
 
tsp2002Author Commented:
while(!IsWindow(hWndMain))  //Wait for the Main window to create & come up
       Sleep(100);

I don´t know where and how I can do this?????????????????? SORRY, need more help
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mahesh1402Commented:
from where r u creating / invoking your dialog ?
0
 
tsp2002Author Commented:
I have added  a new DialogBox via Ressource and added a new Class call CPassword.
I call the DialogBox in OnDraw of my View.cpp

void CPassword_SDI_DialogboxView::OnDraw(CDC* /*pDC*/)
{
      CPassword_SDI_DialogboxDoc* pDoc = GetDocument();
      ASSERT_VALID(pDoc);
    if (!pDoc)
          return;

      //--------------------------- Dialogbox to enter Password -------------------
      Sleep(100);
      CPassword m_passwDialog;
      if(m_passwDialog.DoModal()==IDOK)
      {
      }
     
      //-------------------------------------------------------------
0
 
mahesh1402Commented:
No ! dont add that to OnDraw handler....thats not the right place...

I will suggest you to use custom PostMessage() to send message to your main window from main window's OnCreate and invoke your dialog from there...in this case I think even you dont need above loop to wait til window creation finished...

So try this :

#define DLG_INVOKE_MSG (WM_APP + 1)   // declare custom message

In main window's OnCreate do

PostMessage(DLG_INVOKE_MSG , 0, 0);  // Post custom message to this window

In CMainFrame message map do

ON_MESSAGE(DLG_INVOKE_MSG , OnDlgInvoke) //define & declare OnDlgInvoke()

and then invoke your dialog in the message handler

LRESULT CMainFrame::OnDlgInvoke(WPARAM, LPARAM)
{
   CYourDialog dlg;
   if (dlg.DoModal()==IDOK)
   { ...
   }
   return 0;
}

try this.

-MAHESH
0
 
tsp2002Author Commented:
Hi Mahesh,
it works....thank you.
Please take a look at my CMainFrame code:
Is this so okay??? Please let me know. Thank you.

// MainFrm.cpp
//

#include "stdafx.h"
#include "Password_SDI_Dialogbox.h"
#include "Password.h"

#include "MainFrm.h"
#include ".\mainfrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW

#endif
//------------------------------
#define DLG_INVOKE_MSG 12345
//------------------------------
 
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
      
      ON_WM_CREATE()
      ON_MESSAGE(DLG_INVOKE_MSG, OnDlgInvoke)    //<<<<<<=========================
      
      
END_MESSAGE_MAP()

static UINT indicators[] =
{
      ID_SEPARATOR,          
      ID_INDICATOR_CAPS,
      ID_INDICATOR_NUM,
      ID_INDICATOR_SCRL,
};




CMainFrame::CMainFrame()
{
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
      
      if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
      {
            TRACE0("Symbolleiste konnte nicht erstellt werden\n");
            return -1;      
      }

      if (!m_wndStatusBar.Create(this) ||
            !m_wndStatusBar.SetIndicators(indicators,
              sizeof(indicators)/sizeof(UINT)))
      {
            TRACE0("Statusleiste konnte nicht erstellt werden\n");
            return -1;      
      }
      
      m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
      EnableDocking(CBRS_ALIGN_ANY);
      DockControlBar(&m_wndToolBar);

      //------------------------------
      PostMessage(DLG_INVOKE_MSG,0,0);  //<<<<================
      //------------------------------

      return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
      if( !CFrameWnd::PreCreateWindow(cs) )
            return FALSE;
      

      return TRUE;
}


// CMainFrame Diagnose

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
      CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
      CFrameWnd::Dump(dc);
}

#endif //_DEBUG






//###########################################################
LRESULT CMainFrame::OnDlgInvoke(WPARAM wParam, LPARAM lParam)
{
      CPassword m_passwDialog;
      if(m_passwDialog.DoModal()==IDOK)
      {
      }
      return 0;
}
//###########################################################
0
 
mahesh1402Commented:
#define DLG_INVOKE_MSG 12345 <====

declare it as I said above ..dont use any constant values.. rather use as preferably WM_APP + 1

#define DLG_INVOKE_MSG (WM_APP + 1)

Rest is ok.

-MAHESH
0
 
tsp2002Author Commented:
#define DLG_INVOKE_MSG (WM_APP + 1)
I tried this....but I get an error .... can not compile....
does not allow    +1    in Marko-Parameter ????
0
 
mahesh1402Commented:
and what if you define that as WM_USER+1

#define DLG_INVOKE_MSG WM_USER+1

-MAHESH
0
 
tsp2002Author Commented:
Thank you MAHESH,
yes it works great. Thank you so much for your help.
I left you 500 points with grade A
Till next time....
Best regards,
Thomas
tsp2002 .... still a beginner in MFC
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now