?? dibview.cpp
字號:
// DIBView.cpp : implementation of the CDIBView class
//
#include "stdafx.h"
#include "DIB.h"
#include "DIBDoc.h"
#include "DIBView.h"
#include "GeoTrans.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDIBView
IMPLEMENT_DYNCREATE(CDIBView, CView)
BEGIN_MESSAGE_MAP(CDIBView, CView)
//{{AFX_MSG_MAP(CDIBView)
ON_COMMAND(ID_Menu_Translation, OnMenuTranslation)
ON_COMMAND(ID_Menu_Mirror_V, OnMenuMirrorV)
ON_COMMAND(ID_Menu_Mirror_H, OnMenuMirrorH)
ON_COMMAND(ID_Menu_Scale, OnMenuScale)
ON_COMMAND(ID_Menu_Rotate, OnMenuRotate)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDIBView construction/destruction
CDIBView::CDIBView()
{
// TODO: add construction code here
}
CDIBView::~CDIBView()
{
}
BOOL CDIBView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDIBView drawing
void CDIBView::OnDraw(CDC* pDC)
{
// 顯示等待光標
BeginWaitCursor();
// 獲取文檔
CDIBDoc* 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();
}
/////////////////////////////////////////////////////////////////////////////
// CDIBView printing
BOOL CDIBView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDIBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDIBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDIBView diagnostics
#ifdef _DEBUG
void CDIBView::AssertValid() const
{
CView::AssertValid();
}
void CDIBView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDIBDoc* CDIBView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDIBDoc)));
return (CDIBDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDIBView message handlers
void CDIBView::OnMenuTranslation()
{
// 平移位圖
// 獲取文檔
CDIBDoc * pDoc = GetDocument();
// 指向DIB的指針
LPSTR lpDIB;
// 指向DIB象素指針
LPSTR lpDIBBits;
// 鎖定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
// 判斷是否是8-bpp位圖(這里為了方便,只處理8-bpp位圖的平移,其它的可以類推)
if (::DIBNumColors(lpDIB) != 256)
{
// 提示用戶
MessageBox("目前只支持256色位圖的平移!", "系統提示" , MB_ICONINFORMATION | MB_OK);
// 解除鎖定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
// 返回
return;
}
LONG lXOffset;
LONG lYOffset;
/*
// 創建對話框
CDlgGeoTran dlgPara;
// 初始化變量值
dlgPara.m_XOffset = 100;
dlgPara.m_YOffset = 100;
// 顯示對話框,提示用戶設定平移量
if (dlgPara.DoModal() != IDOK)
{
// 返回
return;
}
*/
// 獲取用戶設定的平移量
lXOffset = 40;
lYOffset = 100;
// 刪除對話框
//delete dlgPara;
// 更改光標形狀
BeginWaitCursor();
// 找到DIB圖像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 調用TranslationDIB()函數平移DIB
if (TranslationDIB1(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB), lXOffset, lYOffset))
{
// 設置臟標記
pDoc->SetModifiedFlag(TRUE);
// 更新視圖
pDoc->UpdateAllViews(NULL);
}
else
{
// 提示用戶
MessageBox("分配內存失敗!", "系統提示" , MB_ICONINFORMATION | MB_OK);
}
// 解除鎖定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
// 恢復光標
EndWaitCursor();
}
void CDIBView::OnMenuMirrorV()
{
// 垂直鏡像
// 獲取文檔
CDIBDoc* pDoc = GetDocument();
// 指向DIB的指針
LPSTR lpDIB;
// 指向DIB象素指針
LPSTR lpDIBBits;
// 鎖定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
// 判斷是否是8-bpp位圖(這里為了方便,只處理8-bpp位圖的垂直鏡像,其它的可以類推)
if (::DIBNumColors(lpDIB) != 256)
{
// 提示用戶
MessageBox("目前只支持256色位圖的垂直鏡像!", "系統提示" , MB_ICONINFORMATION | MB_OK);
// 解除鎖定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
// 返回
return;
}
// 更改光標形狀
BeginWaitCursor();
// 找到DIB圖像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 調用MirrorDIB()函數垂直鏡像DIB
if (MirrorDIB(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB), FALSE))
{
// 設置臟標記
pDoc->SetModifiedFlag(TRUE);
// 更新視圖
pDoc->UpdateAllViews(NULL);
}
else
{
// 提示用戶
MessageBox("分配內存失敗!", "系統提示" , MB_ICONINFORMATION | MB_OK);
}
// 解除鎖定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
// 恢復光標
EndWaitCursor();
}
void CDIBView::OnMenuMirrorH()
{
// TODO: Add your command handler code here
}
void CDIBView::OnMenuScale()
{
// TODO: Add your command handler code here
}
void CDIBView::OnMenuRotate()
{
// TODO: Add your command handler code here
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -