Loading and Displaying an Image (C++ 5.0 and later)

Take the following steps to start a project and to add some code that positions, scales, and displays an image in a dialog box.

1.

Start a new project as follows:

 

Run Microsoft Visual C++ 5.0, select the File >New menu option, and do the following:

 

a.

Click the Projects tab.

 

b.

Select MFC AppWizard (exe) as the project type

 

c.

In the Project name text box, specify tutor.

 

d.

In the Location text box, specify the path of the project.

 

e.

Click the OK button.

2.

In the Step 1 dialog box, do the following:

 

a.

Select Dialog based.

 

b.

Click the Next button.

3.

In the Step 2 of 4 dialog box, do the following:

 

a.

Ensure that About Box is selected.

 

b.

Ensure that 3D Controls is selected.

 

c.

Select ActiveX Controls

 

d.

Click the Next button.

4.

In the Step 3 of 4 dialog box, do the following:

 

a.

For comments, ensure that Yes, Please is selected.

 

b.

For how to use the MFC library, select Use MFC in a Shared DLL.

 

c.

Click the Next button.

5.

In the Step 4 of 4 dialog box, just click Finish.

6.

Read New Project Information, and click OK. (The AppWizard creates the project files and opens the project.)

7.

Add #import and #include statements to your program so you can access the LEAD COM constants and classes:

 

a.

In the Project Workspace, click the FileView tab.

 

b.

Double-click the tutor files folder to open it.

 

c.

Double-click the Header Files folder to open it.

 

d.

Double-click the StdAfx.h file to edit it.

 

Add the following lines to the end of the file (keep in mind, you may have to change the path to where the dll's reside):

#import "c:\\winnt\\system32\\ltrvr14n.dll"  no_namespace, named_guids
#import "c:\\winnt\\system32\\ltr14n.dll"  no_namespace, named_guids
#import "c:\\winnt\\system32\\ltrvw14n.ocx" no_namespace, named_guids
#import "c:\\winnt\\system32\\ltrio14n.dll" no_namespace, named_guids, exclude("LoadResizeConstants")
#import "c:\\winnt\\system32\\ltrpr14n.dll" no_namespace, named_guids

8.

Add a LEAD RasterView control to the main window as follows:

 

a.

In the Project Workspace, click the ResourceView tab.

 

b.

Double-click the tutor resources folder to open it.

 

c.

Double-click the Dialog folder to open it.

 

d.

Double-click IDD_TUTOR_DIALOG to design the form.

 

e.

Select the TODO... text control; then press the Delete key to delete it.

 

f.

From the main menu, select Project > Add to project > Components and Controls. (The Component Gallery appears.)

 

g.

Click the Registered ActiveX Contols.

 

h.

Double-click the LEAD RasterView Control (14.5) icon. (The Confirm Classes dialog box appears.)

 

i.

Ensure that both CLEADRasterView, CLEADRaster, and CPicture are checked.

 

j.

Click OK to complete the selection; then click Close to close the Component Gallery. (The LEAD RasterView control appears in the Controls toolbar.)

 

k.

image\btnlead.gif Click the LEAD RasterView control icon; then size and position the control as you want it to appear at run time.

 

l.

Use the right mouse button to edit the properties of the new LEAD RasterView control.

 

m.

Change the ID to IDC_LEADRASTERVIEW1.

9.

image\btncmd.gif Add a command button to your form and name it as follows:

 

ID

Caption

 

IDC_LOADLEAD

Load Image

10.

Press Ctrl-F4 to close all windows back to the Project Workspace.

11.

Do the following to add m_LEADRasterView1 to the CTutorDlg class and link the variable to the LEAD RasterView control using dynamic data exchange:

 

a.

Press Ctrl-W. (The MFC ClassWizard dialog box appears.)

 

b.

Click the Member Variables tab.

 

c.

In the Class Name box, select CTutorDlg.

 

d.

In the Control IDs list, select IDC_LEADRASTERVIEW1.

 

e.

Click the Add Variable... button.

 

f.

Specify m_LEADRasterView1 as the variable name, and Control as the category.

 

g.

Click OK to close the dialog box, and click OK to close the MFC ClassWizard.

12.

Go to the OnInitDialog() function as follows:

 

a.

In the Project Workspace, click the ClassView tab.

 

b.

Double-click the tutor classes folder to open it.

 

c.

Expand the CTutorDlg class.

 

d.

Double-click the OnInitDialog() function to edit it.

13.

Edit the OnInitDialog() function to add the following code after the line that says //TODO: Add extra initialization here:

// Set defaults for displaying the image.
// These are all persistent properties that can be set in the properties box.

m_LEADRasterView1.SetAppearance(RASTERVIEW_APPEARANCE_FLAT);
m_LEADRasterView1.SetBorderStyle(1);
m_LEADRasterView1.SetBackColor(RGB(255, 255, 125));
m_LEADRasterView1.SetPaintDither(PAINTDITHER_DIFFUSION);
m_LEADRasterView1.SetPaintPalette(PAINTPALETTE_AUTO);
m_LEADRasterView1.SetAutoRepaint(TRUE);
m_LEADRasterView1.SetAutoSize(FALSE);
m_LEADRasterView1.SetAutoSetRects(TRUE);
m_LEADRasterView1.SetPaintSizeMode(PAINTSIZEMODE_FIT);

14.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select CTutorDlg.

 

c.

In the Object IDs list box, select IDC_LOADLEAD.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

Click the Add function button. Choose OK for the default function name (OnLoadlead).

 

f.

Click the Edit Code button and enter the following code: (you will have to change to file name to an existing image file name on your computer)

   ILEADRasterIO *pRasterIO=NULL;
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
   pRasterIO->Load(m_LEADRasterView1.GetRaster(), "d:\\temp\\24bit.bmp", 0, 0, 1);
   pRasterIO->Release();

15.

To accommodate a 256-color display driver, continue with the remaining steps, which send WM_QUERYNEWPALETTE and WM_PALETTECHANGED messages to the LEADTOOLS RasterView ActiveX.

16.

In the Project Workspace, click the FileView tab.

17.

Edit the TUTORDLG.H file and insert the following lines in the definition of CTutorDlg class, just before DECLARE_MESSAGE_MAP():

afx_msg BOOL OnQueryNewPalette();
afx_msg void OnPaletteChanged( CWnd *pwin );

18.

Edit the TUTORDLG.CPP file and insert at the top of the file after the #include statement:

#include "leadraster.h"

19.

Edit the TUTORDLG.CPP file and insert the following lines between the lines: BEGIN_MESSAGE_MAP(CTutorDlg, CDialog) and END_MESSAGE_MAP():

ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()

20.

Go to the end of TUTORDLG.CPP file and insert the following two functions:

BOOL CTutorDlg::OnQueryNewPalette()
{
  if(!IsWindow(m_LEADRasterView1.m_hWnd))
    return FALSE;
  return m_LEADRasterView1.SendMessage(WM_QUERYNEWPALETTE);
}
void CTutorDlg::OnPaletteChanged(CWnd* pFocusWnd)
{
  if(pFocusWnd->m_hWnd == m_hWnd || !IsWindow(m_LEADRasterView1.m_hWnd))
     return;
  m_LEADRasterView1.SendMessage(WM_PALETTECHANGED, (WPARAM) pFocusWnd->m_hWnd);
}

21.

On the main menu, select Build > Build tutor.exe to build the project.

22.

On the main menu, select Build > Execute tutor.exe to run the project.