MFC Dragging Objects - c++

I painted three objects onto a window using CPaintDC, and now I am trying to get one of them (the ellipse) to become draggable by using OnMouseMove event handler. I am just playing around with MFC and am lost as to how to get this to work.
This is what I have tried in 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);
Please find my code pasted below.
Thanks so much for the 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 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);
}

I think Mark Ransom's comment is right, you need to invalidate two 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);
}

Related

Spin Control GetPos() value is delayed

I have an edit control buddied with a spin control with the initial position set to 0. When the up arrow is clicked the edit box goes from 0 to 1, which is fine. But when I use GetPos() the MyValue is 0. When the spin control is incremented again from 1 to 2, the MyValue becomes 1. When down arrow is pressed the edit box goes from 2 to 1, but the value becomes 2. It seems that the MyValue is always one action behind the spin control.
BOOL CAlphaDlg::OnInitDialog()
{
// default code left out to keep it short ...
// TODO: Add extra initialization here
// set range and initial position
mSpinControl.SetRange(0, 3600); // range
mSpinControl.SetPos(0); // inital position
MyValue = mSpinControl.GetPos();
// display initial value in buddy editcontrol
mEditControlDisplay.Format("%d", MyValue);
UpdateData(false);
return TRUE;
}
void CAlphaDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
UpdateData(true);
int MyValue= mSpinControl.GetPos();
std::cout << MyValue << std::endl;
*pResult = 0;
}
I have tried getting the value from the editcontrol but the value is exhibiting the same behavior. How do I get the value of the GetPos() to match what is showing in the edit control?
Thank you in advance.
edit: Here is the full code
// AlphaDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Alpha.h"
#include "AlphaDlg.h"
#include "afxdialogex.h"
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CAlphaDlg dialog
CAlphaDlg::CAlphaDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_ALPHA_DIALOG, pParent)
, mEditControlDisplay(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAlphaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, mEditControlDisplay);
DDX_Control(pDX, IDC_SPIN1, mSpinControl);
}
BEGIN_MESSAGE_MAP(CAlphaDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN1, &CAlphaDlg::OnDeltaposSpin1)
END_MESSAGE_MAP()
// CAlphaDlg message handlers
BOOL CAlphaDlg::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
mSpinControl.SetRange(0, 10);
mSpinControl.SetPos(0);
int MyValue = mSpinControl.GetPos();
mEditControlDisplay.Format("%d", MyValue);
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAlphaDlg::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 CAlphaDlg::OnPaint()
{
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 CAlphaDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CAlphaDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
UpdateData(true);
int MyValue = mSpinControl.GetPos();
std::cout << mEditControlDisplay << std::endl;
std::cout << MyValue << std::endl;
}
Firstly, consider mapping your edit box to a int and then you do not need to cast to a string. And the default value is 0 to begin with so the spinner will be OK. You can also switch off the auto buddy.
In the deltapos handler you could do this:
void CMFCApplication1Dlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
SetDlgItemInt(IDC_EDIT1, pNMUpDown->iPos);
UpdateData(TRUE);
CString a;
a.Format(_T("%d"), iNumberValue);
AfxMessageBox(a);
}
For me the results in the popup message and the edit control are the same.
Update
You can workout what the new value would be by looking at the structure:
void CMFCApplication1Dlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
int iCurrentPos = pNMUpDown->iPos;
if (pNMUpDown->iDelta > 0)
iCurrentPos++;
else
iCurrentPos--;
CString strNewValue;
strNewValue.Format(_T("%d"), iCurrentPos);
AfxMessageBox(strNewValue);
*pResult = 0;
}
For Consideration
You might want to give some thought to CSpinButtonCtrl::SetAccel which sets the acceleration for a spin button control.
In your case I don't think it is going to matter because your range is only 10
units. But if you had a larger range then it is possible that it would increment by more than one unit. It is just something to keep in mind.
According to this reference for UDN_DELTAPOS it says:
The iDelta member of the structure is a signed integer that contains the proposed change in position.
So you could improve the code and increment/decrement based on the iDelta value instead of 1. This would factor in for acceleration. So:
int iCurrentPos = pNMUpDown->iPos + pNMUpDown->iDelta;

Adding vertical gripper to dialog

I am not sure what code to show you here. I have a CDialogEx derived resource in my MFC application:
It supports the dynamic resize layout controls so the user can resize the window. But I would like to add a vertical gripper (indicated in red) so the user can make the width of the names column larger.
I have done some research on this and all the articles are nearly 10 years old and don't factor in the newer dynamic resizing controls.
Having researched this more I see that the "resize gripper" term is not what I mean. That is the icon in the bottom right. I don't mean this.
I am sure you know what I mean though. Is it possible?
Adding a custom gripper control can be relatively easy. See CMySplitter class below.
But if all the controls are in one dialog, it will be very difficult reposition/resize individual controls one by one.
Ideally, use two child dialogs. Set the resize/reposition properties for individual controls in resource editor. Put the grip control in between the two dialogs and resize in response.
Class for gripper control:
#include <functional>
class CMySplitter : public CStatic
{
public:
class CPopup : public CWnd
{
public:
CMySplitter *parent;
int offset;
void OnMouseMove(UINT flag, CPoint pt);
void OnLButtonUp(UINT flag, CPoint pt);
DECLARE_MESSAGE_MAP()
};
std::function<void(int)> callback;
CRect boundary;
CPopup popup;
void OnLButtonDown(UINT flag, CPoint point);
void PreSubclassWindow();
void SetRange(int left, int right);
DECLARE_MESSAGE_MAP()
};
//create splitter control from a static control in dialog
void CMySplitter::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
//modify static control's style (must have SS_NOTIFY)
SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_VISIBLE | SS_GRAYRECT | WS_CHILD | SS_NOTIFY);
//create a popup window with transparency
static CString classname =
AfxRegisterWndClass(0, 0, (HBRUSH)GetStockObject(BLACK_BRUSH));
popup.CreateEx(WS_EX_LAYERED | WS_EX_PALETTEWINDOW | WS_EX_NOACTIVATE,
classname, NULL, WS_POPUP, CRect(0, 0, 0, 0), this, 0);
popup.SetLayeredWindowAttributes(0, 128, LWA_ALPHA);
popup.parent = this;
}
//when user click the static control, show a popup window
void CMySplitter::OnLButtonDown(UINT flag, CPoint pt)
{
CStatic::OnLButtonDown(flag, pt);
GetCursorPos(&pt);
CRect rc;
GetWindowRect(&rc);
popup.offset = pt.x - rc.left;
popup.SetWindowPos(NULL, rc.left, rc.top, rc.Width(), rc.Height(), SWP_SHOWWINDOW);
popup.SetCapture();
}
//how far to the left and right the splitter can go
void CMySplitter::SetRange(int left_, int right_)
{
CRect rc;
GetParent()->GetWindowRect(&rc);
boundary.left = rc.left + left_;
boundary.right = rc.right - right_;
}
//move this popup window
void CMySplitter::CPopup::OnMouseMove(UINT flag, CPoint pt)
{
CWnd::OnMouseMove(flag, pt);
GetCursorPos(&pt);
CRect rc;
GetWindowRect(&rc);
int x = pt.x - offset;
if (x > parent->boundary.left && x < parent->boundary.right)
SetWindowPos(NULL, x, rc.top, 0, 0, SWP_NOSIZE);
}
//hide popup window, let the parent dialog know
void CMySplitter::CPopup::OnLButtonUp(UINT flag, CPoint pt)
{
CWnd::OnLButtonUp(flag, pt);
ReleaseCapture();
ShowWindow(SW_HIDE);
CRect rc;
GetWindowRect(&rc);
parent->callback(rc.left);
}
BEGIN_MESSAGE_MAP(CMySplitter::CPopup, CWnd)
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CMySplitter, CWnd)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
Usage:
Add a static control with IDC_STATIC1 to the dialog and use as follows.
The code below has a main dialog CMyDialog with IDD_DIALOG, normal dialog
It has two child dialogs, child1 and child2 with IDD_PAGE1 and IDD_PAGE2
IDD_PAGE1 and IDD_PAGE2 are dialog resources with "child" style (not popup)
class CMyDialog : public CDialogEx
{
public:
class CChild1 : public CDialogEx
{
};
class CChild2 : public CDialogEx
{
};
CChild1 child1;
CChild1 child2;
//respond to gripper resize
void respond(int position)
{
CRect rs;
m_splitter.GetWindowRect(&rs);
rs.MoveToX(position);
ScreenToClient(&rs);
CRect rc;
GetClientRect(&rc);
CRect r1(0, 0, rs.left, rc.bottom);
CRect r2(rs.right, 0, rc.right, rc.bottom);
child1.MoveWindow(r1, TRUE);
child2.MoveWindow(r2, TRUE);
m_splitter.MoveWindow(rs, TRUE);
m_splitter.Invalidate(TRUE);
}
CMySplitter m_splitter;
BOOL OnInitDialog()
{
CDialogEx::OnInitDialog();
child1.Create(IDD_PAGE1, this);
child2.Create(IDD_PAGE2, this);
m_splitter.SubclassDlgItem(IDC_STATIC1, this);
m_splitter.SetRange(50, 50);
m_splitter.callback = std::bind(&CMyDialog::respond, this, std::placeholders::_1);
//width for splitter
int dx = 10;
CRect rc;
GetClientRect(&rc);
CRect r1(0, 0, 200, rc.bottom);
CRect r2(r1.right + dx, 0, rc.right, rc.bottom);
CRect rs(r1.right, 10, r2.left, rc.bottom - 10);
child1.MoveWindow(r1);
child2.MoveWindow(r2);
m_splitter.MoveWindow(rs);
child1.ShowWindow(SW_SHOW);
child2.ShowWindow(SW_SHOW);
return TRUE;
}
...
};

How to avoid flickering while the CScrollView is moving?

I was trying to make the CScrollView move
Although it moved successfully, but I found a problem...
The CScrollView flickered randomly while it is moving.
Below is the whole code of my project:
#include <afxwin.h>
#include <afxext.h>
#include "resource.h"
class MyView : public CScrollView
{
public:
void OnDraw(CDC *aDC){
CRect rc;
GetClientRect(&rc);
aDC->FillSolidRect(&rc, RGB(0,0,255));
}
BOOL PreCreateWindow(CREATESTRUCT& cs)
{
cs.style &= ~WS_BORDER;
return CScrollView::PreCreateWindow(cs);
}
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
CSize DCSize(200, 800);
SetScrollSizes(MM_TEXT, DCSize);
return 0;
}
DECLARE_DYNCREATE(MyView)
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(MyView, CScrollView)
BEGIN_MESSAGE_MAP(MyView, CScrollView)
ON_WM_CREATE()
END_MESSAGE_MAP()
class CMainDlg : public CDialog
{
public:
CMainDlg(CWnd* pParent = NULL);
enum { IDD = IDD_MAIN_DIALOG };
CWnd* pFrameWnd;
CCreateContext context;
MyView* pView;
int time;
virtual BOOL OnInitDialog();
afx_msg void OnTimer(UINT_PTR timer);
void OnPaint(){
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
dc.FillSolidRect(&rc,RGB(255,187,187));
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
ON_WM_TIMER()
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMainDlg::IDD, pParent)
{
}
BOOL CMainDlg::OnInitDialog()
{
CDialog::OnInitDialog();
pFrameWnd = this;
context.m_pCurrentDoc = NULL;
context.m_pNewViewClass = RUNTIME_CLASS(MyView);
pView = (MyView*)((CFrameWnd*)pFrameWnd)->CreateView(&context);
pView->ShowWindow(SW_SHOW);
time = 0;
SetTimer(1, 1, 0);
return TRUE;
}
void CMainDlg::OnTimer(UINT_PTR timer)
{
if(timer == 1){
if(time > 300){
KillTimer(1);
return;
}
pView->MoveWindow(CRect(time,10,time+300,200),FALSE);
}
time+=1;
Invalidate(FALSE);
}
class MyApp : public CWinApp
{
public:
BOOL InitInstance()
{
CWinApp::InitInstance();
CMainDlg Frame;
Frame.DoModal();
return true;
}
} a_app;
I don't know why the CScrollView flickered while it is moving. Can anyone solve this problem?
It is flickering because you're invalidating the entire window. This causes a WM_ERASE, which blanks the window, then a WM_PAINT, which redraws the entire thing. You're passing FALSE as the bRepaint (last) parameter to MoveWindow, so that it won't repaint any necessary window area after the window has been moved.
Normally when a window moves the content moves with it, and the only parts that need to be redrawn are bits that were offscreen or under another window. Passing TRUE as the last parameter will cause only these areas of the window to be redrawn, which will avoid the flicker.

Having trouble by MFC lntelliSense on C++

Hello I have this Microsoft Foundation Classes that will run mostly and check USB Device and other stuff. The problem is, certain IntelliSense is undefined that's why most errors will display when running the program.
Here's the error list:
The Entire Code:
// MFCApplication2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include "afxwin.h"
#include "CyAPI.h"
#define UART_H
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool IsConnect = false;
// 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()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMFCApplication2Dlg dialog
CMFCApplication2Dlg::CMFCApplication2Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCApplication2Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCApplication2Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication2Dlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON3, &CMFCApplication2Dlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication2Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// CMFCApplication2Dlg message handlers
BOOL CMFCApplication2Dlg::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 CMFCApplication2Dlg::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 CMFCApplication2Dlg::OnPaint()
{
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 CMFCApplication2Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
bool CMFCApplication2Dlg::OnBnClickedButton1()
{
USBDevice->Open(0);
if (USBDevice->IsOpen() != TRUE)
{
AfxMessageBox(_T("Failed to Open Device"));
return false;
}
else
{
IsConnect = true;
return true;
}
}
void CMFCApplication2Dlg::OnBnClickedButton3()
{
USBDevice->Close();
IsConnect = false;
}
void CMFCApplication2Dlg::OnBnClickedButton2()
{
char tmpUart[60];
long OutPacketSize;
OutPacketSize = sizeof(sUart);
LPTSTR pBuffer;
CString sBuffer;
int i;
if (IsConnect == false)
{
AfxMessageBox(_T("USB Connect Fail"));
return;
}
CEdit*OutValue = (CEdit*)GetDlgItem(IDC_OUT_VALUE);
pBuffer = sBuffer.GetBuffer(60);
OutValue->GetWindowText(pBuffer, 60);
strcpy(tmpUart, pBuffer);
OutPacketSize = strlen(tmpUart);
for (i = 0; i<OutPacketSize; i++) sUart[i] = tmpUart[i];
sUart[OutPacketSize + 1] = 0;
OutPacketSize = OutPacketSize + 1;
//Perform the BULK OUT
if (USBDevice->BulkOutEndPt)
{
USBDevice->BulkOutEndPt->XferData(sUart, OutPacketSize);
}
}
HELP:
What certain libraries do I have to import or include to fix these kind of errors? I'm new to MFC's and I don't quite yet understand the structures and ways to program this. Please help.
The ON_BN_CLICKED message map macro expects a member function pointer with the signature
afx_msg void memberFxn()
However, your OnBnClickedButton1 returns a bool. That member function must not have a return value (void) to use it with ON_BN_CLICKED.

Member Variables - Undefined and Inaccessible

I am not able to access the private member variables from member function CAboutDlg::OnLButtonDown.
When I attempted to assign a new value to ellipseColor i get the undeclared identifier error. When I try to access it through its class CCSIT861a3VasilkovskiyDlg::ellipseColorthe compiler says that it is inaccessible.
What have I missed, and how can I access/modify these variable?
Header File:
#pragma once
// CCSIT861a3VasilkovskiyDlg dialog
class CCSIT861a3VasilkovskiyDlg : public CDialogEx
{
// Construction
public:
CCSIT861a3VasilkovskiyDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_CSIT861A3VASILKOVSKIY_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
COLORREF ellipseColor;
COLORREF centerRectColor;
COLORREF rightRectColor;
CRect centerRect;
CRect rightRect;
CSize ellipse;
bool mouseCaptured;
};
Implementation File:
#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()
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);
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);
}
Your OnLButtonDown() is a member function of CAboutDlg, but you are trying to access a private, non-static data member of CCSIT861a3VasilkovskiyDlg, which is a different class.
You probably meant to have functions OnLButtonDown(), OnLButtonUp(), and OnMouseMove() as member functions of CCSIT861a3VasilkovskiyDlg, and not as member functions of CAboutDlg.