MFC拖动对象
MFC Dragging Objects
我使用CPaintDC
在窗口上画了三个对象,现在我试图通过使用OnMouseMove
事件处理程序使其中一个(椭圆)成为可拖动的。我只是在玩MFC,我失去了如何让这个工作。
这是我在OnMouseMove
中尝试过的:
// TODO: Add your 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);
请查收我粘贴在下面的代码。
谢谢你的帮助。
#include "stdafx.h"
#include "CSIT861a3 Vasilkovskiy.h"
#include "CSIT861a3 VasilkovskiyDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
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 to system menu.
// IDM_ABOUTBOX must be in the 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 the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCSIT861a3VasilkovskiyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCSIT861a3VasilkovskiyDlg::OnPaint()
{
//Create pen and pointer to the old pen
CPen blackPen;
blackPen.CreatePen(PS_SOLID, 1, RGB (0, 0, 0));
//Create a bursh
CBrush blackBrush(centerRectColor);
CBrush redBrush(rightRectColor);
CBrush hatchRedBrush(HS_CROSS, ellipseColor);
//Set up object for 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 for 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 the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCSIT861a3VasilkovskiyDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//void CAboutDlg::OnLButtonDown(UINT nFlags, CPoint point)
//{
// // TODO: Add your 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 your message handler code here and/or call default
// ReleaseCapture ();
// CDialogEx::OnLButtonUp(nFlags, point);
//}
//void CAboutDlg::OnMouseMove(UINT nFlags, CPoint point)
//{
// // TODO: Add your message handler code here and/or call default
//
// CDialogEx::OnMouseMove(nFlags, point);
//}
void CCSIT861a3VasilkovskiyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your 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 your message handler code here and/or call default
ReleaseCapture ();
mouseCaptured = false;
CDialogEx::OnLButtonUp(nFlags, point);
}
void CCSIT861a3VasilkovskiyDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (mouseCaptured)
{
/*
Code to drag and drop and redraw ellipse
*/
InvalidateRect(rectEllipse, TRUE);
}
CDialogEx::OnMouseMove(nFlags, point);
}
我认为Mark Ransom的评论是对的,你需要在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);
}
同样,在OnLButtonUp()
if (mouseCaptured)
{ ellipseColor = RGB(255, 0, 0);
CRect rectEllipse(ellipse.cx, ellipse.cy,(ellipse.cx) + 10, (ellipse.cy) + 10);
InvalidateRect(rectEllipse, TRUE);
}
相关文章:
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 如何在鼠标挂钩过程中检测拖动
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件
- 如何防止特定的QTreeView分支被拖动?
- 通过拖动边框调整大小时不显示SDL_WINDOWEVENT_SIZE_CHANGED?
- 在 qt 中的绘图中拖动误差线
- 非拖动功能模板参数包的合法使用
- QML在被Mousearea拖动时无法更改项目位置
- 在OpenGL中,如何将鼠标跟随更改为鼠标单击,拖动并移动释放?
- 如何在 Win32 应用中拖动纯色矩形,而不会产生白色闪烁,也不会干扰其他屏幕对象
- 图形项目的QgraphicsScene中的可拖动像素
- Qt - 防止在拖动标题栏时调整窗口大小
- 在内部使用ComboBox拖动ListWidget项目
- 使用C 绘制矩形拖动
- 在 QGraphicsScene 中拖动 QPixmap:如何避免 lambda 参数中不允许'auto'
- 基于鼠标选择Opengl拖动对象
- 鼠标拖动OpenGL/GLUT中的对象
- 使用 C++ 和 OpenGL 拖动三维对象
- C++OpenGL用鼠标拖动多个对象
- MFC拖动对象