rj030619view.cpp
来自「VC++一些代码,自己制作,可以实现密码登录,有画圆等功能.」· C++ 代码 · 共 368 行
CPP
368 行
// RJ030619View.cpp : implementation of the CRJ030619View class
//
#include "stdafx.h"
#include "RJ030619.h"
#include"math.h"
#include "RJ030619Doc.h"
#include "RJ030619View.h"
#include"MainFrm.h"
#define Pi 3.1415926
#ifdef _DEBUG
#define new DEBUG_NEW
#define WM_MYMESSAGE WM_USER+1
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRJ030619View
IMPLEMENT_DYNCREATE(CRJ030619View, CView)
BEGIN_MESSAGE_MAP(CRJ030619View, CView)
//{{AFX_MSG_MAP(CRJ030619View)
ON_COMMAND(ID_COMMON_COLOR, OnCommonColor)
ON_COMMAND(ID_COMMON_FONT, OnCommonFont)
ON_COMMAND(ID_FORE_COLOR, OnForeColor)
ON_COMMAND(ID_MENU_TEST, OnMenuTest)
ON_WM_CREATE()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
ON_COMMAND(ID_MENUPLAY, OnMenuplay)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRJ030619View construction/destruction
CRJ030619View::CRJ030619View()
{
// TODO: add construction code here
m_ForeColor=240;
m_BackColor=0xFFFFFF;
m_Font.CreateStockObject(SYSTEM_FONT);
nFirst=0;
OutStr="看看这个,会隐形的!";
OutStrLen=OutStr.GetLength();
m_bDragging=false;
m_hCross=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
}
CRJ030619View::~CRJ030619View()
{
}
BOOL CRJ030619View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CRJ030619View drawing
void CRJ030619View::OnDraw(CDC* pDC)
{
CRJ030619Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data hereint x,y;
int x,y;
CFont *pOldFont;
TEXTMETRIC TM;
int textHeight;
//设置正文的字体
pOldFont=pDC->SelectObject(&m_Font);
//设置正文的前景色和背景色
pDC->SetTextColor(m_ForeColor);
pDC->SetBkColor(m_BackColor);
//计算每行正文的高度
pDC->GetTextMetrics(&TM);
textHeight=TM.tmHeight+TM.tmExternalLeading;
//输出正文
x=550;y=340;
pDC->TextOut(x,y," 华 东 交 通 大 学 ");
y+=2*textHeight;
pDC->TextOut(x, y,"软 件 学 院 03级6班 卢 敏");
//恢复原来的字体
pDC->SelectObject(pOldFont);
// pDC->SelectObject (NewFont);
//输出从nFirst开始的文本串
pDC->TextOut(120,140, OutStr.Mid(nFirst));
////////////////*********************************
int nIndex=pDoc->GetNumLines();//取得线段的数量
TRACE("nIndex1=%d\n",nIndex);//调试程序用
while(nIndex--)//循环画出每一段线段
{
TRACE("nIndex2=%d\n",nIndex);
pDoc->GetLine(nIndex)->DrawLine(pDC);
}
///*********************************************
//画硬币
for(int i=0;i<pDoc->m_nCoins;i++)
{
int y=200-5*i;
pDC->Ellipse(600,y,500,y-30);
pDC->Ellipse(600,y-5,500,y-35);
}
CString strCoins;
CStatusBar*pStatus=(CStatusBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
if(pStatus)
{
strCoins.Format("硬币数:%d",pDoc->m_nCoins);
pStatus->SetPaneText(1,strCoins);
}
//输出显示三半图
pDC->SelectStockObject(BLACK_PEN);
CBrush RedBrush,BlueBrush,YellowBrush;
RedBrush.CreateSolidBrush(RGB(255,0,0));
pDC->SelectObject (&RedBrush);
pDC->Pie(10,100,100,190,(int)(55+45*cos(2*Pi/3)),(int)(145+45*sin(2*Pi/3)),
(int)(55+45*cos(0)),(int)(145+45*sin(0)));
BlueBrush.CreateSolidBrush(RGB(0,255,255));
pDC->SelectObject (&BlueBrush);
pDC->Pie(10,100,100,190,(int)(55+45*cos(4*Pi/3)),(int)(145+45*sin(4*Pi/3)),
(int)(55+45*cos(2*Pi/3)),(int)(145+45*sin(2*Pi/3)));
YellowBrush.CreateSolidBrush(RGB(255,255,0));
pDC->SelectObject (&YellowBrush);
pDC->Pie(10,100,100,190,(int)(55+45*cos(0)),(int)(145+45*sin(0)),
(int)(55+45*cos(4*Pi/3)),(int)(145+45*sin(4*Pi/3)));
}
/////////////////////////////////////////////////////////////////////////////
// CRJ030619View printing
BOOL CRJ030619View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CRJ030619View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CRJ030619View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CRJ030619View diagnostics
#ifdef _DEBUG
void CRJ030619View::AssertValid() const
{
CView::AssertValid();
}
void CRJ030619View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CRJ030619Doc* CRJ030619View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRJ030619Doc)));
return (CRJ030619Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRJ030619View message handlers
void CRJ030619View::OnCommonColor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
if(dlg.DoModal()==IDOK)
{
m_BackColor=dlg.GetColor();
//重绘视图
Invalidate();
}
}
void CRJ030619View::OnCommonFont()
{
// TODO: Add your command handler code here
CFontDialog dlg;
if(dlg.DoModal()==IDOK)
{
LOGFONT LF;
//获取所选字体的信息
dlg.GetCurrentFont(&LF);
m_ForeColor=dlg.GetColor();
//建立新的字体
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&LF);
Invalidate();
}
}
void CRJ030619View::OnForeColor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
if(dlg.DoModal()==IDOK)
{
m_ForeColor=dlg.GetColor();
//重绘视图
Invalidate();
}
}
void CRJ030619View::OnMenuTest()
{
// TODO: Add your command handler code here
CComctlDlg dlg;
if(dlg.DoModal()!=IDOK)
return;
Invalidate();
UpdateWindow();
CClientDC dc(this);
CPen penNew;
penNew.CreatePen(PS_SOLID,dlg.m_nLineWt,RGB(0,25,255));
CPen* ppenOld=dc.SelectObject(&penNew);
dc.Ellipse(50,50,2*dlg.m_nRadius,2*dlg.m_nRadius);
dc.SelectObject(ppenOld);
}
int CRJ030619View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(1,200,NULL);
// TODO: Add your specialized creation code here
return 0;
}
void CRJ030619View::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu menuPopup;
if(menuPopup.CreatePopupMenu())
{ menuPopup.AppendMenu(MF_STRING,ID_COIN_ADD,"增加硬币\tCtrl+A");
menuPopup.AppendMenu(MF_STRING,ID_COIN_SUB,"减少硬币\tCtrl+B");
menuPopup.TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y, this);
}
}
void CRJ030619View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
::SetCursor(m_hCross);
m_ptOrigin=point;
m_bDragging=TRUE;
CView::OnLButtonDown(nFlags, point);
}
void CRJ030619View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDragging)
{
m_bDragging=false;
ReleaseCapture();
}
CView::OnLButtonUp(nFlags, point);
}
void CRJ030619View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
CStatusBar *pStatusBar=&pMainFrame->m_wndStatusBar;
if(pStatusBar)
{
CString str;
str.Format("光标位置 X:%d,y:%d",point.x,point.y);
pStatusBar->SetPaneText(0,str);
}
if(m_bDragging)
{
CRJ030619Doc *pDoc=GetDocument();//获得文档对象的指针
ASSERT_VALID(pDoc);//测试文档对象是否运行有效
pDoc->AddLine(m_ptOrigin,point);//加入线段到指针数组
//————
CClientDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_ptOrigin=point;
}
CView::OnMouseMove(nFlags, point);
}
void CRJ030619View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
SendMessage(WM_MYMESSAGE);
CView::OnTimer(nIDEvent);
}
LRESULT CRJ030619View::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
CTime time; //定义时间类对象
time=CTime::GetCurrentTime(); //调用系统当前时间
CString strTime; //定义字符串型对象
strTime=time.Format("时间:%H:%M:%S"); //定义系统时间输出的格式
CStatusBar*pStatus=(CStatusBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
if(pStatus)
{
pStatus->SetPaneText(3,strTime);
}
return 0;
}
void CRJ030619View::OnMenuplay()
{
// TODO: Add your command handler code here
nFirst=nFirst+18; //设置文本串开始位置,向前走一个汉字
if(nFirst>=OutStrLen+1)//文本串开始位置归0
{
nFirst=0;
}
Invalidate ();
}
void CRJ030619View::OnDestroy()
{
CView::OnDestroy();
KillTimer(1);
// TODO: Add your message handler code here
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?