Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / MFC

Function to load a bitmap on a dialog

Rate me:
Please Sign up or sign in to vote.
1.31/5 (21 votes)
6 Apr 2006CPOL 62K   1.8K   13  
Here is a function to load a bitmap on any dialog.
// SBitDialogDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SBitDialog.h"
#include "SBitDialogDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int flag1=0,flag2=0;

/////////////////////////////////////////////////////////////////////////////
// CSBitDialogDlg dialog

CSBitDialogDlg::CSBitDialogDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSBitDialogDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSBitDialogDlg)
	m_id = _T("");
	m_password = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSBitDialogDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSBitDialogDlg)
	DDX_Control(pDX, IDC_BUTTON2, m_grass);
	DDX_Control(pDX, IDCANCEL, m_cancel);
	DDX_Control(pDX, IDOK, m_ply);
	DDX_Text(pDX, IDC_EDIT2, m_id);
	DDX_Text(pDX, IDC_EDIT3, m_password);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSBitDialogDlg, CDialog)
	//{{AFX_MSG_MAP(CSBitDialogDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, Onlogin)
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSBitDialogDlg message handlers

BOOL CSBitDialogDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	// TODO: Add extra initialization here
	//m_grass.MakeButton("GRASS BACKGROUND",IDB_button1N,IDB_button1P,IDB_button1N,IDB_button1N);
    //m_ply.MakeButton("PLYWOOD BACKGROUND",IDB_button1N,IDB_button1P,IDB_button1N,IDB_button1N);
	//m_cancel.MakeButton("CANCEL",IDB_button1N,IDB_button1P,IDB_button1N,IDB_button1N);
	
	           
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CSBitDialogDlg::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
	{
		CDialog::OnPaint();
	}
}

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

void CSBitDialogDlg::Onlogin() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString id=m_id;
	CString password=m_password;
	UpdateData(FALSE);
	if((id=="trek")&&(password=="trek"))
	{
		flag1=1;
		AfxMessageBox("WElCOME!  now you can change ur background.");
		
	}
     	else
	{
	   	AfxMessageBox("Invalid Username Or Password. Please Re-Enter!");
		UpdateData(TRUE);
		m_id="";
		m_password="";
		UpdateData(FALSE);
    }
	
}

bool CSBitDialogDlg::SBitdraw(CDC *pDC, UINT nIDResource, int i)
{
	CBitmap* m_bitmap;
	m_bitmap=new CBitmap();
	m_bitmap->LoadBitmap(nIDResource);
	if(!m_bitmap->m_hObject)
		return true;
    CRect rect;
	GetClientRect(&rect);
	CDC dc;
	dc.CreateCompatibleDC(pDC);	
	dc.SelectObject(m_bitmap);
	int bmw, bmh ;
	BITMAP bmap;
	m_bitmap->GetBitmap(&bmap);
	bmw = bmap.bmWidth;
	bmh = bmap.bmHeight;
	int xo=0, yo=0;
	switch(i){
	case 1:
	pDC->StretchBlt(xo, yo, rect.Width(),
			rect.Height(), &dc,
			0, 0,bmw,bmh, SRCCOPY);
	     break;
	case 2:
			if(bmw < rect.Width())
			xo = (rect.Width() - bmw)/2;
		else 
			xo=0;
		if(bmh < rect.Height())
			yo = (rect.Height()-bmh)/2;
		else
			yo=0;
		pDC->BitBlt (xo, yo, rect.Width(),
			rect.Height(), &dc,
			0, 0, SRCCOPY);
		break;
    case 3:
		for (yo = 0; yo < rect.Height(); yo += bmh)
		{
			for (xo = 0; xo < rect.Width(); xo += bmw)
			{
				pDC->BitBlt (xo, yo, rect.Width(),
					rect.Height(), &dc,
					0, 0, SRCCOPY);
			}
		}
	}
	return true;

}

BOOL CSBitDialogDlg::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnEraseBkgnd(pDC);
	if((flag2==1)&&(flag1==1))
	{
		SBitdraw(pDC,IDB_back1,3);
		
	}
    if((flag2==2)&&(flag1==1))
	{
		SBitdraw(pDC,IDB_back2,3);
		
	}
	return true;
}

void CSBitDialogDlg::OnOK() 
{
	// TODO: Add extra validation here
	flag2=1;
	CClientDC Client(this);
    OnEraseBkgnd(&Client) ;
	Invalidate();
	AddBit1();
	
}

void CSBitDialogDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	flag2=2;
	CClientDC Client(this);
    OnEraseBkgnd(&Client) ;
	Invalidate();
	AddBit2();
	
	
}

bool CSBitDialogDlg::AddBit1()
{
    if(flag1==1)
	{
	m_grass.SetSkin(IDB_button1N,IDB_button1P,IDB_button1N,IDB_button1N,NULL,NULL,0,0,0);
	m_ply.SetSkin(IDB_button1N,IDB_button1P,IDB_button1N,IDB_button1N,NULL,NULL,0,0,0);
    m_cancel.SetSkin(IDB_button1N,IDB_button1P,IDB_button1N,IDB_button1N,NULL,NULL,0,0,0);
	}
	return true;
}

bool CSBitDialogDlg::AddBit2()
{
	if(flag1==1)
	{
	m_grass.SetSkin(IDB_button2N,IDB_button2P,IDB_button2N,IDB_button2N,NULL,NULL,0,0,0);
	m_ply.SetSkin(IDB_button2N,IDB_button2P,IDB_button2N,IDB_button2N,NULL,NULL,0,0,0);
    m_cancel.SetSkin(IDB_button2N,IDB_button2P,IDB_button2N,IDB_button2N,NULL,NULL,0,0,0);
	}
	return true;

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This is Saday Chand Sarkar , New in MFC world.
Working in Trek Technology Pte.Ltd.

Comments and Discussions