Home > Software engineering >  MFC message response problem
MFC message response problem

Time:10-01

Build a CFormView window by VC6.0, and establish a non-modal dialog window frame, two Windows through the message response of the data communication, the parent window to the child window transfer no problem, but the child window to the parent window when the transmission message response function no response?
Specific code is as follows:
Non-modal window call:
Void CViewComTestView: : OnButton1 ()
{
//TODO: Add your the control notification handler code here
if(! Dlg1. M_hWnd)
{
Dlg1. Create (IDD_DIALOG1);
Dlg1. ShowWindow (SW_SHOW);
}
The else
{
Dlg1. DestroyWindow ();
Dlg1. Create (IDD_DIALOG1);
Dlg1. ShowWindow (SW_SHOW);
}
}

Add a message:
# define WM_MAIN_MSG WM_USER + 0 x01001
# define WM_SUB_MSG WM_USER + 0 x02001

Message response function definition:
BEGIN_MESSAGE_MAP (CMySubDlg CDialog)
//{{AFX_MSG_MAP (CMySubDlg)
ON_BN_CLICKED (IDC_BUTTON1 OnButton1)
//}} AFX_MSG_MAP
ON_MESSAGE (WM_SUB_MSG CMySubDlg: : OnSubMsg)
END_MESSAGE_MAP ()

BEGIN_MESSAGE_MAP (CViewComTestView CFormView)
//{{AFX_MSG_MAP (CViewComTestView)
ON_BN_CLICKED (IDC_BUTTON1 OnButton1)
ON_BN_CLICKED (IDC_BUTTON3 OnButton2)
//}} AFX_MSG_MAP
//Standard printing commands
ON_COMMAND (ID_FILE_PRINT CFormView: : OnFilePrint)
ON_COMMAND (ID_FILE_PRINT_DIRECT CFormView: : OnFilePrint)
ON_COMMAND (ID_FILE_PRINT_PREVIEW CFormView: : OnFilePrintPreview)
ON_MESSAGE (WM_MAIN_MSG CViewComTestView: : OnMainMsg)
END_MESSAGE_MAP ()

Child window send a message to the parent window:
Void CMySubDlg: : OnButton1 ()
{
//TODO: Add your the control notification handler code here
Cstrings strEdit;
GetDlgItemText (IDC_EDIT1 strEdit);

//get a handle to the parent window
HWND HWND=this - & gt; The GetParent () - & gt; GetSafeHwnd ();
//send a message to the parent window
If (hWnd==NULL) MessageBox (_T (" get the parent window handle failure!" ));
: : SendNotifyMessage (hWnd WM_MAIN_MSG, (WPARAM) & amp; StrEdit, NULL);
}

The message response function of the parent window: (child window when sending a message window without any response)
Afx_msg LRESULT CViewComTestView: : OnMainMsg (WPARAM WPARAM, LPARAM LPARAM)
{
WParam cstrings * strMsg=(cstrings *);
SetDlgItemText (IDC_EDIT1. * strMsg);
return 0;
}
Pray god give directions!!!!!!!!!!

CodePudding user response:

SendNotifyMessage messages sent not into circular queue.

Using SendMessage look,

CodePudding user response:

Dlg1. Create (IDD_DIALOG1, this);//try to specify the parent window

CodePudding user response:

HWND HWND=this - & gt; The GetParent () - & gt; GetSafeHwnd ();
Don't pass this way for the parent window handle
When creating a child window handle to the parent window when a variable of an assignment to the child window to use in the child window

CodePudding user response:

The
reference 3 floor xianglitian response:
HWND HWND=this - & gt; The GetParent () - & gt; GetSafeHwnd ();
Don't pass this way for the parent window handle
In creating a child window handle to the parent window when a variable assigned to child Windows in the child window using

I realize it, but I was using a pointer,

CodePudding user response:

SendNotifyMessage return the value judgment, try to switch to PostMessage

CodePudding user response:

reference 4 floor kakabulusi response:
Quote: reference xianglitian reply: 3/f

HWND HWND=this - & gt; The GetParent () - & gt; GetSafeHwnd ();
Don't pass this way for the parent window handle
In creating a child window handle to the parent window when a variable assigned to child Windows in the child window using

I realize it, but I was using a pointer,
handle relatively safer

CodePudding user response:

refer to 6th floor xianglitian response:
Quote: refer to 4th floor kakabulusi response:

Quote: refer to the third floor xianglitian response:

HWND HWND=this - & gt; The GetParent () - & gt; GetSafeHwnd ();
Don't pass this way for the parent window handle
In creating a child window handle to the parent window when a variable assigned to child Windows in the child window using

I realize it, but I was using a pointer,
handle is relatively safe some

Pointer speed faster, but afraid of is operation address change or to content in the process of change, the handle is feasible, but if the handle is dynamic window will have some problems,
Anyway, your approach to me is also a very good communication, thanks! Thank you!!!!!
  • Related