// WinCapView.cpp : implementation of the CWinCapView class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "WinCapView.h"
CWinCapView::CWinCapView() : m_hBitmap(NULL),m_bSaved(false)
{
m_size.cx = 0;
m_size.cy = 0;
}
void CWinCapView::BuildBitmap(HWND hWnd,TCHAR* pNameString)
{
RECT rect;
::GetWindowRect(hWnd,&rect);
m_size.cx = rect.right - rect.left;
m_size.cy = rect.bottom - rect.top;
SetScrollOffset(0, 0, FALSE);
SetScrollSize(m_size);
HDC hDC = ::GetDC(NULL);
HDC hMemDC = ::CreateCompatibleDC(hDC);
m_hBitmap = ::CreateCompatibleBitmap(hDC,m_size.cx,m_size.cy);
HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hMemDC,m_hBitmap);
BitBlt(hMemDC,0,0,rect.right,rect.bottom,hDC,rect.left,rect.top,SRCCOPY);
::SelectObject(hMemDC,hOldBitmap);
::DeleteDC(hMemDC);
::ReleaseDC(NULL,hDC);
InvalidateRect(NULL);
strcpy_s(m_NameString,pNameString);
if (strlen(m_NameString) == 0)
strcpy_s(m_NameString,_T("Temp"));
strcat_s(m_NameString,_T(".bmp"));
}
HBITMAP& CWinCapView::GetBitmapHandle()
{
return m_hBitmap;
}
BOOL CWinCapView::PreTranslateMessage(MSG* pMsg)
{
pMsg;
return FALSE;
}
void CWinCapView::DoPaint(CDCHandle dc)
{
if(m_hBitmap)
DrawBitmap(dc.m_hDC,m_hBitmap);
}
bool CWinCapView::IsSaved()
{
return m_bSaved;
}
void CWinCapView::SavedIt()
{
if (m_bSaved)
{
MessageBox(_T("The image already has been saved."),_T("saved image"),MB_OK | MB_ICONINFORMATION);
return;
}
CFileDialog dlg(FALSE, _T("bmp"),m_NameString, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
if(dlg.DoModal() == IDOK)
{
char szExt[_MAX_EXT];
_splitpath_s(dlg.m_szFileName, NULL, 0, NULL,0, NULL, 0, szExt, _MAX_EXT);
if (_stricmp(szExt, ".bmp"))
{
MessageBox(_T("At this time only bmp file can be saved"),_T("Info"),MB_OK);
return;
}
HANDLE hf = CreateFile(dlg.m_szFileName, GENERIC_READ | GENERIC_WRITE,
(DWORD) 0, (LPSECURITY_ATTRIBUTES) NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if(hf == NULL)
{
MessageBox(_T("Unable to create file"),_T("Info"),MB_OK);
return;
}
HDC hDC = ::GetDC(NULL);
DWORD dwTmp=0;
BITMAP bmInfo;
::GetObject(m_hBitmap,sizeof(BITMAP),&bmInfo);
BITMAPINFOHEADER bmInfoh;
memset(&bmInfoh,0,sizeof(BITMAPINFOHEADER));
bmInfoh.biSize = sizeof(BITMAPINFOHEADER);
bmInfoh.biWidth = bmInfo.bmWidth;
bmInfoh.biHeight = bmInfo.bmHeight;
bmInfoh.biPlanes = bmInfo.bmPlanes;
bmInfoh.biSizeImage = bmInfo.bmWidthBytes * bmInfo.bmHeight;
bmInfoh.biBitCount = bmInfo.bmBitsPixel;
bmInfoh.biCompression = BI_RGB;
// rest of every thing we already set to zero
BITMAPFILEHEADER bmfh;
memset(&bmfh,0,sizeof(BITMAPFILEHEADER));
bmfh.bfType = 'B'+('M'<<8);
bmfh.bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
bmfh.bfSize = (DWORD) (bmfh.bfOffBits + bmInfoh.biSizeImage);
int SizeOfFileHeader = sizeof(BITMAPFILEHEADER);
// Writing file header -- the BITMAPFILEHEADER structure
if (!WriteFile(hf, (LPVOID) &bmfh, SizeOfFileHeader,(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
return;
int SizeOfBitmapHeader = sizeof(BITMAPINFOHEADER);
// Writing file header -- the BITMAPINFOHEADER structure
if (!WriteFile(hf, (LPVOID) &bmInfoh, SizeOfBitmapHeader,(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
return;
BYTE* pBits = new BYTE[bmInfoh.biSizeImage];
int NoOfLinesScaned = GetDIBits(hDC, (HBITMAP)m_hBitmap, 0, (UINT) bmInfoh.biHeight,
(LPVOID) pBits, (LPBITMAPINFO) &bmInfoh, DIB_RGB_COLORS);
BOOL nSuccess = WriteFile(hf, (LPVOID) pBits, bmInfoh.biSizeImage,(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
delete pBits;
pBits = NULL;
CloseHandle(hf);
ReleaseDC(hDC);
m_bSaved = true;
}
}
void CWinCapView::OnEditCopy()
{
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, m_hBitmap);
CloseClipboard();
}