c++ - MFC Dragging Objects -
i painted 3 objects onto window using cpaintdc
, , trying 1 of them (the ellipse) become draggable using onmousemove
event handler. playing around mfc , lost how work.
this have tried in onmousemove
:
// todo: add message handler code here and/or call default cclientdc dc(this); crect rect; getclientrect(&rect); dc.setmapmode(mm_anisotropic); dc.setwindowext(100, 100); crect rectellipse(ellipse.cx, ellipse.cy,(ellipse.cx) + 10, (ellipse.cy) + 10);//(10, 10, 20, 20);dc.ellipse(ellipse.cx, ellipse.cy, (ellipse.cx) + 10, (ellipse.cy) + 10);//10, 10, 20, 20); //cpoint ellipsedest; //dc.setviewportext(rect.width(), rect.height()); if (mousecaptured) { ellipse.cx = point.x; ellipse.cy = point.y; invalidaterect(rectellipse, true); } cdialogex::onmousemove(nflags, point);
please find code pasted below.
thanks help.
#include "stdafx.h" #include "csit861a3 vasilkovskiy.h" #include "csit861a3 vasilkovskiydlg.h" #include "afxdialogex.h" #ifdef _debug #define new debug_new #endif // caboutdlg dialog used app class caboutdlg : public cdialogex { public: caboutdlg(); // dialog data enum { idd = idd_aboutbox }; protected: virtual void dodataexchange(cdataexchange* pdx); // ddx/ddv support // implementation protected: declare_message_map() public: // afx_msg void onlbuttondown(uint nflags, cpoint point); // afx_msg void onlbuttonup(uint nflags, cpoint point); // afx_msg void onmousemove(uint nflags, cpoint point); }; caboutdlg::caboutdlg() : cdialogex(caboutdlg::idd) { } void caboutdlg::dodataexchange(cdataexchange* pdx) { cdialogex::dodataexchange(pdx); } begin_message_map(caboutdlg, cdialogex) // on_wm_lbuttondown() // on_wm_lbuttonup() // on_wm_mousemove() end_message_map() // ccsit861a3vasilkovskiydlg dialog ccsit861a3vasilkovskiydlg::ccsit861a3vasilkovskiydlg(cwnd* pparent /*=null*/) : cdialogex(ccsit861a3vasilkovskiydlg::idd, pparent) { m_hicon = afxgetapp()->loadicon(idr_mainframe); ellipsecolor = rgb(255, 0, 0); centerrectcolor = rgb(0, 0, 0); rightrectcolor = rgb(255, 0, 0); centerrect.left = 40; centerrect.top = 20; centerrect.right = 55; centerrect.bottom = 80; rightrect.left = 75; rightrect.top = 35; rightrect.right = 90; rightrect.bottom = 50; ellipse.cx = 10; ellipse.cy = 10; mousecaptured = false; } void ccsit861a3vasilkovskiydlg::dodataexchange(cdataexchange* pdx) { cdialogex::dodataexchange(pdx); } begin_message_map(ccsit861a3vasilkovskiydlg, cdialogex) on_wm_syscommand() on_wm_paint() on_wm_querydragicon() on_wm_lbuttondown() on_wm_lbuttonup() on_wm_mousemove() end_message_map() // ccsit861a3vasilkovskiydlg message handlers bool ccsit861a3vasilkovskiydlg::oninitdialog() { cdialogex::oninitdialog(); // add "about..." menu item system menu. // idm_aboutbox must in system command range. assert((idm_aboutbox & 0xfff0) == idm_aboutbox); assert(idm_aboutbox < 0xf000); cmenu* psysmenu = getsystemmenu(false); if (psysmenu != null) { bool bnamevalid; cstring straboutmenu; bnamevalid = straboutmenu.loadstring(ids_aboutbox); assert(bnamevalid); if (!straboutmenu.isempty()) { psysmenu->appendmenu(mf_separator); psysmenu->appendmenu(mf_string, idm_aboutbox, straboutmenu); } } // set icon dialog. framework automatically // when application's main window not dialog seticon(m_hicon, true); // set big icon seticon(m_hicon, false); // set small icon // todo: add initialization here return true; // return true unless set focus control } void ccsit861a3vasilkovskiydlg::onsyscommand(uint nid, lparam lparam) { if ((nid & 0xfff0) == idm_aboutbox) { caboutdlg dlgabout; dlgabout.domodal(); } else { cdialogex::onsyscommand(nid, lparam); } } // if add minimize button dialog, need code below // draw icon. mfc applications using document/view model, // automatically done framework. void ccsit861a3vasilkovskiydlg::onpaint() { //create pen , pointer old pen cpen blackpen; blackpen.createpen(ps_solid, 1, rgb (0, 0, 0)); //create bursh cbrush blackbrush(centerrectcolor); cbrush redbrush(rightrectcolor); cbrush hatchredbrush(hs_cross, ellipsecolor); //set object painting cpaintdc dc (this); crect rect; getclientrect (&rect); dc.setmapmode (mm_anisotropic); dc.setwindowext (100, 100); dc.setviewportext (rect.width (), rect.height ()); //select pen dc.selectobject(&blackpen); //rectangle red interior color dc.selectobject(&redbrush); dc.rectangle(rightrect); //(75, 35, 90, 50); //rectangel black interior dc.selectobject(&blackbrush);//select brush dc.rectangle( centerrect);//(40, 20, 55, 80); //ellipse dc.selectobject(&hatchredbrush);//select brush dc.ellipse(ellipse.cx, ellipse.cy, (ellipse.cx) + 10, (ellipse.cy) + 10);//10, 10, 20, 20); if (isiconic()) { cpaintdc dc(this); // device context painting sendmessage(wm_iconerasebkgnd, reinterpret_cast<wparam>(dc.getsafehdc()), 0); // center icon in client rectangle int cxicon = getsystemmetrics(sm_cxicon); int cyicon = getsystemmetrics(sm_cyicon); crect rect; getclientrect(&rect); int x = (rect.width() - cxicon + 1) / 2; int y = (rect.height() - cyicon + 1) / 2; // draw icon dc.drawicon(x, y, m_hicon); } else { cdialogex::onpaint(); } } // system calls function obtain cursor display while user drags // minimized window. hcursor ccsit861a3vasilkovskiydlg::onquerydragicon() { return static_cast<hcursor>(m_hicon); } //void caboutdlg::onlbuttondown(uint nflags, cpoint point) //{ // // todo: add message handler code here and/or call default // //setcapture(); // cclientdc dc(this); // crect rect; // getclientrect(&rect); // dc.setmapmode(mm_anisotropic); // dc.setwindowext(100, 100); // dc.setviewportext(rect.width(), rect.height()); // crect rectellipse(10, 10, 20, 20); // // ellipsecolor = rgb(0,0,0); // // cdialogex::onlbuttondown(nflags, point); //} //void caboutdlg::onlbuttonup(uint nflags, cpoint point) //{ // // todo: add message handler code here and/or call default // releasecapture (); // cdialogex::onlbuttonup(nflags, point); //} //void caboutdlg::onmousemove(uint nflags, cpoint point) //{ // // todo: add message handler code here and/or call default // // cdialogex::onmousemove(nflags, point); //} void ccsit861a3vasilkovskiydlg::onlbuttondown(uint nflags, cpoint point) { // todo: add message handler code here and/or call default cclientdc dc(this); crect rect; getclientrect(&rect); dc.setmapmode(mm_anisotropic); dc.setwindowext(100, 100); dc.setviewportext(rect.width(), rect.height()); crect rectellipse(ellipse.cx, ellipse.cy, 20, 20);//(10, 10, 20, 20); crgn circle; dc.lptodp(rectellipse); circle.createellipticrgnindirect(rectellipse); if (circle.ptinregion(point)) { setcapture(); mousecaptured = true; ellipsecolor = rgb(0,0,255); invalidaterect(rectellipse, false); } cdialogex::onlbuttondown(nflags, point); } void ccsit861a3vasilkovskiydlg::onlbuttonup(uint nflags, cpoint point) { // todo: add message handler code here and/or call default releasecapture (); mousecaptured = false; cdialogex::onlbuttonup(nflags, point); } void ccsit861a3vasilkovskiydlg::onmousemove(uint nflags, cpoint point) { // todo: add message handler code here and/or call default if (mousecaptured) { /* code drag , drop , redraw ellipse */ invalidaterect(rectellipse, true); } cdialogex::onmousemove(nflags, point); }
i think mark ransom's comment right, need invalidate 2 rectangles on mousemove.
crect rectellipse(ellipse.cx, ellipse.cy,(ellipse.cx) + 10, (ellipse.cy) + 10); if (mousecaptured) { invalidaterect(rectellipse, true); ellipse.cx = point.x; ellipse.cy = point.y; crect rectnew(ellipse.cx, ellipse.cy,(ellipse.cx) + 10, (ellipse.cy) + 10); invalidaterect(rectnew, true);
}
also, on onlbuttonup()
if (mousecaptured) { ellipsecolor = rgb(255, 0, 0); crect rectellipse(ellipse.cx, ellipse.cy,(ellipse.cx) + 10, (ellipse.cy) + 10); invalidaterect(rectellipse, true); }
Comments
Post a Comment