I created a check box in a dialog box and trying to access its condition whether checked or not. This is my code:
CButton *m_ctlCheckBlack = (CButton *)GetDlgItem(IDC_BLACK);
int chkBoxBlack = m_ctlCheckBlack->GetCheck();
As I run through this code, it pops up an exception saying :
Exception thrown at 0x0FA45564 (mfc140d.dll) in braille_obr.exe: 0xC0000005:
Access violation reading location 0x00000020.
any help will be appreciated. thank you.
There is rarely a good reason to use GetDlgItem. In your resource editor, right click the button and 'Add Variable..'
It will default as a control. Give it a name. You will get a member in the dialog class:
CButton myButtonName;
Now it should be safe to:
myButtonName.GetChecked( );
as it will have been created and properly subclassed.
Perhaps, I doubt it tried to get the state of button when the Button was not yet constructed.
int chkBoxBlack = m_ctlCheckBlack != NULL ? m_ctlCheckBlack->GetCheck() : 0;
Or, do you try to operate from outside dialog?
If it is so, you had better try to do this.
■CButton *m_ctlCheckBlack = (CButton *)yourdlg.GetDlgItem(IDC_BLACK);
■FindWindowEx(yourdlg.GetSafeHwnd(), NULL, NULL, "(your button caption)");
Related
When i use GetMenu()->GetSubMenu(2)-> 'xxx', there is an error:
Exception thrown at 0x00007FFEF70E4FD3 (mfc140d.dll) (in Menu.exe): 0xC0000005: An access violation occurred while reading location 0x0000000000008.
I added the above code at the end of the OnCreate function in the CMainFrame class.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){
GetMenu()->GetSubMenu(2)->CheckMenuItem(0, MF_BYPOSITION | MF_CHECKED);
}
3.Class "Cxxxx" does not have member "GetContextMenuManager" 'GetContextMenuManager': is not a member of'Cxxxx' After analysis, it is found that the current class Cxxx is inherited from CWinApp, which does not have the GetContextMenuManager function. If you select Use Menu Bar and Toolbar when creating a project, no error is reported. After analysis, it is found that Cxxx is derived from CWinAppEx. It has the GetContextMenuManager() member method. Therefore, do not report an error, manually modify the derived class.
It's true that GetMenu() got a null pointer. I found two solutions:
Change the value of CMFCMenuBar m_wndMenuBar in Mainfrm.h mainfrm.cpp. Delete the code of the. The compilation is successful.
When creating a project, select Use a classic menu.
(There's a TL;DR on the last line)
I'm implementing a handler to close selected windows open in a software application. Here's a rough code:
void CDlg_Dummy_Dialog::OnCloseWindows()
{
for (int i = 0; i < m_WindowsInfo.size(); i++) {
Window_Node *pWN = &m_WindowsInfo.at(i);
if (pWN->checked && IsWindow(pWN->pWnd->GetSafeHwnd())) {
pWN->pWnd->GetParentFrame()->SendMessage(WM_CLOSE);
}
}
}
Here are some declarations of the parameters shown above:
struct Window_Node {
CString name;
CString path;
CWnd *pWnd;
BOOL checked;
HICON icon;
....
};
class CDlg_Dummy_Dialog : public CDialog {
...
protected:
std::vector<struct Window_Node> m_WindowsInfo;
...
}
Also, there can be multiple instances of Window_Node with different pWnd parameter, originating from a single CDocument class (ie. different types of windows exist to show different displays for the document).
For this software, if the first window of the document (which is always the "green" Window type in the diagram) is closed, all other windows associated with that document will automatically be closed with them. This is where the problem happens.
If the user selects multiple windows from the same document (with the green window among them), it closes all windows by the time it finishes the first iteration, and all pWnd pointers are now pointing to a now unassigned memory. Therefore, when it tries to call GetSafeHwnd() on the next iteration, it prompts a memorry access violation error:
First-chance exception at 0x00000000521B4AD0 (mfc100d.dll) in Settle3D.exe: 0xC0000005: Access violation reading location 0x00000000136943E0.
Unhandled exception at 0x00000000521B4AD0 (mfc100d.dll) in Settle3D.exe: 0xC000041D: An unhandled exception was encountered during a user callback.
I'm aware that the easy fix would be to iterate through the vector in the opposite direction. However, I am trying to integrate this method on several other software as well, and they don't necessarily organize their windows in the same manner.
So, after all the long question above, here's TL;DR:
Is there any way to check if an instance of CWnd* is pointing to a valid window?
One possibility would be to start with your main window, and recursively search through the child windows if you find the HWND in question.
Get the first child with CWnd::GetWindow(GW_CHILD) and the next windows with CWnd::GetWindow(GW_HWNDNEXT).
I have a Modeless dialog which shows a bunch of buttons; some of these are customized to draw stuff with GDI.
Now, when the user clicks on a customized one under certain conditions, a message box appears to alert user of the error and this is fine.
The problem is that after accepting the Message Box (showed as MB_ICON_ERROR), everywhere I click in the dialog, I always get the error message as if the whole dialog send the message to the customized button and the only way to get rid this is to press tab and give the focus to another control.
This is a strange behaviour and knowing why happens wouldn't be bad, but a simple workaround for now should do the job.
Since the moment that is probably a matter of focus, I've tried to set it on another control (in the owner dialog) by doing:GetDlgItem( IDC_BTN_ANOTHER_BUTTON )->SetFocus();
and then, inside the customized control by adding:KillFocus( NULL );but had no results.
How should I use these functions?
Thanks in advance.
PS: if I comment the AfxMessageBox, the control does not show this bizarre behaviour.
EDITI'll show some code as requested.
// This is where Message Box is popping out. It is effectively inside the dialog code.
void CProfiloSuolaDlg::ProcessLBtnDownGraphProfilo(PNT_2D &p2dPunto)
{
// m_lboxProfiles is a customized CListBox
if(m_lboxProfiles.GetCurSel() == 0)
{
// This profile cannot be modified.
/*
CString strMessage;
strMessage.Format( _T("Default Profile cannot be edited.") );
AfxMessageBox( strMessaggio, MB_ICONERROR );
*/
return;
}
// Selecting a node from sole perimeter.
SelectNodo(p2dPoint);
}
Actually, the message is commented to keep the dialog working.
// This is inside the customization of CButton
void CMyGraphicButton::OnLButtonDown(UINT nFlags, CPoint point)
{
PNT_2D p2dPunto;
CProfiloSuolaDlg* pDlg = (CProfiloSuolaDlg*)GetParent();
m_pVD->MapToViewport(point,p2dPunto);
switch(m_uType)
{
case GRF_SEZIONE:
pDlg->ProcessLBtnDownGraphProfilo(p2dPunto);
break;
case GRF_PERIMETRO:
pDlg->ProcessLBtnDownGraphPerimetro(p2dPunto);
break;
}
CButton::OnLButtonDown(nFlags, point);
}
Since you are handling the button down event in the button handler for the custom control, you don't need to call the base class. Just comment out CButton::OnLButtonDown(nFlags, point).
Why second line of code below rises exception
StatusDlg statusDlg (CWnd::GetDesktopWindow());
statusDlg.ShowWindow(SW_SHOW);
and code below goes without problems
StatusDlg * statusDlg = new StatusDlg(NULL);
statusDlg->Create(StatusDlg::IDD,CWnd::GetDesktopWindow());
statusDlg->ShowWindow(SW_SHOW)
?
Because the first dosen't create the dialog's underlying window?
I am working on a MFC project with Kinect that uses a FPS counter. This is a static text. I want this FPS counter to be editted by a method which is also responsible for make the kinect images. So in my main I pass the HWND on to my kinect object like this:
kinect->initialize(this->GetSafeHwnd());
In my kinect object this thing gets saved:
this->hwnd = hWnd;
and then this piece of code to initialize the Static Text I want to use:
CStatic * MFC_ecFPSCOLOR;
MFC_ecFPSCOLOR = (CStatic *) GetDlgItem(hWnd, TC_FPSCOLOR);
And then I want to use it:
MFC_ecFPSCOLOR->SetWindowTextW(L"TEST");
And here it goes wrong:
Unhandled exception at 0x54431C19 (mfc110ud.dll) in KinectMain.exe: 0xC0000005: Access violation reading location 0x0031004C.
Can someone point me in the right direction?
You're using the wrong version of GetDlgItem, it returns a handle and not a CWnd*. The cast is hiding the error from you.