Search code examples
htmlc++visual-studio-2013mfc

Move Window in mfc c++ without titlebar?


I am using this code to move the window. But this code does not work well. When I click anywhere on windows from it will move but i just want to move windows form. When i click on specific think. For example picture. I am using MFC C++ HtmlDialog. Anyone know how to do that?

DHTML_EVENT_ONCLICK(_T("image"), PreTranslateMessage)

BOOL CHtmlDlgTestDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_MOUSEMOVE && (pMsg->wParam & MK_LBUTTON))
{
    CPoint p;
    GetCursorPos(&p);
    CRect r;
    GetWindowRect(&r);
    if (r.PtInRect(p))
    {
        ReleaseCapture();
        SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
        SendMessage(WM_NCLBUTTONUP, HTCAPTION, 0);
        return 1;
    }
}

return CDialog::PreTranslateMessage(pMsg);    
}

Solution

  • WM_NCLBUTTONDOWN is a notification message, Windows sends this message and the program responds to it. The program should not send this message to Windows. In this case it works but it's not recommended.

    I don't know how this code works: DHTML_EVENT_ONCLICK(_T("image"), PreTranslateMessage) it probably gets ignored and you can remove it. PreTranslateMessage is still called. You can restrict it to any rectangle within the Window, for example CRect(50,50,200,200):

    BOOL CHtmlDlgTestDlg::PreTranslateMessage(MSG* pMsg)
    {
       if (pMsg->message == WM_MOUSEMOVE && (pMsg->wParam & MK_LBUTTON))
       {
           CPoint p = pMsg->pt;
           ScreenToClient(&p);
           CRect r(50,50,200,200);
           if (r.PtInRect(p))
           {
               ReleaseCapture();
               SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
               SendMessage(WM_NCLBUTTONUP, HTCAPTION, 0);
               return 1;
           }
       }
       return CDialog::PreTranslateMessage(pMsg);
    }
    

    If you want to move an element within the window you can use javascript:

    Moveable/draggable <div>

    Ps, normally you should use WM_NCHITTEST as explained earlier. This case is very unusual because it's HTML dialog. You should reconsider putting a normal title bar which users understand, or you could put html control within a dialog, then you can control the rest of the dialog with standard WinApi.