Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / MFC

Simple Snap-to-Grid cursor to your graphics application

Rate me:
Please Sign up or sign in to vote.
4.98/5 (23 votes)
26 Oct 20024 min read 117.3K   7K   74  
Simple Class to add Snap-to-Grid capability to a Windows drawing program.
//////////////////////////////////////////////////////////////////////////////
//
// SnapCursor.h: declaration of the CSnapCursor class.
// 
// Code James P. Hawkins
// Mail: jim@jphawkins.com
// Web:  www.jphawkins.com
//
//////////////////////////////////////////////////////////////////////////////
//
// This code is free for personal and commercial use, providing this 
// notice remains intact in the source files and all eventual changes are
// clearly marked with comments.
//
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
//////////////////////////////////////////////////////////////////////////////
//
// History
// --------
// #v1.0
//	10/18/02:	Created
//
// #v1.01
//	mm/dd/yy:	None
//
// Note(s)
// -----
//
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_SNAPCURSOR_H__675A2FC2_27F5_4771_8C57_686CE7567F0C__INCLUDED_)
#define AFX_SNAPCURSOR_H__675A2FC2_27F5_4771_8C57_686CE7567F0C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CSnapCursor
{
private:
	int      m_nSnapIncrement;
	CPoint   m_OldSnapPoint;
    CDC     *m_pDC;
	BOOL     m_bSnapOn;
    BOOL     m_bEraseOnMove;
    int      m_nX_offset;
    int      m_nY_offset;
public:
	void SetSnapOffset(int nX_offset, int nY_offset);
    void Reset();
	void Enable(BOOL bOn);
	void SetSnapIncrement(int nIncrement);
	void GetFirstSnapPos(CPoint *point);
	void Draw(CDC *pDC, CPoint *point);
	CSnapCursor();
	virtual ~CSnapCursor();
};

#endif // !defined(AFX_SNAPCURSOR_H__675A2FC2_27F5_4771_8C57_686CE7567F0C__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a hybrid CS and EE professional and 3D Animator/Modeler.

I have been a software developer since 1975, having coded in numerous languages, including C, C++, Java, Perl, Basic, PDP-11 and other assembly languages.

I have worked in the UNIX/C/Shellscript world as well as Windows/C++/Visual C++ platform areas.

I use 3DS MAX 7 for Animation and 3D modeling, combined with other graphics applications for overall production

I have worked on a wide variety of projects: language interpreters/compilers, graphics, automatic gpib test control programs, real-time automation in embedded systems, scientific and engineering applications as well as financial and business. I prefer the scientific, control system and engineering related software.

Major companies include Bell Labs, Singer Corporate Research and Tycom Submarine systems both as full-time and a contractor.

Other interests include Vacuum Tube collecting, Ham Radio (WA2WHV), Photography, 3D art, antiques, Abyssinian cats, Piano, travel, nature.

Comments and Discussions