学习太累,来点艺术吧。这里我们要制作一个简单的工具,功能是通过移动鼠标来画出艺术图。高老头说写程序的最高境界是与艺术相通的,这点我等凡人体会不到,但我们现在却可以通过编程制作出创造艺术品的工具了。 不知为何,这几天优快云博客的图片居然一直在审核,我结合《庄子》一书创造的几幅“艺术图”现在还不方便传上来。
下面这份代码的实现基础是鼠标事件和设备上下文的写屏操作,在codesource的代码基础上做了少许修改,技术上很简单,详述可见参考资料。
源代码
/* ***********************************************************
* File: mfc3.cpp
* Author: think.hy@gmail.com
* Refer: http://www.codersource.net/mfc_tutorial_Part3.html
*********************************************************** */
// MFC3//MFC_Tutorial_3.CPP - MFC Tutorial Part 3 from CoderSource.net
//
#include <afxwin.h>
class MFC_Tutorial_Window :public CFrameWnd
{
CPoint m_StartPoint, m_EndPoint;
BOOL m_isLButtonDown;
public :
MFC_Tutorial_Window()
{
m_isLButtonDown = FALSE ;
Create(NULL ,"MFC Tutorial Part 3 CoderSource Window" );
}
void OnLButtonDown(UINT nFlags, CPoint point);
void OnLButtonUp(UINT nFlags, CPoint point);
void OnMouseMove(UINT nFlags, CPoint point);
void OnRButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handler
ON_WM_LBUTTONUP() //Macro to map the left button click to the handler
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
void MFC_Tutorial_Window::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO : Add your message handler code here and/or call default
CFrameWnd::OnLButtonDown(nFlags, point);
m_StartPoint = point;
m_isLButtonDown = TRUE ;
}
void MFC_Tutorial_Window::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO : Add your message handler code here and/or call default
CFrameWnd::OnLButtonDown(nFlags, point);
m_isLButtonDown = FALSE ;
}
void MFC_Tutorial_Window::OnMouseMove(UINT nFlags, CPoint point)
{
CFrameWnd::OnMouseMove(nFlags, point);
if (m_isLButtonDown == TRUE ) // Left button is clicking.
{
m_EndPoint = point;
CClientDC dc(this );
dc.MoveTo(m_StartPoint);
dc.LineTo(m_EndPoint);
}
}
void MFC_Tutorial_Window::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO : Add your message handler code here and/or call default
CClientDC dc(this );
}
class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
public :
BOOL InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(TRUE );
return TRUE ;
}
};
MyApp theApp;
//End of MFC_Tutorial_3.CPP.cpp - MFC Tutorial Part 3 from CoderSource.net
Makefile:
# Makefile for MFC Application with wineLib on Linux(Ubuntu 9.10)
# *****************************************************************************************
# * Previous steps:
# * 1. Install wine 1.1.35
# * wget http://ibiblio.org/pub/linux/system/emulators/wine/wine-1.1.35.tar.bz2
# * unzip && ./configure && install
# *
# * Q: configure: error: FreeType development files not found.
# * A:sudo apt-get install libfreetype6-dev
# *
# * 2. Copy all the files of Vistual Studio 6 to wine directory.
# *
# * Notice:
# * When you make with this Makefile at first time,there are some errors about "err:module". Dont worry,make again,you will be success.
# *
# * Creator: think.hy@gmail.com
# * Date: 2010年 01月 03日 星期日 17:20:53 CST
# ****************************************************************************************
WINE = /home/thinkhy/work/wine/wine-1.1.35/wine
LINK = LINK.EXE
LFLAG = /subsystem:windows #/OPT:REF
CC = CL.EXE
CFLAG = /MT /c #/GX /D "_AFXDLL"
INCLUDE = /I. /I/home/thinkhy/.wine/drive_c/Program/ Files/Microsof/ Visual/ Studio/VC98/Include /
/I/home/thinkhy/.wine/drive_c/Program/ Files/Microsof/ Visual/ Studio/VC98/MFC/Include
LIB = /LIBPATH:/home/thinkhy/.wine/drive_c/Program/ Files/Microsof/ Visual/ Studio/VC98/Lib /LIBPATH:/home/thinkhy/.wine/drive_c/Program/ Files/Microsof/ Visual/ Studio/VC98/MFC/Lib
RES =
SRC = ./mfc3.cpp
OBJ = ./mfc3.obj
OUTFILE = mfc3.exe
OUTPUT = /OUT:$(OUTFILE)
all : compile link
compile : $(SRC)
$(WINE) $(CC) $(SRC) $(CFLAG) $(INCLUDE)
link : $(OBJ)
$(WINE) $(LINK) $(LFLAG) $(LIB) $(OBJ) $(RES) $(OUTPUT)
clean:
- rm $(OBJ)
- rm $(OUTFILE)
touch $(SRC)
###########################################################################################