#include <afxwin.h>
#include <afxext.h>//MFC扩展类的头文件,也就是类的定义
#include "resource.h"
class sample:public CFrameWnd
{
public:
CToolBar t;//实例化工具条类
CToolBar t2;
CStatusBar s;//实例化状态条类
CPoint NewPoint;//一个终止点
CPoint OldPoint;//一个起始点
RECT r;//需要刷新的区域
sample()
{
Create(NULL,"MFC Window",WS_OVERLAPPEDWINDOW,rectDefault,NULL,MAKEINTRESOURCE(IDR_MENU1),0,NULL);
}
void OnLButtonDown(UINT i,CPoint p)//添加的消息处理函数
{
OldPoint=p;
}
void OnLButtonUp(UINT i,CPoint p)
{
NewPoint=p;
r.left=OldPoint.x;
r.top=OldPoint.y;
r.right=NewPoint.x;
r.bottom=NewPoint.y;
CWnd::InvalidateRect(&r,TRUE);
}
BOOL OnCreateClient(CREATESTRUCT *c,CCreateContext *p)
{
//工具条创建
UINT tool[]={0,ID_DISPLAY_DOWN,ID_DISPLAY_UP,ID_DISPLAY_RIGHT,ID_DISPLAY_LEFT,0,ID_DRAW_LINE,ID_DRAW_RECT,ID_DRAW_ROUND_RECT,ID_DRAW_CIRCLE,ID_DRAW_CURVE};
t.CreateEx(this,TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
//工具条加载图片
t.LoadBitmap(IDB_BITMAP1);
//设置按钮
t.SetButtons(tool,11);
//工具条可以停靠在任何位置
t.EnableDocking(CBRS_ALIGN_ANY);
//框架接受任意停靠位置
EnableDocking(CBRS_ALIGN_ANY);
//执行停靠工具条
DockControlBar(&t,AFX_IDW_DOCKBAR_TOP);
//状态条创建
UINT stat[]={0,0,ID_INDICATOR_NUM,ID_INDICATOR_CAPS};
s.Create(this);
//状态条设置窗格
s.SetIndicators(stat,4);
return TRUE;
}
void up()
{
::MessageBox(0,"up","消息",MB_OK);
}
void down()
{
::MessageBox(0,"down","消息",MB_OK);
}
void left()
{
::MessageBox(0,"left","消息",MB_OK);
}
void right()
{
::MessageBox(0,"right","消息",MB_OK);
}
void line()
{
::MessageBox(0,"line","消息",MB_OK);
}
void rect()
{
::MessageBox(0,"rect","消息",MB_OK);
}
void round_rect()
{
::MessageBox(0,"round_rect","消息",MB_OK);
}
void circle()
{
::MessageBox(0,"circle","消息",MB_OK);
}
void curve()
{
::MessageBox(0,"curve","消息",MB_OK);
}
void OnMouseMove(UINT nFlags, CPoint point)
{
CString str;
CString str_x;
CString str_y;
str_x.Format("%d",point.x);
str_y.Format("%d",point.y);
str+="X: ";
str+=str_x;
str+=" ";
str+="Y: ";
str+=str_y;
s.SetPaneText(1,str,TRUE);
str_x.Empty();
str_y.Empty();
str.Empty();
}
void OnPaint()
{
CPaintDC d(this);
d.MoveTo(OldPoint);
d.LineTo(NewPoint);
}
DECLARE_MESSAGE_MAP()//消息映射的申明
};
BEGIN_MESSAGE_MAP(sample,CFrameWnd)
//这个宏填写两个参数,一个子类,一个父类
ON_WM_PAINT()
ON_COMMAND(ID_DISPLAY_UP, up)
ON_COMMAND(ID_DISPLAY_DOWN,down)
ON_COMMAND(ID_DISPLAY_LEFT,left)
ON_COMMAND(ID_DISPLAY_RIGHT,right)
ON_COMMAND(ID_DRAW_LINE,line)
ON_COMMAND(ID_DRAW_RECT,rect)
ON_COMMAND(ID_DRAW_ROUND_RECT,round_rect)
ON_COMMAND(ID_DRAW_CIRCLE,circle)
ON_COMMAND(ID_DRAW_CURVE,curve)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()//左键按下的事件
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()//结束宏
class App:public CWinApp
{
public:
BOOL InitInstance();
BOOL ExitInstance();
};
BOOL App::InitInstance()
{
//MessageBox(0,"My MFC Window","InitInstance",MB_OK|MB_ICONASTERISK);
sample *obj;
obj=new sample;
m_pMainWnd=obj;
obj->ShowWindow(SW_SHOWMAXIMIZED);
return TRUE;
}
BOOL App::ExitInstance()
{
//MessageBox(0,"My Window","ExitInstance",MB_OK|MB_ICONHAND);
return TRUE;
}
App a;