⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dicomreaddlg.cpp

📁 转化为DIB位图再显示出来的dicom文件C++代码
💻 CPP
字号:
// DicomReadDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DicomRead.h"
#include "DicomReadDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDicomReadDlg dialog

CDicomReadDlg::CDicomReadDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDicomReadDlg::IDD, pParent)
{
	di=0;
	b_Isediting=false;
	//{{AFX_DATA_INIT(CDicomReadDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CDicomReadDlg::~CDicomReadDlg()
{
	if(di)
	{
		delete di;
		di=0;
	}
}

void CDicomReadDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDicomReadDlg)
	DDX_Control(pDX, IDC_SW, m_sw);
	DDX_Control(pDX, IDC_SC, m_sc);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDicomReadDlg, CDialog)
//{{AFX_MSG_MAP(CDicomReadDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_KILLFOCUS(IDC_W, OnKillfocusW)
ON_EN_KILLFOCUS(IDC_C, OnKillfocusC)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SW, OnReleasedcaptureSw)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SC, OnReleasedcaptureSc)
ON_BN_CLICKED(IDC_ChoosePath, OnChoosePath)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SW, OnCustomdrawSw)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_SW, OnOutofmemorySw)
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDicomReadDlg message handlers

BOOL CDicomReadDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	SetDlgItemInt(IDC_C,0);
	SetDlgItemInt(IDC_W,0);
	m_sw.SetRange(0, 10000);
	m_sc.SetRange(0,10000);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDicomReadDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDicomReadDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CPaintDC dc(this); // device context for painting
		float wandh;
		int oldStretchBltMode;
		oldStretchBltMode=dc.SetStretchBltMode(COLORONCOLOR);
		wandh=((float)dib.GetWidth())/((float)dib.GetHeight());
		dib.Display(&dc,0,0,600*wandh,600,0,0,dib.GetWidth(),dib.GetHeight());
		CDialog::OnPaint();
		dc.SetStretchBltMode(oldStretchBltMode);
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDicomReadDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDicomReadDlg::OnKillfocusW() 
{
	if(di)
	{
		di->setWindow(GetDlgItemInt(IDC_C),GetDlgItemInt(IDC_W));
		RefreshDraw();
	}
	m_sc.SetPos(GetDlgItemInt(IDC_C));
	m_sw.SetPos(GetDlgItemInt(IDC_W));
	Invalidate(false);
}

void CDicomReadDlg::OnKillfocusC() 
{
	if(di)
	{
		di->setWindow(GetDlgItemInt(IDC_C),GetDlgItemInt(IDC_W));
		RefreshDraw();
	}
	m_sc.SetPos(GetDlgItemInt(IDC_C));
	m_sw.SetPos(GetDlgItemInt(IDC_W));
	Invalidate(false);
}



void CDicomReadDlg::OnReleasedcaptureSw(NMHDR* pNMHDR, LRESULT* pResult) 
{
	SetDlgItemInt(IDC_W,m_sw.GetPos());
	OnKillfocusW();
	*pResult = 0;
}

void CDicomReadDlg::OnReleasedcaptureSc(NMHDR* pNMHDR, LRESULT* pResult) 
{
	SetDlgItemInt(IDC_C,m_sc.GetPos());
	OnKillfocusC();
	*pResult = 0;
}


void CDicomReadDlg::OnChoosePath() 
{
	char NowPath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,NowPath);
	CFileDialog* FD = new CFileDialog(TRUE,NULL,NULL,
		OFN_FILEMUSTEXIST,
		NULL, this);	
	FD->m_ofn.lpstrTitle = _T("选择DICOM文件");
	if(FD->DoModal()==IDOK)
	{
		SetDlgItemText(IDC_FILEPATH,FD->GetPathName());
	}
	SetCurrentDirectory(NowPath);
	if(di)
	{
		delete di;
		di=0;
	}
	di=new DicomImage(FD->GetPathName());
	if(!di)
		MessageBox("打开文件失败!");
	di->flipImage(0,1);
	RefreshDraw();
}

void CDicomReadDlg::OnCustomdrawSw(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;
}

void CDicomReadDlg::OnOutofmemorySw(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;
}

void CDicomReadDlg::RefreshDraw()
{
	if (di != NULL)
	{
		if (di->getStatus() == EIS_Normal)
		{
			const int gap =  (4 - di->getWidth() & 0x3) & 0x3 ;
            const unsigned long pixnum = OFstatic_cast(unsigned long, di->getWidth() + gap) * OFstatic_cast(unsigned long, di->getHeight());
			
			//DWORD pixnum=di->getHeight()*di->getWidth()*2;
			void* pixdata=malloc(pixnum);
			DWORD GetNum=di->createWindowsDIB(pixdata,pixnum,0,8,0,1);
			if(!GetNum)
			{
				MessageBox("读取图像信息失败");
				free(pixdata);
				return;
			}
			const int bits=8;
			void * fp;
			fp=malloc(14 /*sizeof(SB_BitmapFileHeader)*/ + 40 /*sizeof(SB_BitmapInfoHeader)*/ + 1024 + GetNum);
			if ((pixdata != NULL) && (GetNum > 0))
			{
				/* number of bytes */
				SB_BitmapFileHeader fileHeader;
				SB_BitmapInfoHeader infoHeader;
				Uint32 *palette = (bits == 8) ? new Uint32[256] : OFstatic_cast(Uint32 *, NULL);
				/* fill bitmap file header with data */
				fileHeader.bfType[0] = 'B';
				fileHeader.bfType[1] = 'M';
				fileHeader.bfSize = 14 /*sizeof(SB_BitmapFileHeader)*/ + 40 /*sizeof(SB_BitmapInfoHeader)*/ + GetNum;
				fileHeader.bfReserved1 = 0;
				fileHeader.bfReserved2 = 0;
				fileHeader.bfOffBits = 14 /*sizeof(SB_BitmapFileHeader)*/ + 40 /*sizeof(SB_BitmapInfoHeader)*/;
				/* fill bitmap info header with data */
				infoHeader.biSize = 40 /*sizeof(SB_BitmapInfoHeader)*/;
				infoHeader.biWidth = di->getHeight();
				infoHeader.biHeight = di->getWidth();
				infoHeader.biPlanes = 1;
				infoHeader.biBitCount = bits;
				infoHeader.biCompression = 0;
				infoHeader.biSizeImage = 0;
				infoHeader.biXPelsPerMeter = 0;
				infoHeader.biYPelsPerMeter = 0;
				infoHeader.biClrUsed = 0;
				infoHeader.biClrImportant = 0;
				/* create and fill color palette */
				if (palette != NULL)
				{
					/* add palette size */
					fileHeader.bfSize += 256 * 4;
					fileHeader.bfOffBits += 256 * 4;
					/* fill palette entries with gray values, format: B-G-R-0 */
					for (Uint32 i = 0; i < 256; ++i)
						palette[i] = (i << 16) | (i << 8) | i;
				}
				/* swap bytes if necessary */
				if (gLocalByteOrder != EBO_LittleEndian)
				{
					/* other data elements are always '0' and, therefore, can remain as they are */
					swap4Bytes(OFreinterpret_cast(Uint8 *, &fileHeader.bfSize));
					swap4Bytes(OFreinterpret_cast(Uint8 *, &fileHeader.bfOffBits));
					swap4Bytes(OFreinterpret_cast(Uint8 *, &infoHeader.biSize));
					swap4Bytes(OFreinterpret_cast(Uint8 *, &infoHeader.biWidth));
					swap4Bytes(OFreinterpret_cast(Uint8 *, &infoHeader.biHeight));
					swap2Bytes(OFreinterpret_cast(Uint8 *, &infoHeader.biPlanes));
					swap2Bytes(OFreinterpret_cast(Uint8 *, &infoHeader.biBitCount));
					if (palette != NULL)
						swapBytes(OFreinterpret_cast(Uint8 *, palette), 256 * 4 /*byteLength*/, 4 /*valWidth*/);
				}
				void* topfp=fp;
				memcpy(fp,&fileHeader.bfType, sizeof(fileHeader.bfType));
				fp=(void*)((DWORD)fp+sizeof(fileHeader.bfType));
				memcpy(fp,&fileHeader.bfSize, sizeof(fileHeader.bfSize));
				fp=(void*)((DWORD)fp+sizeof(fileHeader.bfSize));
				memcpy(fp,&fileHeader.bfReserved1, sizeof(fileHeader.bfReserved1));
				fp=(void*)((DWORD)fp+sizeof(fileHeader.bfReserved1));
				memcpy(fp,&fileHeader.bfReserved2, sizeof(fileHeader.bfReserved2));
				fp=(void*)((DWORD)fp+sizeof(fileHeader.bfReserved2));
				memcpy(fp,&fileHeader.bfOffBits, sizeof(fileHeader.bfOffBits));
				/* write bitmap info header: do not write the struct because of 32-bit alignment  */
				fp=(void*)((DWORD)fp+sizeof(fileHeader.bfOffBits));
				memcpy(fp,&infoHeader.biSize, sizeof(infoHeader.biSize));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biSize));
				memcpy(fp,&infoHeader.biWidth, sizeof(infoHeader.biWidth));
				fp=(void*)((DWORD)fp+ sizeof(infoHeader.biWidth));
				memcpy(fp,&infoHeader.biHeight, sizeof(infoHeader.biHeight));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biHeight));
				memcpy(fp,&infoHeader.biPlanes, sizeof(infoHeader.biPlanes));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biPlanes));
				memcpy(fp,&infoHeader.biBitCount, sizeof(infoHeader.biBitCount));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biBitCount));
				memcpy(fp,&infoHeader.biCompression, sizeof(infoHeader.biCompression));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biCompression));
				memcpy(fp,&infoHeader.biSizeImage, sizeof(infoHeader.biSizeImage));
				fp=(void*)((DWORD)fp+ sizeof(infoHeader.biSizeImage));
				memcpy(fp,&infoHeader.biXPelsPerMeter, sizeof(infoHeader.biXPelsPerMeter));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biXPelsPerMeter));
				memcpy(fp,&infoHeader.biYPelsPerMeter, sizeof(infoHeader.biYPelsPerMeter));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biYPelsPerMeter));
				memcpy(fp,&infoHeader.biClrUsed, sizeof(infoHeader.biClrUsed));
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biClrUsed));
				memcpy(fp,&infoHeader.biClrImportant, sizeof(infoHeader.biClrImportant));
				/* write color palette (if applicable) */
				fp=(void*)((DWORD)fp+sizeof(infoHeader.biClrImportant));
				if (palette != NULL)
				{
					memcpy(fp,palette, 1024);
					fp=(void*)((DWORD)fp+1024);
				}
				/* write pixel data */
				memcpy(fp,pixdata, OFstatic_cast(size_t, GetNum));
				/* delete color palette */
				delete[] palette;
				dib.Read(topfp);
				free(pixdata);
				free(topfp);
				pixdata=0;
				fp=0;
				topfp=0;
			}
			else
			{
				MessageBox("图像读取失败!");
				return;
			}
		}
		else
		{
			MessageBox("图像读取失败!");
			return;
		}
	}
				else
				{
					MessageBox("图像读取失败!");
					return;
				}
				Invalidate(false);
}

void CDicomReadDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	b_Isediting=true;
	mousex=point.x;
	mousey=point.y;
	CDialog::OnRButtonDown(nFlags, point);
}

void CDicomReadDlg::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	b_Isediting=false;
	CDialog::OnRButtonUp(nFlags, point);
}

void CDicomReadDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(b_Isediting)
	{
		long windowcen,windowwin;
		windowcen=(point.x-mousex)*3;
		windowwin=(point.y-mousey)*3;
		mousex=point.x;
		mousey=point.y;
		SetDlgItemInt(IDC_W,GetDlgItemInt(IDC_W)+windowwin);
		SetDlgItemInt(IDC_C,GetDlgItemInt(IDC_C)+windowcen);
		OnKillfocusW();
		OnKillfocusC();
	}
	CDialog::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -