// MainFrm.cpp : implementation of the CMainFrame class
//#include "stdafx.h"
#include "Vista.h"#include "MainFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()
//}}AFX_MSG_MAP
ON_MESSAGE (WM_USER_UPDATE_STATS, OnUpdateImageStats)
END_MESSAGE_MAP()static UINT indicators[] =
{
ID_SEPARATOR,
ID_SEPARATOR
};/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destructionCMainFrame::CMainFrame()
{
}CMainFrame::~CMainFrame()
{
}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1; //
// Create the status bar.
//
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
} //
// Size the status bar's rightmost pane to hold a text string.
//
TEXTMETRIC tm;
CClientDC dc (this);
CFont* pFont = m_wndStatusBar.GetFont ();
CFont* pOldFont = dc.SelectObject (pFont);
dc.GetTextMetrics (&tm);
dc.SelectObject (pOldFont); int cxWidth;
UINT nID, nStyle;
m_wndStatusBar.GetPaneInfo (1, nID, nStyle, cxWidth);
cxWidth = tm.tmAveCharWidth * 24;
m_wndStatusBar.SetPaneInfo (1, nID, nStyle, cxWidth);
return 0;
}BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
return TRUE;
}/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlersBOOL CMainFrame::OnQueryNewPalette()
{
CDocument* pDoc = GetActiveDocument ();
if (pDoc != NULL)
GetActiveDocument ()->UpdateAllViews (NULL);
return TRUE;
}void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd)
{
if (pFocusWnd != this) {
CDocument* pDoc = GetActiveDocument ();
if (pDoc != NULL)
GetActiveDocument ()->UpdateAllViews (NULL);
}
}LRESULT CMainFrame::OnUpdateImageStats (WPARAM wParam, LPARAM lParam)
{
m_wndStatusBar.SetPaneText (1, (LPCTSTR) lParam, TRUE);
return 0;
}
// Vista.cpp : Defines the class behaviors for the application.
//#include "stdafx.h"
#include "Vista.h"#include "MainFrm.h"
#include "VistaDoc.h"
#include "VistaView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CVistaAppBEGIN_MESSAGE_MAP(CVistaApp, CWinApp)
//{{AFX_MSG_MAP(CVistaApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CVistaApp constructionCVistaApp::CVistaApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CVistaApp objectCVistaApp theApp;/////////////////////////////////////////////////////////////////////////////
// CVistaApp initializationBOOL CVistaApp::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need. // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CVistaDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CVistaView));
AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE; // The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow(); return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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)
// No message handlers
//}}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()// App command to run the dialog
void CVistaApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}/////////////////////////////////////////////////////////////////////////////
// CVistaApp message handlers
//#include "stdafx.h"
#include "Vista.h"#include "MainFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()
//}}AFX_MSG_MAP
ON_MESSAGE (WM_USER_UPDATE_STATS, OnUpdateImageStats)
END_MESSAGE_MAP()static UINT indicators[] =
{
ID_SEPARATOR,
ID_SEPARATOR
};/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destructionCMainFrame::CMainFrame()
{
}CMainFrame::~CMainFrame()
{
}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1; //
// Create the status bar.
//
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
} //
// Size the status bar's rightmost pane to hold a text string.
//
TEXTMETRIC tm;
CClientDC dc (this);
CFont* pFont = m_wndStatusBar.GetFont ();
CFont* pOldFont = dc.SelectObject (pFont);
dc.GetTextMetrics (&tm);
dc.SelectObject (pOldFont); int cxWidth;
UINT nID, nStyle;
m_wndStatusBar.GetPaneInfo (1, nID, nStyle, cxWidth);
cxWidth = tm.tmAveCharWidth * 24;
m_wndStatusBar.SetPaneInfo (1, nID, nStyle, cxWidth);
return 0;
}BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
return TRUE;
}/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlersBOOL CMainFrame::OnQueryNewPalette()
{
CDocument* pDoc = GetActiveDocument ();
if (pDoc != NULL)
GetActiveDocument ()->UpdateAllViews (NULL);
return TRUE;
}void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd)
{
if (pFocusWnd != this) {
CDocument* pDoc = GetActiveDocument ();
if (pDoc != NULL)
GetActiveDocument ()->UpdateAllViews (NULL);
}
}LRESULT CMainFrame::OnUpdateImageStats (WPARAM wParam, LPARAM lParam)
{
m_wndStatusBar.SetPaneText (1, (LPCTSTR) lParam, TRUE);
return 0;
}
// Vista.cpp : Defines the class behaviors for the application.
//#include "stdafx.h"
#include "Vista.h"#include "MainFrm.h"
#include "VistaDoc.h"
#include "VistaView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CVistaAppBEGIN_MESSAGE_MAP(CVistaApp, CWinApp)
//{{AFX_MSG_MAP(CVistaApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CVistaApp constructionCVistaApp::CVistaApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CVistaApp objectCVistaApp theApp;/////////////////////////////////////////////////////////////////////////////
// CVistaApp initializationBOOL CVistaApp::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need. // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CVistaDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CVistaView));
AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE; // The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow(); return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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)
// No message handlers
//}}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()// App command to run the dialog
void CVistaApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}/////////////////////////////////////////////////////////////////////////////
// CVistaApp message handlers
//#include "stdafx.h"
#include "Vista.h"#include "VistaDoc.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CVistaDocIMPLEMENT_DYNCREATE(CVistaDoc, CDocument)BEGIN_MESSAGE_MAP(CVistaDoc, CDocument)
//{{AFX_MSG_MAP(CVistaDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CVistaDoc construction/destructionCVistaDoc::CVistaDoc()
{
}CVistaDoc::~CVistaDoc()
{
}BOOL CVistaDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}/////////////////////////////////////////////////////////////////////////////
// CVistaDoc serializationvoid CVistaDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}/////////////////////////////////////////////////////////////////////////////
// CVistaDoc diagnostics#ifdef _DEBUG
void CVistaDoc::AssertValid() const
{
CDocument::AssertValid();
}void CVistaDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CVistaDoc commandsBOOL CVistaDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument (lpszPathName))
return FALSE; //
// Open the file and create a DIB section from its contents.
//
HBITMAP hBitmap = (HBITMAP) ::LoadImage (NULL, lpszPathName,
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); if (hBitmap == NULL) {
CString string;
string.Format (_T ("%s does not contain a DIB"), lpszPathName);
AfxMessageBox (string);
return FALSE;
} m_bitmap.Attach (hBitmap); //
// Return now if this device doesn't support palettes.
//
CClientDC dc (NULL);
if ((dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) == 0)
return TRUE; //
// Create a palette to go with the DIB section.
//
if ((HBITMAP) m_bitmap != NULL) {
DIBSECTION ds;
m_bitmap.GetObject (sizeof (DIBSECTION), &ds); int nColors;
if (ds.dsBmih.biClrUsed != 0)
nColors = ds.dsBmih.biClrUsed;
else
nColors = 1 << ds.dsBmih.biBitCount; //
// Create a halftone palette if the DIB section contains more
// than 256 colors.
//
if (nColors > 256)
m_palette.CreateHalftonePalette (&dc); //
// Create a custom palette from the DIB section's color table
// if the number of colors is 256 or less.
//
else {
RGBQUAD* pRGB = new RGBQUAD[nColors]; CDC memDC;
memDC.CreateCompatibleDC (&dc);
CBitmap* pOldBitmap = memDC.SelectObject (&m_bitmap);
::GetDIBColorTable ((HDC) memDC, 0, nColors, pRGB);
memDC.SelectObject (pOldBitmap); UINT nSize = sizeof (LOGPALETTE) +
(sizeof (PALETTEENTRY) * (nColors - 1));
LOGPALETTE* pLP = (LOGPALETTE*) new BYTE[nSize]; pLP->palVersion = 0x300;
pLP->palNumEntries = nColors; for (int i=0; i<nColors; i++) {
pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
} m_palette.CreatePalette (pLP);
delete[] pLP;
delete[] pRGB;
}
}
return TRUE;
}void CVistaDoc::DeleteContents()
{
if ((HBITMAP) m_bitmap != NULL)
m_bitmap.DeleteObject (); if ((HPALETTE) m_palette != NULL)
m_palette.DeleteObject ();
CDocument::DeleteContents();
}CBitmap* CVistaDoc::GetBitmap()
{
return ((HBITMAP) m_bitmap == NULL) ? NULL : &m_bitmap;
}CPalette* CVistaDoc::GetPalette()
{
return ((HPALETTE) m_palette == NULL) ? NULL : &m_palette;
}
// VistaView.cpp : implementation of the CVistaView class
//#include "stdafx.h"
#include "Vista.h"#include "VistaDoc.h"
#include "VistaView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CVistaViewIMPLEMENT_DYNCREATE(CVistaView, CScrollView)BEGIN_MESSAGE_MAP(CVistaView, CScrollView)
//{{AFX_MSG_MAP(CVistaView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CVistaView construction/destructionCVistaView::CVistaView()
{
}CVistaView::~CVistaView()
{
}BOOL CVistaView::PreCreateWindow(CREATESTRUCT& cs)
{
return CScrollView::PreCreateWindow(cs);
}/////////////////////////////////////////////////////////////////////////////
// CVistaView drawingvoid CVistaView::OnDraw(CDC* pDC)
{
CVistaDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); CBitmap* pBitmap = pDoc->GetBitmap (); if (pBitmap != NULL) {
CPalette* pOldPalette;
CPalette* pPalette = pDoc->GetPalette (); if (pPalette != NULL) {
pOldPalette = pDC->SelectPalette (pPalette, FALSE);
pDC->RealizePalette ();
} DIBSECTION ds;
pBitmap->GetObject (sizeof (DIBSECTION), &ds); CDC memDC;
memDC.CreateCompatibleDC (pDC);
CBitmap* pOldBitmap = memDC.SelectObject (pBitmap); pDC->BitBlt (0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight, &memDC,
0, 0, SRCCOPY); memDC.SelectObject (pOldBitmap); if (pPalette != NULL)
pDC->SelectPalette (pOldPalette, FALSE);
}
}void CVistaView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate (); CString string;
CSize sizeTotal;
CBitmap* pBitmap = GetDocument ()->GetBitmap (); //
// If a bitmap is loaded, set the view size equal to the bitmap size.
// Otherwise, set the view's width and height to 0.
//
if (pBitmap != NULL) {
DIBSECTION ds;
pBitmap->GetObject (sizeof (DIBSECTION), &ds);
sizeTotal.cx = ds.dsBm.bmWidth;
sizeTotal.cy = ds.dsBm.bmHeight;
string.Format (_T ("\t%d x %d, %d bpp"), ds.dsBm.bmWidth,
ds.dsBm.bmHeight, ds.dsBmih.biBitCount);
}
else {
sizeTotal.cx = sizeTotal.cy = 0;
string.Empty ();
} AfxGetMainWnd ()->SendMessage (WM_USER_UPDATE_STATS, 0,
(LPARAM) (LPCTSTR) string);
SetScrollSizes (MM_TEXT, sizeTotal);
}/////////////////////////////////////////////////////////////////////////////
// CVistaView diagnostics#ifdef _DEBUG
void CVistaView::AssertValid() const
{
CScrollView::AssertValid();
}void CVistaView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}CVistaDoc* CVistaView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVistaDoc)));
return (CVistaDoc*)m_pDocument;
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CVistaView message handlers