// coverdlg.cpp : implementation file
//

#include "stdafx.h"
#include "tutorial.h"
#include "mainfrm.h"
#include "contact.h"
#include "malloc.h"

//#define INT_MAX 640
//#define TIMER 10
//#define DELAY 200

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CContactDlg dialog


CContactDlg::CContactDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CContactDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CContactDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
//    m_hDIB = NULL;
//    m_palDIB = NULL;
//    m_lpLogPal = NULL;
}

CContactDlg::~CContactDlg()
{ 
	m_font.DeleteObject();
	m_font2.DeleteObject();

	if(m_pBitmap)
	{   
	  delete m_pBitmap;
	}

/*    if ( m_palDIB )
    	delete m_palDIB;
    
    if ( m_hDIB ) {
        ::GlobalFree((HGLOBAL) m_hDIB);
        m_hDIB = NULL;
    }
*/
}

void CContactDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CContactDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CContactDlg, CDialog)
    //{{AFX_MSG_MAP(CContactDlg)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CContactDlg message handlers
/*
BOOL CContactDlg::PreTranslateMessage(MSG *pMsg)
{                    
//	if ( pMsg->wParam == VK_ESCAPE ) {
//        return 1;
//    }
   
    return CWnd::PreTranslateMessage(pMsg);
}
*/

BOOL CContactDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_pBitmap=new CBitmap;
	if(!m_pBitmap)
	{   
	  m_pBitmap=0;
	  return FALSE;
	}
	MoveWindow(0, 0, 640, 480, 1);
	GetDlgItem(IDOK)->MoveWindow(480, 430, 100, 24, 1);
	
	LOGFONT lf;
	memset(&lf,0,sizeof(LOGFONT));
    lf.lfHeight = 15;
    lf.lfWidth = 6;//lf.lfEscapement = lf.lfOrientation = 0;
    lf.lfWeight = FW_BOLD;
    strcpy(lf.lfFaceName, "Arial");
    
    m_font.CreateFontIndirect(&lf); 
    
    lf.lfWeight = FW_NORMAL;
    m_font2.CreateFontIndirect(&lf);

    
/*
    if ( m_hDIB == NULL ) {
		
        OpenDIB("c:\\tutorial\\contact.bmp");

        OnDoRealize((WPARAM)m_hWnd, 0);   // same as SendMessage(WM_DOREALIZE);
    }
    
    // TODO: Add your message handler code here
    if (m_hDIB != NULL) {
        LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
        int cxDIB = (int) ::DIBWidth(lpDIB);         // Size of DIB - x
        int cyDIB = (int) ::DIBHeight(lpDIB);        // Size of DIB - y
        ::GlobalUnlock((HGLOBAL) m_hDIB);
        m_rcDIB.top = m_rcDIB.left = 0;
        m_rcDIB.right = cxDIB;
        m_rcDIB.bottom = cyDIB;
     //   CRect rcDest = rcDIB;
    }
*/    
    return TRUE;
}

void CContactDlg::OnPaint()
{
	CPaintDC dc(this); // device context for painting
    //dc.SaveDC();
    
	CRect rectWin,rectWnd;
    GetWindowRect(&rectWin);
    rectWnd.SetRect(0,0,rectWin.Width(),rectWin.Height());
    m_pBitmap->LoadBitmap(IDB_BITMAP5);
    CMainFrame *pWnd=(CMainFrame *)AfxGetApp()->m_pMainWnd;
    pWnd->PaintWall(dc,rectWnd,m_pBitmap);
    CPoint posLine;
    posLine.x=640;
    posLine.y=437;
    pWnd->DrawControl(dc,posLine.x,posLine.y);
    
    dc.SetBkMode(TRANSPARENT);
    
    int y1=40;
    int y2=154;
    int y3=264;
    
	dc.SelectObject(&m_font);                                    
	dc.SetTextColor(RGB(255, 0, 0));
     
	dc.TextOut(75, y1, "MICROTEK INTERNATIONAL INC.");
    dc.TextOut(75, y2, "U.S.A. Office");
    dc.TextOut(75, y3, "P.R.C. Branch");                                 
	
	dc.SelectObject(&m_font2);
	dc.SetTextColor(RGB(255, 255, 255));	
	
	dc.TextOut(75, y1+18, "No.6, Industry East Road 3");
	dc.TextOut(75, y1+36, "Science-Based Industrial Park");
	dc.TextOut(75, y1+54,	"Hsinchu, Taiwan 30077, China");
	dc.TextOut(75, y1+72,	"Tel: (886)-35-772155");
	dc.TextOut(75, y1+90,	"Fax: (886)-35-776443");
	
	
	dc.TextOut(75, y2+18, "Development Systems Division");
	dc.TextOut(75, y2+36, "3300 N.W.211th Terrace");
	dc.TextOut(75, y2+54, "Tel: (503)645-733, (800)886-7333");
	dc.TextOut(75, y2+72, "Fax: (503)629-8460");
	
	
	dc.TextOut(75, y3+18, "EA and R&D Department");
	dc.TextOut(75, y3+36, "4th Floor, Building No.14");
	dc.TextOut(75, y3+54, "No. 470 Guiping Road, ShangHai, China");
	dc.TextOut(75, y3+72, "Tel: (86)-21-64851295");
	dc.TextOut(75, y3+90, "Fax: (86)-21-64853259");

	//dc.RestoreDC(-1);
}


/////////////////////////////////////////////////////////////////////////////
//
//  Name:   
//
//  Description: 
//
//  Input:  
//      
//  Output:                                          
//
//  Return: 
//
/////////////////////////////////////////////////////////////////////////////
/*
BOOL CContactDlg::OpenDIB(const char* pszPathName)
{
    CFile file;
    CFileException fe;
    if(!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite, &fe)) {
        AfxMessageBox("Open DIB error!");
        return FALSE;
    }

    // replace calls to Serialize with ReadDIBFile function
    TRY
    {
        m_hDIB = ::ReadDIBFile(file);
    }
    CATCH (CFileException, eLoad)
    {
        file.Abort(); // will not throw an exception
        AfxMessageBox("Read DIB error!");
        m_hDIB = NULL;
        return FALSE;
    }
    END_CATCH

    InitDIBData();

    if (m_hDIB == NULL) {
        // may not be DIB format
        AfxMessageBox("Couldn't load DIB");
        return FALSE;
    }
    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
//
//  Name:   
//
//  Description: 
//
//  Input:  
//
//  Output: 
//
//  Return: 
//
/////////////////////////////////////////////////////////////////////////////
void CContactDlg::InitDIBData()
{
    if (m_hDIB == NULL) return;
    
    LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
    if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX) {
        ::GlobalUnlock((HGLOBAL) m_hDIB);
        ::GlobalFree((HGLOBAL) m_hDIB);
        m_hDIB = NULL;
        AfxMessageBox("DIB is too large");
        return;
    }
    ::GlobalUnlock((HGLOBAL) m_hDIB);
    // Create copy of palette
    m_palDIB = new CPalette;
    if (m_palDIB == NULL) {
        // we must be really low on memory
        ::GlobalFree((HGLOBAL) m_hDIB);
        m_hDIB = NULL;
        return;
    }
    if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL) {
        // DIB may not have a palette
        delete m_palDIB;
        m_palDIB = NULL;
        return;
    }
}                                     

/////////////////////////////////////////////////////////////////////////////
//
//  Name:   
//
//  Description: 
//
//  Input:  
//
//  Output: 
//
//  Return: 
//
/////////////////////////////////////////////////////////////////////////////
LRESULT CContactDlg::OnDoRealize(WPARAM wParam, LPARAM)
{
    ASSERT(wParam != NULL);
    if (m_hDIB == NULL)
        return 0L;  

    if (m_palDIB != NULL) {
        CClientDC dc(this);
        CPalette* oldPalette = 
            dc.SelectPalette(m_palDIB, ((HWND)wParam) != m_hWnd);

        if (oldPalette != NULL) {
            UINT nColorsChanged = dc.RealizePalette();
            dc.SelectPalette(oldPalette, TRUE);
        } else {
            TRACE0("\tSelectPalette failed\n");
        }
    }

    return 0L;
}

*/