tailieunhanh - Ivor Horton’s Beginning Visual C++ 2005 phần 8

Trước tiên, bạn tạo một đối tượng CClientDC để sử dụng trong MoveElement () chức năng. Sau đó, bạn gọi MoveElement () để di chuyển các phần tử hiện đang được chọn khoảng cách từ vị trí con trỏ hiện tại vị trí con trỏ ban đầu mà chúng tôi đã lưu trong m_FirstPos. Sau khi các phần tử có được vị trí | Chapter 15 void CSketcherView OnRButtonDown UINT nFlags CPoint point if m_MoveMode In moving mode so drop element back in original position CClientDC aDC this OnPrepareDC aDC MoveElement aDC m_FirstPos m_MoveMode FALSE m_pSelected 0 GetDocument - UpdateAllViews 0 return Get origin adjusted Move element to orig position Kill move mode De-select element Redraw all the views We are done You first create a CClientDC object for use in the MoveElement function. You then call the MoveElement function to move the currently selected element the distance from the current cursor position to the original cursor position that we saved in m_FirstPos. After the element has been repositioned you just turn off move mode deselect the element and get all the views redrawn. Exercising the Application Everything is now complete for the context pop-ups to work. If you build Sketcher you can select the element type and color from one context menu or if you are over an element you can then move or delete that element from the other context menu. Dealing with Masked Elements There s still a limitation that you might want to get over. If the element you want to move or delete is enclosed by the rectangle of another element that is drawn after the element you want you won t be able to highlight it because Sketcher always finds the outer element first. The outer element completely masks the element it encloses. This is a result of the sequence of elements in the list. You could fix this by adding a Send to Back item to the context menu that would move an element to the beginning of the list. Add a separator and a menu item to the element drop-down in the IDR_CURSOR_MENU resource as shown in Figure 15-15. Figure 15-15 814 Creating the Document and Improving the View You can add a handler for the item to the view class through the Properties window for the CSketcherView class. It s best to handle it in the view because that s where you record the selected element. Select the Messages toolbar .