Code: Select all
// TestView.cpp : implementation of the CTestView class
//
#include "stdafx.h"
#include "Test.h"
#include "TestDoc.h"
#include "TestView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
ON_COMMAND(IDM_OPEN, OnOpen)
ON_COMMAND(IDM_SCALED, OnScaled)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_CREATE()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction
CTestView::CTestView()
{
// TODO: add construction code here
u_Count = 1;
}
CTestView::~CTestView()
{
}
BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground= //(HBRUSH)GetStockObject(BLACK_BRUSH);//获取一个黑色的背景画刷
CreateSolidBrush(RGB(0,0,0));
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=AfxGetInstanceHandle(); //注意用全局函数获取应用程序hInstance句柄
wndcls.lpfnWndProc=::DefWindowProc; //必须指定为API函数,不同于CWnd中的DefWindowProc(参数个数不同)
wndcls.lpszClassName="BlackBackground";
wndcls.lpszMenuName=NULL; //菜单的创建并不是在设计窗口类时创建的,由MFC在构造pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,…) 被创建。
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
cs.lpszClass="BlackBackground";
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTestView drawing
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBrush brush(RGB(255,0,0));
CClientDC dc(this);
dc.FillRect(Right_Rect,&brush);
dc.FillRect(Under_Rect,&brush);
}
/////////////////////////////////////////////////////////////////////////////
// CTestView printing
BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid();
}
void CTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers
void CTestView::OnOpen()
{
// TODO: Add your command handler code here
LoadFile();
m_pDicomImg->writeBMP("txt.bmp",24);
// BMPHead();
ShowBmp("txt.bmp");
m_bIsOpened = true;
}
void CTestView::OnScaled()
{
// TODO: Add your command handler code here
if (u_Count %4 ==1)
{
m_pDicomImg->rotateImage(90);
}
if (u_Count %4 ==2)
{
m_pDicomImg->rotateImage(180);
}
if (u_Count %4 ==3)
{
m_pDicomImg->rotateImage(270);
}
if (u_Count %4 ==0)
{
m_pDicomImg->rotateImage(360);
}
m_pDicomImg->writeBMP("TXT.bmp",24);
u_Count++;
BMPHead();
ShowBmp("txt.bmp");
}
void CTestView::LoadFile()
{
DcmFileFormat * pDicomFile=new DcmFileFormat();
OFCondition Fcond=pDicomFile->loadFile("txt.dcm");//打开文件,此处可以根据具体路径进行更改
m_pDC = GetDC();
if(Fcond.good())
m_pDC->TextOut(50,50,(CString )"load file success");
else
m_pDC->TextOut(50,50,(CString)"load file error");
m_pDataset=pDicomFile->getDataset();
E_TransferSyntax xfer=m_pDataset->getOriginalXfer();
m_pDicomImg = new DicomImage((DcmObject * )m_pDataset, xfer);
m_nWidth = m_pDicomImg->getWidth();
m_nHeight = m_pDicomImg->getHeight();
m_nBits = m_pDicomImg->getDepth();
m_pDicomImg->setMinMaxWindow();
}
void CTestView::BMPHead()
{
unsigned long iDataSize= m_pDicomImg->createWindowsDIB(m_pDicomDibits,0,0,8,1,1);
//bitmap文件头信息
BITMAPINFOHEADER BitmapInfoHeader;
BitmapInfoHeader.biBitCount=8;
BitmapInfoHeader.biClrImportant=0;
BitmapInfoHeader.biClrUsed=0;
BitmapInfoHeader.biCompression=BI_RGB;
BitmapInfoHeader.biPlanes=1;
BitmapInfoHeader.biHeight=m_pDicomImg->getHeight();//得到图像的高度
BitmapInfoHeader.biWidth=m_pDicomImg->getWidth();//得到图像的宽度
BitmapInfoHeader.biXPelsPerMeter=0;
BitmapInfoHeader.biYPelsPerMeter=0;
BitmapInfoHeader.biSize=sizeof(BITMAPINFOHEADER);
BitmapInfoHeader.biSizeImage=0;
RGBQUAD pColorTable[256];
for(int i=0;i<256;i++)
{
pColorTable[i].rgbBlue=i;
pColorTable[i].rgbGreen=i;
pColorTable[i].rgbRed=i;
pColorTable[i].rgbReserved=0;
}
//定义一个bmpinfo需要内存大小的内存
char p[sizeof(BITMAPINFOHEADER)+256*4];
memcpy(p,&BitmapInfoHeader,sizeof(BITMAPINFOHEADER));
memcpy(p+sizeof(BITMAPINFOHEADER),pColorTable,1024);
PBITMAPINFO BitmapInfo = (PBITMAPINFO)p;
//颜色表
int colorTableLng;
colorTableLng=256;
// 创建调色板
HPALETTE hPalette=0,hOldPal;
if (colorTableLng!=0)
{
//定义颜色表指针pColorTable,指向DIB的颜色表
//申请缓冲区,生成LOGPALETTE结构
LPLOGPALETTE pLogPal = (LPLOGPALETTE)new char[2*sizeof(WORD)
+colorTableLng * sizeof(PALETTEENTRY)];
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries =colorTableLng;
for(int i = 0; i < colorTableLng; i++)
{
pLogPal->palPalEntry[i].peRed= pColorTable[i].rgbRed;
pLogPal->palPalEntry[i].peGreen =pColorTable[i].rgbGreen;
pLogPal->palPalEntry[i].peBlue = pColorTable[i].rgbBlue;
pLogPal->palPalEntry[i].peFlags = 0;
}
//
//创建逻辑调色板
hPalette =::CreatePalette(pLogPal);
// 将调色板选入系统
hOldPal=::SelectPalette(m_pDC->GetSafeHdc(), hPalette, TRUE);
//实现调色板
m_pDC->RealizePalette();
//释放缓冲区
delete []pLogPal;
}
}
void CTestView::ShowBmp(char *bmp)
{
if( m_bmp.m_hObject != NULL )
m_bmp.DeleteObject();
CBitmap Bitmap;
HBITMAP hBitmap;
BITMAP bmInfo;
CString StrFileName="txt.bmp";
hBitmap = (HBITMAP)LoadImage(NULL,StrFileName,
IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
Bitmap.Attach(hBitmap);
DCCompatible.SelectObject(&Bitmap);
Bitmap.GetObject(sizeof(bmInfo),&bmInfo);
pDC->BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight, &DCCompatible, 0,0,SRCCOPY);
}
void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bLButtonDown = false;
CView::OnLButtonUp(nFlags, point);
}
void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_nLDown_x=point.x;
m_nLDown_y=point.y;
m_bLButtonDown = true;
CView::OnLButtonDown(nFlags, point);
}
void CTestView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bRButtonDown = false;
CView::OnRButtonUp(nFlags, point);
}
void CTestView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_nRDown_x = point.x;
m_nRDown_y = point.y;
m_bRButtonDown = true;
CView::OnRButtonDown(nFlags, point);
}
void CTestView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
double nWidth, nCenter;
LONG lYDistance, lXDistance;
int nFactor, nBits;
GetBmpSize();
if (m_bIsOpened == true && m_bLButtonDown ==true)
{
if (point.y - m_nLDown_y>10 )
{
lYDistance = point.y - m_nLDown_y;
lXDistance = point.x - m_nLDown_x;
}
if ( point.y-m_nLDown_y <10)
{
lYDistance = point.y - m_nLDown_y;
lXDistance = point.x - m_nLDown_x;
}
m_pDicomImg->getWindow(nCurCenter, nCurWidth);
nFactor = 10;
nWidth = nCurWidth + lXDistance*nFactor;
nCenter = nCurCenter + lYDistance*nFactor;
// nBits = m_pDicomImg->getDepth();
// nWidth = nWidth<0?0:nWidth;
// nCenter = nCenter<0?0:nCenter;
// int nTopPV = pow(2, nBits) -1;
// nWidth = nWidth>nTopPV?nTopPV:nWidth;
// nCenter = nCenter>nTopPV?nTopPV:nCenter;
if (nCenter>nCurCenter/3 && nCurCenter<nCurCenter*3 &&nWidth>nCurWidth/3 && nWidth<nCurWidth*3)
{
m_pDicomImg->setWindow(nCenter,nWidth);
nCurCenter = nCenter;
nCurWidth = nWidth;
}
BMPHead();
m_pDicomImg->writeBMP("txt.bmp",24);
ShowBmp("txt.bmp");
nCurCenter = nCenter;
nCurWidth = nWidth;
}
if (m_bIsOpened == true && m_bRButtonDown ==true)
{
GetClientRect(Black_Rect);
Image_Rect.TopLeft().x=0;
Image_Rect.TopLeft().y =0;
Image_Rect.BottomRight().x = m_nBmpWidth;
Image_Rect.BottomRight().y = m_nBmpHeight;
Right_Rect.TopLeft().x = Black_Rect.Width()-Image_Rect.Width();
Right_Rect.TopLeft().y = 0;
Right_Rect.BottomRight().x = Black_Rect.Width();
Right_Rect.BottomRight().y = Image_Rect.Height();
Under_Rect.TopLeft().x =0;
Under_Rect.TopLeft().y = Black_Rect.Height()-Image_Rect.Height();
Under_Rect.BottomRight().x = Black_Rect.Width();
Under_Rect.BottomRight().y = Black_Rect.Height();
if (m_nWidth >=100)
{
if ( point.y-m_nRDown_y <0)
{
m_nWidth=m_nWidth*0.99;
m_nHeight = m_nHeight* 0.99;
// m_nXFactor = m_nXFactor-0.1;
}
}
if ( point.y-m_nRDown_y >0)
{
m_nWidth=m_nWidth* 1.01;
m_nHeight = m_nHeight* 1.01;
// m_nYFactor = m_nYFactor+0.1;
}
DicomImage *DicomImg =m_pDicomImg->createScaledImage((const unsigned long )m_nWidth,(const unsigned long )m_nHeight,0,0);
// DicomImage *DicomImg = m_pDicomImg ->createScaledImage(m_nXFactor,m_nYFactor,0,0);
if (m_nHeight >100 && m_nWidth>100)
{
DicomImg->writeBMP("txt.bmp",24);
ShowBmp("txt.bmp");
}
GetBmpSize();
m_nLDown_y = point.y;
m_nLDown_x = point.x;
delete DicomImg;
}
// CTestView::OnMouseMove(nFlags, point);
}
void CTestView::GetBmpSize()
{
CFile MyFile;
if(!MyFile.Open("TXT.BMP",CFile::modeRead))
return;
BITMAPFILEHEADER bmfHeader;
//读位图文件头信息
if (MyFile.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return ;
//判断是否是BMP
if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
return ;
//读位图头信息
BITMAPINFOHEADER bmiHeader;
if (MyFile.Read((LPSTR)&bmiHeader, sizeof(bmiHeader)) !=sizeof(bmiHeader))
return ;
//获得大小信息
m_nBmpWidth=bmiHeader.biWidth;
m_nBmpHeight=bmiHeader.biHeight;
CString StrInfo;
StrInfo.Format("%s当前的大小为:%d X %d","txt.bmp",bmiHeader.biWidth,bmiHeader.biHeight);
AfxGetMainWnd()->SetWindowText(StrInfo);
}
int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
pDC=new CClientDC(this);
DCCompatible.CreateCompatibleDC(pDC);
return 0;
}
it 's the Demo image: