2

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);    
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Muhammad Raza
  • 847
  • 1
  • 8
  • 22

1 Answers1

2

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.

Community
  • 1
  • 1
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Sir, How i can perform this task. When i just click on ok button. Mean When i press ok button then i am able to move window. Actually no press just push it down? – Muhammad Raza Jun 05 '15 at 08:39
  • @MuhammadRaza: IMHO this is not the way to go. Use IInspectable's solution. – Jabberwocky Jun 05 '15 at 08:42
  • @MichaelWalz in this case `OnNcHitTest` doesn't work because it's an html dialog. Also the SO's requirement is unreasonablein trying to move html window by clicking OK button within the html window. – Barmak Shemirani Jun 05 '15 at 08:44
  • Sir, Barmak Shemirani. What i just want when i just press button ok down. I am able to move whole windows form any where in the screen. And if i click on any another place the windows is not going to move. You are correct OnNcHitTest is not work with Mfc html dialog. – Muhammad Raza Jun 05 '15 at 08:47
  • @BarmakShemirani correct. I didn't know that. Thanks. – Jabberwocky Jun 05 '15 at 08:49
  • @MuhammadRaza Get the coordinates of the OK button. If the coordinates are at `50,50,200,200` then the edit that I made should work. – Barmak Shemirani Jun 05 '15 at 08:55
  • Sir i understand you and thank you. You help me lot. Just one think which rectangle we make. How to color that one to black? – Muhammad Raza Jun 05 '15 at 09:00
  • @MuhammadRaz I am not sure what you are referring to. You could just remove the OK button and put in a black rectangle instead, because the OK button is no longer functioning as OK button anyway. – Barmak Shemirani Jun 05 '15 at 09:16