To start a project and add code that creates a new paint window:
In stdafx.h add the following headers: (keep in mind, you may have to change the path to where the header files reside):
#include "..\..\..\..\..\include\ClassLib\ltWrappr.h"
Add the following lines after //TODO: add construction code here
:
L_TCHAR * pszLicenseFile = L"Replace this with the path to the LEADTOOLS license file";
L_TCHAR * pszDeveloperKey = L"Replace this with your developer key";
LSettings::SetLicenseFile(pszLicenseFile, pszDeveloperKey);
LBase::LoadLibraries(LT_PDG|LT_KRN|LT_FIL|LT_DLG|LT_AUT|LT_CON|LT_TLB|LT_DIS);
LDialogBase::Initialize(DLG_INIT_COLOR);
Click on the "Class View" tab.
LDialogBase::Free();
LBase::UnloadLibraries(LT_PDG|LT_KRN|LT_FIL|LT_DLG|LT_AUT|LT_CON|LT_TLB|LT_DIS);
//Hide the Auto appearance window (View)
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
Create a new file called Imports.cpp in place it beside your project files:
#include "StdAfx.h"
#if defined(WIN64)
#pragma comment(lib, "..\\..\\..\\..\\..\\Lib\\CDLLVC10\\x64\\Ltwvc_x.lib")
#else
#pragma comment(lib, "..\\..\\..\\..\\..\\Lib\\CDLLVC10\\Win32\\Ltwvc_u.lib")
#endif // #if defined(WIN64)
LRasterPaintWindow m_RasterPntWnd;
LRasterDialog m_PaintDialog;
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
ON_WM_CREATE()
int CTutorialView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_RasterPntWnd.SetWndHandle(m_hWnd);
return 1;
}
protected:
LToolbar m_LToolbar;
LRasterPaintWindow::Initialize();
LRasterPaintWindow::CreateToolbar(&m_LToolbar, m_hWnd, TEXT("Test Toolbar"));
LRasterPaintWindow::SetToolbar(&m_LToolbar);
POINT ptPos = {0, 0};
RECT rcClient;
CMainFrame::GetClientRect(&rcClient);
ptPos.x = rcClient.left + 2;
ptPos.y = rcClient.top + 2;
CMainFrame::ClientToScreen(&ptPos);
LRasterPaintWindow::SetToolbarPosition(&ptPos);
LRasterPaintWindow::ShowToolbar(TRUE);
afx_msg void OnClose();
ON_WM_CLOSE()
void CMainFrame::OnClose()
{
LRasterPaintWindow::FreeToolbar(&m_LToolbar);
LRasterPaintWindow::Free();
CMDIFrameWnd::OnClose();
}
Handle Save & Update UI for it
// TODO" comment line:
POSITION pos = GetFirstViewPosition();
CTutorialView* pView = (CTutorialView*) GetNextView(pos);
LBitmapBase* pLBtmpbase = pView->m_RasterPntWnd.GetBitmap();
BOOL bFlag = AfxGetMainWnd()->EnableWindow(FALSE);
SAVEDLGPARAMS SaveParms;
OPENFILENAME OpenFileName;
memset(&SaveParms, 0, sizeof(SAVEDLGPARAMS));
memset(&OpenFileName, 0, sizeof(OPENFILENAME));
OpenFileName.lStructSize = sizeof(OPENFILENAME);
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.lpstrTitle = _T("Save a File");
OpenFileName.nFilterIndex = 0;
SaveParms.uStructSize = sizeof(SAVEDLGPARAMS);
SaveParms.nQFactor = 2;
SaveParms.nPageNumber = 1;
SaveParms.nStampBits = 24;
SaveParms.nStampWidth = 120;
SaveParms.nStampHeight = 120;
SaveParms.nBitsPerPixel = 24;
SaveParms.nFormat = FILE_BMP;
SaveParms.uSaveMulti = MULTIPAGE_OPERATION_REPLACE;
SaveParms.uDlgFlags = DLG_SAVE_SHOW_FILEOPTIONS_PROGRESSIVE |
DLG_SAVE_SHOW_FILEOPTIONS_MULTIPAGE |
DLG_SAVE_SHOW_FILEOPTIONS_STAMP |
DLG_SAVE_SHOW_FILEOPTIONS_QFACTOR |
DLG_SAVE_SHOW_FILEOPTIONS_J2KOPTIONS |
DLG_SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS;
pLBtmpbase->DialogFile()->SetSaveParams(&SaveParms);
pLBtmpbase->DialogFile()->SetOpenFile(&OpenFileName);
pLBtmpbase->DialogFile()->EnableAutoProcess(TRUE);
L_INT nRetCode = pLBtmpbase->DialogFile()->DoModalSave(pView->m_hWnd);
if(nRetCode!=SUCCESS_DLG_OK && nRetCode!=SUCCESS_DLG_CANCEL)
pView->MessageBox(_T("Can't Save to file, check if file is read only."), _T("File Save Error"), MB_ICONWARNING | MB_OK);
bFlag = AfxGetMainWnd()->EnableWindow(TRUE);
AfxGetMainWnd()->SetActiveWindow();
// TODO" comment line
:
pCmdUI->Enable(m_RasterPntWnd.GetBitmap()->IsAllocated());
Handle Open & Update UI
Add the following lines of code after the // TODO
comment line:
OPENDLGPARAMS FOParm;
OPENFILENAME OpenFileName;
memset(&FOParm, 0, sizeof(OPENDLGPARAMS));
memset(&OpenFileName, 0, sizeof(OPENFILENAME));
OpenFileName.lStructSize = sizeof(OPENFILENAME);
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.Flags = OFN_EXPLORER;
FOParm.uStructSize = sizeof(OPENDLGPARAMS);
FOParm.uDlgFlags = DLG_OPEN_ENABLESIZING |
DLG_OPEN_SHOW_PROGRESSIVE |
DLG_OPEN_USEFILESTAMP |
DLG_OPEN_SHOW_MULTIPAGE |
DLG_OPEN_SHOW_LOADROTATED |
DLG_OPEN_SHOW_LOADCOMPRESSED |
DLG_OPEN_SHOW_DELPAGE |
DLG_OPEN_SHOW_LOADOPTIONS |
DLG_OPEN_SHOW_FILEINFO |
DLG_OPEN_SHOW_PDFOPTIONS |
DLG_OPEN_SHOW_RASTEROPTIONS |
DLG_OPEN_SHOW_VECTOROPTIONS |
DLG_OPEN_VIEWTOTALPAGES;
FOParm.bPreviewEnabled = TRUE;
m_LBitmap.DialogFile()->SetOpenParams(&FOParm);
m_LBitmap.DialogFile()->EnablePreview(TRUE);
m_LBitmap.DialogFile()->SetOpenParams(&OpenFileName);
m_LBitmap.DialogFile()->EnableCallBack(FALSE);
L_INT nRetCode = m_LBitmap.DialogFile()->DoModalOpen(m_pMainWnd->m_hWnd);
if(nRetCode==SUCCESS_DLG_OK)
{
L_TCHAR szFileName[256];
memset(szFileName,0,sizeof(szFileName));
L_UINT uSize=sizeof(szFileName);
m_LBitmap.GetFileName(szFileName,&uSize);
m_LBitmap.DialogFile()->GetOpenParams(&FOParm,sizeof(FOParm));
POSITION pos = GetFirstDocTemplatePosition();
CDocTemplate* pDocTemplate = GetNextDocTemplate(pos);
pDocTemplate->OpenDocumentFile(szFileName);
}
Click on the "Class View" tab.
public:
LBitmapBase m_LBitmap;
Replace return statement with the following code:
CTutorialApp* pTheApp = (CTutorialApp*) AfxGetApp();
POSITION pos = GetFirstViewPosition();
CTutorialView* pView = (CTutorialView*)GetNextView(pos);
FILEINFO fInfo;
OPENDLGPARAMS FOParm;
memset(&FOParm, 0, sizeof(OPENDLGPARAMS));
memset(&fInfo, 0, sizeof(FILEINFO));
fInfo.uStructSize = sizeof(FILEINFO);
LBitmapBase LBtmpbase;
pTheApp->m_LBitmap.DialogFile()->GetOpenParams(&FOParm, sizeof(OPENDLGPARAMS));
LBtmpbase.SetFileName((L_TCHAR *)lpszPathName);
LBtmpbase.File()->GetInfo(&fInfo, sizeof(FILEINFO), FILEINFO_TOTALPAGES, NULL);
L_INT nBitsPerPixel = fInfo.BitsPerPixel;
if(nBitsPerPixel != 1 && nBitsPerPixel != 4 && nBitsPerPixel != 8 && nBitsPerPixel != 16 && nBitsPerPixel != 24)
{
pView->MessageBox(_T("Your image format is not either 1,4,8,16 or 24 bits per pixel"), _T("Error Opening Image"), MB_ICONWARNING | MB_OK);
return FALSE;
}
L_INT nRetCode = LBtmpbase.Load(fInfo.BitsPerPixel, ORDER_BGRORGRAY, (FOParm.pFileData)? FOParm.pFileData[0].nPageNumber : 1);
if(nRetCode!=SUCCESS)
LBase::DisplayError(NULL, nRetCode);
else
pView->m_RasterPntWnd.SetBitmap(&LBtmpbase);
return((nRetCode == SUCCESS) ? TRUE : FALSE);
Open the "TutorialDoc.cpp" file from the "File View" tab.
#include "TutorialView.h"
Delete Save As menu Item
Delete New Menu Item
Delete Resent Files Menu Item
Handling Palette Changes
Add this define statement after the #include "resource.h" // main symbols
statement
#define WM_HANDLEPALETTE WM_APP + 0
Click on the "Class View" tab.
Add the following lines of code after the " void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd) line:
if(pFocusWnd!=NULL)
SendMessageToDescendants(WM_HANDLEPALETTE,(WPARAM)pFocusWnd ->m_hWnd, TRUE);
Right click "CMainFrame" and select "Properties".
Replace the "return CMDIFrameWnd::OnQueryNewPalette();" statement with the following lines of code:
CMDIChildWnd* pMDIChildWnd = MDIGetActive();
if(pMDIChildWnd!=NULL)
{
CView* pView = pMDIChildWnd->GetActiveView();
if(pView!=NULL)
{
pView->SendMessage(WM_HANDLEPALETTE,0,FALSE);
return TRUE;
}
}
return FALSE;
Right click "CMainFrame" and select "Properties".
Replace the "CMDIFrameWnd::OnSysColorChange();" statement with the following line of code which actually calls the OnQueryNewPalette() function:
OnQueryNewPalette();
Right click " CTutorialView" and select "Properties".
Write the following lines of code after the // TODO: Add your specialized code here and/or call the base class
statement:
if(bActivate==TRUE&&pActivateView==this)
m_RasterPntWnd.HandlePalette(WM_QUERYNEWPALETTE,0,0);
Open the "TutorialView.h" file from the "Soluation Explorer" tab and add the following line before the DECLARE_MESSAGE_MAP() statement:
afx_msg LRESULT OnHandlePalette(WPARAM wParam, LPARAM lParam);
ON_MESSAGE(WM_HANDLEPALETTE,OnHandlePalette)
LRESULT CTutorialView::OnHandlePalette(WPARAM wParam, LPARAM lParam)
{
if(lParam==TRUE)
{
if(m_RasterPntWnd.HandlePalette(WM_PALETTECHANGED, wParam, 0)==FALSE)
m_RasterPntWnd.Repaint();
}
else
{
if(m_RasterPntWnd.HandlePalette(WM_QUERYNEWPALETTE, 0, 0)==FALSE)
m_RasterPntWnd.Repaint();
}
return TRUE;
}
Show / Hide the Tool bar
Add the following line of code inside the generated function body:
LRasterPaintWindow::ShowToolbar(!m_LToolbar.IsVisible());
Right-click again on the "Toolbar" menu item and choose "Add Event Handler&"
pCmdUI->SetCheck(m_LToolbar.IsVisible());
Here we will be dealing with one of the paint dialogs, which is the "Brush" dialog:
Add the following line of code inside the generated function body:
CTutorialApp* pTheApp = (CTutorialApp*)AfxGetApp();
PAINTDLGBRUSHINFO BrushDlgInfo;
PAINTBRUSH* pPaintBrush = m_RasterPntWnd.GetPaintBrush();
L_INT nRetCode;
L_TCHAR* TouchBitmap [ ] = { TEXT("Leaf") };
L_TCHAR* TextureBitmap [ ] = { TEXT("Texture-00") };
BrushDlgInfo.dwFlags = PAINT_DLG_BRUSH_SHOWALL;
BrushDlgInfo.pszTitle = TEXT("Paintbrush Properties");
BrushDlgInfo.nContentsType = pPaintBrush->Touch.nContentsType;
BrushDlgInfo.crColor = pPaintBrush->Touch.crColor;
BrushDlgInfo.ppszTouchImage = TouchBitmap;
BrushDlgInfo.uTouchImageCount = 1;
BrushDlgInfo.nActiveTouchImageItem = 0;
BrushDlgInfo.nDiameter = pPaintBrush->nDiameter;
BrushDlgInfo.nHardnessValue = pPaintBrush->Hardness.nValue;
BrushDlgInfo.nSpacing = pPaintBrush->nSpacing;
BrushDlgInfo.nDensity = pPaintBrush->nDensity;
BrushDlgInfo.nOpacity = pPaintBrush->nOpacity;
BrushDlgInfo.nFadeOutRate = pPaintBrush->nFadeOutRate;
BrushDlgInfo.ppszPaperTexture = TextureBitmap;
BrushDlgInfo.uPaperTextureCount = 1;
BrushDlgInfo.nActivePaperTextureItem = ((pPaintBrush->pTexture != NULL) ? 0 : -1);
nRetCode = m_PaintDialog.DoModalBrush(m_hWnd, &BrushDlgInfo);
if(nRetCode == SUCCESS)
{
pPaintBrush->nSize = sizeof(PAINTBRUSH);
pPaintBrush->dwMask = PBF_ALL;
pPaintBrush->Touch.nContentsType = BrushDlgInfo.nContentsType;
pPaintBrush->Touch.crColor = BrushDlgInfo.crColor;
pPaintBrush->Touch.nShape = PAINT_TOUCH_SHAPE_CIRCLE;
pPaintBrush->Touch.pBitmap = ((BrushDlgInfo.nActiveTouchImageItem != -1) ? pTheApp->m_LBtmpBrush.GetHandle() : NULL);
pPaintBrush->Touch.crTransparentColor = RGB(0, 0, 0);
pPaintBrush->nDiameter = BrushDlgInfo.nDiameter;
pPaintBrush->Hardness.nDistributionType = PAINT_HARDNESS_DISTRB_TYPE_0;
pPaintBrush->Hardness.nValue = BrushDlgInfo.nHardnessValue;
pPaintBrush->nSpacing = BrushDlgInfo.nSpacing;
pPaintBrush->nDensity = BrushDlgInfo.nDensity;
pPaintBrush->nOpacity = BrushDlgInfo.nOpacity;
pPaintBrush->nFadeOutRate = BrushDlgInfo.nFadeOutRate;
pPaintBrush->pTexture = ((BrushDlgInfo.nActivePaperTextureItem != -1) ? pTheApp->m_LBtmpPaperTexture.GetHandle(): NULL);
m_RasterPntWnd.SetPaintBrush(pPaintBrush);
}
Click on the "Class View" tab.
Add the following public variables:
LBitmapBase m_LBtmpBrush;
LBitmapBase m_LBtmpPaperTexture;
Copy the bitmaps you want to use in the dialog to the "Res" folder in your current directory.
Add the following lines of code to the function body:
DIBSECTION dibsc;
BITMAPHANDLE TempBtmapHandle;
// load paper texture image.
HANDLE hBitmap = LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_PAPER_TEXTURE), IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
GetObject(hBitmap , sizeof(DIBSECTION), &dibsc);
m_LBtmpPaperTexture.SetHandle(&TempBtmapHandle);
m_LBtmpPaperTexture.ConvertFromDIB((LPBITMAPINFO) &dibsc.dsBmih, (L_UCHAR*)dibsc.dsBm.bmBits);
m_LBtmpPaperTexture.ChangeViewPerspective(TOP_LEFT);
DeleteObject((HBITMAP) hBitmap);
// load the brush image.
hBitmap = LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP_BRUSH), IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
GetObject(hBitmap , sizeof (DIBSECTION), &dibsc);
m_LBtmpBrush.SetHandle(&TempBtmapHandle);
m_LBtmpBrush.ConvertFromDIB((LPBITMAPINFO) &dibsc.dsBmih, (L_UCHAR *)dibsc.dsBm.bmBits);
m_LBtmpBrush.ChangeViewPerspective(TOP_LEFT);
DeleteObject((HBITMAP) hBitmap);
Right click on the "CTutorialApp" class and select Add Add Function.
m_LBtmpBrush.Free();
m_LBtmpPaperTexture.Free();
LoadImages();
FreeImages();