📄 ch1_1view.cpp
字号:
// ch1_1View.cpp : implementation of the CCh1_1View class
//
#include "stdafx.h"
#include "ch1_1.h"
#include "ch1_1Doc.h"
#include "ch1_1View.h"
#include "mainfrm.h"
#include "DlgChaos.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCh1_1View
IMPLEMENT_DYNCREATE(CCh1_1View, CScrollView)
BEGIN_MESSAGE_MAP(CCh1_1View, CScrollView)
//{{AFX_MSG_MAP(CCh1_1View)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_COMMAND(ID_Chaos_Pro, OnChaosPro)
ON_COMMAND(ID_Chaos_Encode, OnChaosEncode)
ON_COMMAND(ID_Chaos_Decode, OnChaosDecode)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCh1_1View construction/destruction
CCh1_1View::CCh1_1View()
{
// TODO: add construction code here
}
CCh1_1View::~CCh1_1View()
{
}
BOOL CCh1_1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCh1_1View drawing
void CCh1_1View::OnDraw(CDC* pDC)
{
// 显示等待光标
BeginWaitCursor();
// 获取文档
CCh1_1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// 获取DIB
HDIB hDIB = pDoc->GetHDIB();
// 判断DIB是否为空
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
// 获取DIB宽度
int cxDIB = (int) ::DIBWidth(lpDIB);
// 获取DIB高度
int cyDIB = (int) ::DIBHeight(lpDIB);
::GlobalUnlock((HGLOBAL) hDIB);
CRect rcDIB;
rcDIB.top = rcDIB.left = 0;
rcDIB.right = cxDIB;
rcDIB.bottom = cyDIB;
CRect rcDest;
// 判断是否是打印
if (pDC->IsPrinting())
{
// 是打印,计算输出图像的位置和大小,以便符合页面
// 获取打印页面的水平宽度(象素)
int cxPage = pDC->GetDeviceCaps(HORZRES);
// 获取打印页面的垂直高度(象素)
int cyPage = pDC->GetDeviceCaps(VERTRES);
// 获取打印机每英寸象素数
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
// 计算打印图像大小(缩放,根据页面宽度调整图像大小)
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
/ ((double)cxDIB * cxInch));
rcDest.right = cxPage;
// 计算打印图像位置(垂直居中)
int temp = cyPage - (rcDest.bottom - rcDest.top);
rcDest.bottom += temp/2;
rcDest.top += temp/2;
}
else
// 非打印
{
// 不必缩放图像
rcDest = rcDIB;
}
// 输出DIB
::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
&rcDIB, pDoc->GetDocPalette());
}
// 恢复正常光标
EndWaitCursor();
}
/////////////////////////////////////////////////////////////////////////////
// CCh1_1View printing
BOOL CCh1_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// 设置总页数为一。
pInfo->SetMaxPage(1);
return DoPreparePrinting(pInfo);
}
void CCh1_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCh1_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCh1_1View diagnostics
#ifdef _DEBUG
void CCh1_1View::AssertValid() const
{
CScrollView::AssertValid();
}
void CCh1_1View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CCh1_1Doc* CCh1_1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCh1_1Doc)));
return (CCh1_1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCh1_1View message handlers
BOOL CCh1_1View::OnEraseBkgnd(CDC* pDC)
{
// 主要是为了设置子窗体默认的背景色
// 背景色由文档成员变量m_refColorBKG指定
// 获取文档
CCh1_1Doc* pDoc = GetDocument();
// 创建一个Brush
CBrush brush(pDoc->m_refColorBKG);
// 保存以前的Brush
CBrush* pOldBrush = pDC->SelectObject(&brush);
// 获取重绘区域
CRect rectClip;
pDC->GetClipBox(&rectClip);
// 重绘
pDC->PatBlt(rectClip.left, rectClip.top, rectClip.Width(), rectClip.Height(), PATCOPY);
// 恢复以前的Brush
pDC->SelectObject(pOldBrush);
// 返回
return TRUE;
}
LRESULT CCh1_1View::OnDoRealize(WPARAM wParam, LPARAM)
{
ASSERT(wParam != NULL);
// 获取文档
CCh1_1Doc* pDoc = GetDocument();
// 判断DIB是否为空
if (pDoc->GetHDIB() == NULL)
{
// 直接返回
return 0L;
}
// 获取Palette
CPalette* pPal = pDoc->GetDocPalette();
if (pPal != NULL)
{
// 获取MainFrame
CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
ASSERT_KINDOF(CMainFrame, pAppFrame);
CClientDC appDC(pAppFrame);
// All views but one should be a background palette.
// wParam contains a handle to the active view, so the SelectPalette
// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
if (oldPalette != NULL)
{
UINT nColorsChanged = appDC.RealizePalette();
if (nColorsChanged > 0)
pDoc->UpdateAllViews(NULL);
appDC.SelectPalette(oldPalette, TRUE);
}
else
{
TRACE0("\tCCh1_1View::OnPaletteChanged中调用SelectPalette()失败!\n");
}
}
return 0L;
}
void CCh1_1View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
}
void CCh1_1View::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
CScrollView::OnInitialUpdate();
ASSERT(GetDocument() != NULL);
SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}
void CCh1_1View::OnActivateView(BOOL bActivate, CView* pActivateView,
CView* pDeactiveView)
{
CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
if (bActivate)
{
ASSERT(pActivateView == this);
OnDoRealize((WPARAM)m_hWnd, 0); // same as SendMessage(WM_DOREALIZE);
}
}
void CCh1_1View::OnEditCopy()
{
// 复制当前图像
// 获取文档
CCh1_1Doc* pDoc = GetDocument();
// 打开剪贴板
if (OpenClipboard())
{
// 更改光标形状
BeginWaitCursor();
// 清空剪贴板
EmptyClipboard();
// 复制当前图像到剪贴板
SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
// 关闭剪贴板
CloseClipboard();
// 恢复光标
EndWaitCursor();
}
}
void CCh1_1View::OnEditPaste()
{
// 粘贴图像
// 创建新DIB
HDIB hNewDIB = NULL;
// 打开剪贴板
if (OpenClipboard())
{
// 更改光标形状
BeginWaitCursor();
// 读取剪贴板中的图像
hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));
// 关闭剪贴板
CloseClipboard();
// 判断是否读取成功
if (hNewDIB != NULL)
{
// 获取文档
CCh1_1Doc* pDoc = GetDocument();
// 替换DIB,同时释放旧DIB对象
pDoc->ReplaceHDIB(hNewDIB);
// 更新DIB大小和调色板
pDoc->InitDIBData();
// 设置脏标记
pDoc->SetModifiedFlag(TRUE);
// 重新设置滚动视图大小
SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
// 实现新的调色板
OnDoRealize((WPARAM)m_hWnd,0);
// 更新视图
pDoc->UpdateAllViews(NULL);
}
// 恢复光标
EndWaitCursor();
}
}
void CCh1_1View::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
// 如果当前DIB对象不空,复制菜单项有效
pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}
void CCh1_1View::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
// 如果当前剪贴板中有DIB对象,粘贴菜单项有效
pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
}
void CCh1_1View::Lorenz_3D(double *xi,double *yi,double *zi)
{
double xo,yo,h,w;
h=0.001;
w=8/3;
xo=(*xi)+h*10.0*((*yi)-(*xi));
yo=(*yi)+h*(-(*xi)*(*zi)+28.0*(*xi)-(*yi));
*zi = *zi+h*((*xi)*(*yi)-w*(*zi));
*xi = xo;
*yi = yo;
}
void CCh1_1View::Adtractor_3D(int sizex,int sizey,int itel,int k)
{
int i,j,vt[2];
int x0 = 300,y0=80;
double x=0.6,y=0.4,z=0.6;
CClientDC dc(this);
for(i=0;i<100;i++)
for(j=0;j<itel;j++)
{
Lorenz_3D(&x,&y,&z);
if(k==2)
vt[0]=(int)(y*(double)sizex);
else
vt[0]=(int)(x*(double)sizex);
if(k==0)
vt[1]=(int)(y*(double)sizey);
else
vt[1]=(int)(z*(double)sizey);
dc.SetPixel(vt[0]+x0,vt[1]+y0,RGB(255,0,0));
}
}
void CCh1_1View::OnChaosPro()
{
// TODO: Add your command handler code here
Adtractor_3D(10,10,1000,1);
}
void CCh1_1View::OnChaosEncode()
{
// TODO: Add your command handler code here
//混沌加密
//获取文档
CCh1_1Doc* pDoc = GetDocument();
//指向DIB的指针
LPSTR lpDIB;
//指向DIB象素的指针
LPSTR lpDIBBits;
//锁定DIB
lpDIB =(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
//找到DIB图象象素的起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
//判断是否是8-bpp位图
if(::DIBNumColors(lpDIB)!=256)
{
//提示用户
MessageBox("目前只支持256色位图的反色!","系统提示",MB_ICONINFORMATION|MB_OK);
//解除锁定
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
//返回
return;
}
//参数
double a,XStart;
CDlgChaos dlgPara;
dlgPara.m_ChaoA=4;
dlgPara.m_ChaosXStart=0.6;
if(dlgPara.DoModal()!=IDOK)
{
return;
}
a=dlgPara.m_ChaoA;
XStart=dlgPara.m_ChaosXStart;
//更改光标形状
BeginWaitCursor();
if(::ChaosEncode(lpDIBBits,::DIBWidth(lpDIB),::DIBHeight(lpDIB),XStart,a))
{
//设置脏标记
pDoc->SetModifiedFlag(TRUE);
//更新视图
pDoc->UpdateAllViews(NULL);
}
//解除锁定
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
//恢复光标
EndWaitCursor();
}
void CCh1_1View::OnChaosDecode()
{
// TODO: Add your command handler code here
//混沌解密
//获取文档
CCh1_1Doc* pDoc = GetDocument();
//指向DIB的指针
LPSTR lpDIB;
//指向DIB象素的指针
LPSTR lpDIBBits;
//锁定DIB
lpDIB =(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
//找到DIB图象象素的起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
//判断是否是8-bpp位图
if(::DIBNumColors(lpDIB)!=256)
{
//提示用户
MessageBox("目前只支持256色位图的反色!","系统提示",MB_ICONINFORMATION|MB_OK);
//解除锁定
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
//返回
return;
}
//参数
double a,XStart;
CDlgChaos dlgPara;
dlgPara.m_ChaoA=4;
dlgPara.m_ChaosXStart=0.6;
if(dlgPara.DoModal()!=IDOK)
{
return;
}
a=dlgPara.m_ChaoA;
XStart=dlgPara.m_ChaosXStart;
//更改光标形状
BeginWaitCursor();
if(::ChaosEncode(lpDIBBits,::DIBWidth(lpDIB),::DIBHeight(lpDIB),XStart,a))
{
//设置脏标记
pDoc->SetModifiedFlag(TRUE);
//更新视图
pDoc->UpdateAllViews(NULL);
}
//解除锁定
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
//恢复光标
EndWaitCursor();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -