发送和将消息从一个类发送到另一堂课的问题.MFC程序

  • 本文关键字:程序 MFC 问题 消息 一个 c++ mfc
  • 更新时间 :
  • 英文 :


这是我的代码:当我单击鼠标的左键并发送TE消息时,此功能是调用的:

#define WM_MYMESSAGE WM_USER+7
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{

     counter=0;
    CWnd::OnLButtonDown(nFlags, point);
    CRect wdRect;
    GetClientRect(&wdRect);
    HWND hwnd;
    hwnd=::FindWindow(NULL,"Client");
    if(wdRect.PtInRect(point))
    {
        counter++;
        PostMessage(WM_MYMESSAGE,point.x,point.y);
    }
}

在另一个文件mainfraim.cpp中,借助on_message(wm_mymessage,onnamemsg),我将消息发送到onnamemsg函数。此功能打开BMP文件。问题在于函数OnnameMSG对消息没有响应,并且此功能不起作用。我该怎么做才能使此函数在此消息中响应。你能帮我解决这个问题吗?这是代码。

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_WM_CREATE()
    ON_WM_SETFOCUS()
    ON_MESSAGE(WM_MYMESSAGE, OnNameMsg)
    ON_COMMAND(ID_EDIT_LINE, OnEditLine)

END_MESSAGE_MAP()

afx_msg LRESULT CMainFrame::OnNameMsg(WPARAM wParam,LPARAM IParam)
{
    MSG msg;
    char FileName[500];
char FileTitle[100];
FileName[0]=''; 
GetMessage(&msg,NULL,WM_MOUSEFIRST,0);
CFileDialog file(TRUE);
file.m_ofn.lpstrFilter=TEXT("Bitmap picture files *.bmp*.bmpAll Files *.**.*");
file.m_ofn.lpstrFileTitle=FileTitle;
file.m_ofn.lpstrFile=FileName;
file.m_ofn.lpstrTitle="Open BMP File";
file.DoModal(); 
//if (FileName[0]=='')return;
SetWindowText(FileTitle); 
HANDLE hdibCurrent1 = OpenDIB(FileName);
hbm=0; 
hbm=BitmapFromDib(hdibCurrent1,0); 
GetObject(hbm,sizeof(BITMAP),(LPSTR)&bm); 
CRect wdRect;
GetClientRect(&wdRect);
ClientToScreen(&wdRect); 
    int j=wdRect.Height();
    int i=wdRect.Width();
    //SetWindowPos(NULL,wdRect.left,wdRect.top, i,j,NULL); 

    if(hbm) { CClientDC dc(this); 
        HDC hdc=::GetDC(m_hWnd);
        HDC hdcBits=::CreateCompatibleDC(hdc); 
        SelectObject(hdcBits,hbm); 
        //CRect wdRect;
        GetClientRect(&wdRect);
        CBrush brush;
        brush.CreateSolidBrush(RGB(0,0,0));
        dc.FillRect(&wdRect,&brush); 
        BitBlt(hdc, 0, 0, bm.bmWidth,bm.bmHeight,hdcBits,0,0, SRCCOPY); 
        DeleteDC(hdcBits);
        ::ReleaseDC(m_hWnd,hdc);
    } 

    return 1;
}

您将消息发送给CChildView,而不是将消息发送到CMainFrame。由于您想将消息发送到主框架并在此处处理,因此必须将(PostMessage)发送到主帧的窗口句柄。

您正在调用的PostMessage是方法来自CWnd,该方法与::PostMessage API不同,因此将其发送到this。您需要CMainFrame的指针,然后致电该指针。假设您将主帧的指针纳入pMainFrame,然后可以致电:

pMainFrame->PostMessage(WM_MYMESSAGE,point.x,point.y);

相关内容

最新更新