Cwnd returnd Undefined Value - mfc

I want to use Setwindowpos() in global function to change the postiion of one dialog.
So i coded like below
CWnd *pWnd = (CWnd*)pMain->GetDlgItem(IDD_TOOLBAR_DIALOG);//GetActiveFrame ();
pWnd->SetWindowPos(&CWnd::wndBottom,0,80,45,900,SWP_SHOWWINDOW);
pWnd returns as undefined value.
So i follow this method
CToolTab * tab;
tab = new CToolTab();
if(tab->GetSafeHwnd() == 0)
{
tab = new CToolTab();
if( tab->m_hWnd != NULL )
{
tab->SetWindowPos(&CWnd::wndBottom,0,80,45,900,SWP_SHOWWINDOW); }
}
But m_hWnd returns 0x0000.So in both cases SetWindowPos shows assertion error.
How can i get the handle of dialog in Global function?

Ok so i was thinking this through - and then it occurred to me that IDD_TOOLBAR_DIALOG is almost certainly the resource id for the dialog itself not a control on the dialog. You can't call GetDlgItem referencing the Dialog resource ID and expect to find the window that contains it (It simply won't work and you'll get back a null CWnd* pointer).
What is it that pMain points to? The main application window? Is that the parent to the Dialog?
Where in the code do you create the Dialog that want to move? At that point you must have a valid CWnd derived object for it? One solution would be to hold on to that as member variable to allow you call SetWindowPos on it later when necessary.

Related

How to show Cdialog with std::async

I have a derive class named A was inheritance from CDialog, I created the object to A named a and want to utilize the member function domodal to show dialog. Nonetheless, this dialog cannot show and parent window was block.
A a(this);
auto DlgResult = std::async(std::launch::async, &A::DoModal,&a);
DlgResult.wait();
if (DlgResult.get() == IDOK)
{
std::wstring ss = a.get_text_fromdlg();
}
Can someone help me, thanks!
If I were you, I wouldn't wrestle w/ the Async and DoModal since the purpose of DoModal() is to wait for the response from the dialog to let the app know how to move forward..
Below, I've added a simpler option. Just create member variable pointer to a Dialog class, and then use Show Window. Also, in this instance, you may consider making the dialog topmost so you don't lose focus of it.
MFCClass1* m_pDlg = new MFCClass1();
void CMFCApplication1Dlg::OnBnClickedButton1()
{
m_pDlg->Create(IDD_DIALOG1);
m_pDlg->ShowWindow(SW_SHOWNORMAL);
SetWindowPos(&m_pDlg->wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}

MFC Send message to a button (child to parent)

I want to send a message from my child window (CDialog) to the parent window (CFormview). If I press the cancel button at the child window, the Dialog should quit and the program should continue with the code of the STOP-Button at the parent Window.
void ChildDialog::OnBnClickedCancel()
{
CDTParentView *pButtonWnd = (CDTParentView *)AfxGetMainWnd();
pButtonWnd->OnBnClickedbuttonStop();
CDialogEx::OnCancel();
}
but there is an error in this objore.cpp:
BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const
{
ENSURE(this != NULL);
// it better be in valid memory, at least for CObject size
ASSERT(AfxIsValidAddress(this, sizeof(CObject)));
// simple SI case
CRuntimeClass* pClassThis = GetRuntimeClass(); <------- error
ENSURE(pClassThis);
return pClassThis->IsDerivedFrom(pClass);
}
Can anyone tell me, whats the problem?
And maybe post a better idea to send the button-clicked message?
Your code isn't actually sending a message, it's trying to call the handler directly. It's easy to simulate the clicking of a button the same way Windows would do it, then your existing code will handle it naturally.
CWnd * pMain = AfxGetMainWnd();
CWnd * pButton = pMain->GetDlgItem(ID_STOP_BUTTON);
pMain->PostMessage(WM_COMMAND, MAKEWPARAM(ID_STOP_BUTTON, BN_CLICKED), (LPARAM)pButton->m_hWnd);
AfxGetMainWnd does not return a pointer to the CFormView, it returns a pointer to the CMainFrame. If your dialog is modal you can simply check the return value of the DoModal call that displays the dialog. Or you might have better luck with calling GetParent to get a pointer to the CFormView.

Disabling dialog OK button MFC

How do I disable MFC dialog OK button?
This code:
CWnd* fieldOK = pDlg->GetDlgItem(IDOK);
fieldOK->EnableWindow(FALSE);
causes exception "Access violation reading location..."
in line ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); of function CWnd::EnableWindow(BOOL bEnable) in winnocc.cpp from mfc90d.dll
In this time focus is on another control.
What's can be wrong?
Thanks for help.
[EDITED]
bool CSCalcNormCell::OnSelectionChanged( CWnd* pDlg, int type, int page, UINT ctrl_id )
{
DDX_DataBox(pDX.get(), IDC_WORKSHOP_COMBO, ws_code);
if (!CInfactoryPriceAdapter::CanEditPricesForWorkshop( ws_code ))
{
CWnd* fieldOK = pDlg->GetDlgItem(IDOK);
fieldOK->EnableWindow(FALSE);
}
else
{
CWnd* fieldOK = pDlg->GetDlgItem(IDOK);
fieldOK->EnableWindow(TRUE);
}
}
I'm not sure why would wouldn't be able to do it. If I take a regular CDialog and I do an init like this:
BOOL CMyDialog::OnInitDialog() {
CDialog::OnInitDialog();
CWnd *okbtn = GetDlgItem( IDOK );
if ( okbtn ) {
okbtn->EnableWindow( FALSE );
}
return TRUE;
}
it disables the button just fine. Perhaps something else is wrong?
Try this: http://support.microsoft.com/kb/122489
How to Disable Default Pushbutton Handling for MFC Dialog
Although default button (pushbutton) support is recommended, you might
want to disable or modify the standard implementation in certain
situations. You can do this in an MFC application by following these
steps:
Load the dialog into App Studio and change the OK button identifier
from IDOK to something else such as IDC_MYOK. Also, clear the check
from Default Button property.
Use ClassWizard to create a message
handling function for this button named OnClickedMyOK. This function
will be executed when a BN_CLICKED message is received from this
button.
In the code for OnClickedMyOK, call the base class version of
the OnOK function. Here is an example:
void CMyDialog::OnClickedMyOK()
{
CDialog::OnOK();
}
Override OnOK for your dialog, and do nothing inside the function. Here is an example:
void CMyDialog::OnOK()
{
}
Run the program and bring up the dialog. Give focus to a control other
than the OK button. Press the RETURN key. Notice that CDialog::OnOK()
is never executed.
I suspect the problem comes from pDlg pointer. When you call pDlg->GetDlgItem(IDOK), is the dialog already created already?
Make a breakpoint at the line CWnd* fieldOK = pDlg->GetDlgItem(IDOK); and debug into it to see if fieldOK pointer is null or a valid pointer.
That is why I think mark's answer is very close. You can disable it onOnInitDialog` or other members of you dialog class after it showed up.
The problem you have is that the button control has not been created on the interface yet. We do not get the full vision of your problem.
Anyway, you should protect your code from crashing. It is better that your code does nothing than to crash the application. Restructuring it like this avoids the access violation problem due to the NULL pointer:
bool CSCalcNormCell::OnSelectionChanged( CWnd* pDlg, int type, int page, UINT ctrl_id )
{
DDX_DataBox(pDX.get(), IDC_WORKSHOP_COMBO, ws_code);
CWnd* fieldOK = pDlg->GetDlgItem(IDOK);
if (fieldOK)
{
if (!CInfactoryPriceAdapter::CanEditPricesForWorkshop( ws_code ))
fieldOK->EnableWindow(FALSE);
else
fieldOK->EnableWindow(TRUE);
}
}
You need to load a bitmap for the disable mode of the OK button in LoadBitmaps() function.

mfc access to formview item from dialog box

In my SDI application i need to get this behawiour. After I click on a button on the FormView, a CDialog opens. When I press the OK button on the CDialog, I call a function of the FormView. I don't want to close the CDialog. I try to do it with modeless dialog, but when i call formview function from dialog, i can't access to formview's control, like it's lost hwnd; the error is can't read memory of m_hwnd, the hwnd is ???.
This is my code:
Open modeless dialog:
CCampiDlg *m_pDialog = NULL;
HWND hCampi = NULL;
// Invoking the Dialog
m_pDialog = new CCampiDlg;
if (m_pDialog != NULL)
{
BOOL ret = m_pDialog->Create(m_pDialog->IDD, this);
if (!ret) //Create failed.
{
AfxMessageBox(_T("Error creating Dialog"));
}
m_pDialog->ShowWindow(SW_SHOW);
}
when i press the ok button in the dialog i do:
CEditorTxView pView;
box2 = (CEdit*)(GetDlgItem(IDC_CAMPI_BOX2));
box2->GetWindowTextW(campo);
pView.inserisciCampo(1, campo);
In inserisciCampo function in CEditorTxView (CFormView) i have to do operation with my control txtCtrl, but it's lost hwnd. The declaration of txtCtrl is in the CEditorTxView.h
CTx1 txtCtrl;
And initialize it in DoDataExchange function:
void CEditorTxView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TX1, txtCtrl);
}
Someone can help me plz?
I can give you two answers here:
How to do what you are asking (get access to a control of the CFormView from a modeless dialog)
How to solve your underlying problem (communicate changes in a modeless dialog to the owner view)
For the first one, you have to declare a pointer to the view in the dialog class and initialize it in the constructor of the view:
class CCampiDlg : public CDialog
{
public:
CCampiDlg(CEditorTxView* pView, CWnd*pParent = NULL) // Change declaration to add pointer to view
: m_pView(pView)
{
}
// ... Whatever
private:
CEditorTxView* m_pView;
}
Now in your button handler:
CEdit* box2 = (CEdit*)(GetDlgItem(IDC_CAMPI_BOX2)); // Why not use a control variable?
box2->GetWindowTextW(campo);
m_pView->inserisciCampo(1, campo);
This should do what you are asking for. However, it is the wrong way to do it.
The problem with this approach is that the dialog knows way too much about its parent. It knows it is of type CEditorTxView and that it has a member called inserisciCampo, that takes a number and some text.
It shouldn't know that much. In fact, knowing anything about it, other than it is of type CView or even CWnd, is too much.
If the dialog knows about the view, you can't reuse the dialog with other views, and anytime the view changes its representation (what now is a textbox may be a combobox in the future, for example) the dialog must change accordingly.
The solution would be to send a message to the parent, explaining what's happened. Then the parent (the view) should know haw to handle that event. For example:
class CCampiDlg : public CDialog
{
public:
CCampiDlg(CWnd*pParent = NULL) {}
protected:
OnOk()
{
CString campo;
c_CampiBox2.GetWindowText(campo);
GetParent()->SendMessage(UWM_CAMPO2_SET, 0, (LPARAM)&campo);
}
}
In the view:
// It can be ON_REGISTERED_MESSAGE:
ON_MESSAGE(UWM_CAMPO2_SET, OnCampo2Set)
//...
LRESULT CEditorTxView::OnCampo2Set(WPARAM, LPARAM lParam)
{
CString* s = (CString*) lParam;
inserisciCampo(1, *campo);
return 0;
}
Now, you have decoupled the view and the dialog. The dialog knows nothing about the view. You can change its type, change the representation, even make it a dialog, and you don't have to change anything in the dialog. And if you need that same modeless dialog somewhere else, you just drop it there, create a message handler in the parent, and voilĂ !
For further explanations and better examples, check these articles:
Dialog and control design (Your case is explained in the section "Notifications to the environment")
Message management
On Ok button click the below code is running:
CEditorTxView pView;
box2 = (CEdit*)(GetDlgItem(IDC_CAMPI_BOX2));
box2->GetWindowTextW(campo);
pView.inserisciCampo(1, campo);
Note that, you are creating the new pView in stack and it does't attach with any window. You are not actually referring the view that already created and launched your dialog acting a parent. Revisit the above code and try the get the view:
Try the below code, if it is not working (Google it)
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
CView * pView = pFrame->GetActiveView();

Can I return a custom value from a dialog box's DoModal function?

What I wish to do is, after creating a dialog box with DoModal() and pressing OK in the box to exit it, to have a custom value returned. For example, a couple of strings the user would input in the dialog.
You can't change the return value of the DoModal() function, and even if you could, I wouldn't recommend it. That's not the idiomatic way of doing this, and if you changed its return value to a string type, you would lose the ability to see when the user canceled the dialog (in which case, the string value returned should be ignored altogether).
Instead, add another function (or multiple) to your dialog box class, something like GetUserName() and GetUserPassword, and then query the values of those functions after DoModal returns IDOK.
For example, the function that shows the dialog and processes user input might look like this:
void CMainWindow::OnLogin()
{
// Construct the dialog box passing the ID of the dialog template resource
CLoginDialog loginDlg(IDD_LOGINDLG);
// Create and show the dialog box
INT_PTR nRet = -1;
nRet = loginDlg.DoModal();
// Check the return value of DoModal
if (nRet == IDOK)
{
// Process the user's input
CString userName = loginDlg.GetUserName();
CString password = loginDlg.GetUserPassword();
// ...
}
}
I was looking for an answer and agree that in most cases that you would not change the standard behavior of a dialog. But there might be a case where you would like to pick what the user is actually responding say if you had several buttons and want specifically that they picked the OK at the top versus the OK at the bottom. You know for metrics.
Or say if you wanted to have slightly different results if the dialog caused an error when running on of your functions. It would be nice to return a value that is not IDOK but maybe some other value.
I found Dialog::EndDialog() with details and an example of usage here: MSDN: Dialog::EndDialog
#include "ANewDialog.h"
void CMyWnd::ShowDialog()
{
CMyDialog myDlg;
int nRet = myDlg.DoModal();
if ( nRet == 18 )
AfxMessageBox("Dialog closed. But there was a problem.");
}
/* MyDialog.cpp */
void CMyDialog::OnSomeButtonAction()
{
int nRet = 0;
// Run your function with return value;
nRet = YourReallyFunFunction();
EndDialog(nRet); // Set the return value returned by DoModal!
return; // The dialog closes and DoModal returns here!
}
I don't think it is possible (or reasonable). DoModal returns an INT_PTR, which is usually used to know what the user did to exit the dialog (press OK, Cancel, there was an error...). The way to do it is have public members or functions which the dialog set and the caller of the dialog can access to know the values.
Like so:
CMyDialog dlg;
if(dlg.DoModal()==IDOK)
{
CString str1 = dlg.m_String1;
CString str2 = dlg.GetString2();
}
It's the way you would use CFileDialog, for example.