Category : C Source Code
Archive   : ICOPIK.ZIP
Filename : ICOPIK.CPP

 
Output of file : ICOPIK.CPP contained in archive : ICOPIK.ZIP
//icopik.cpp

#include
#include
#include
#include
#include
#include
#include
#include

#include "icopik.h"
#include "icopik.hpp"


// TIconListBox //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

TIconListBox::TIconListBox(PTWindowsObject AParent, int ResourceId,
PTModule AModule)
:TListBox(AParent, ResourceId, AModule)
{
Attr.Style &= ~LBS_SORT;
Attr.Style &= ~LBS_HASSTRINGS;
Attr.Style |= LBS_OWNERDRAWFIXED;
Attr.Style |= LBS_MULTICOLUMN;
Attr.Style |= LBS_DISABLENOSCROLL;

// have to calculate height to match height of listbox area from dialog
iIconHeight = 0;
// add pixels for each side to make room for focus rect and highlight
iIconWidth = GetSystemMetrics(SM_CXICON)+(ICONBORDER*2);
};


// TIconDialog ///////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

TIconDialog::TIconDialog(PTWindowsObject AParent, int ResID,
LPSTR szIconsFileName, int &iCurIcon) // not &
:TDialog(AParent, ResID)
{
PTListBox_Icon = new TIconListBox(this, ID_ICONLIST);
PTEdit_IconFileName = new TEdit(this, ID_ICONFILE, MAXFILENAME+1);
PTBButton_Find = new TBButton(this, ID_BTN_FIND);

iSelectedIcon = &iCurIcon;
szIconSourceFile = szIconsFileName;
HIGHLIGHTCOLOR = GetSysColor(COLOR_HIGHLIGHT);
WINDOWCOLOR = GetSysColor(COLOR_WINDOW);
hWindowBrush = CreateSolidBrush(WINDOWCOLOR);
hHighlightBrush = CreateSolidBrush(HIGHLIGHTCOLOR);
};


LPSTR TIconDialog::GetClassName()
{
return "BorDlg_ChgIcon";
}


void TIconDialog::GetWindowClass(WNDCLASS& AWndClass)
{
TDialog::GetWindowClass(AWndClass);

AWndClass.lpfnWndProc = BWCCDefDlgProc;
AWndClass.lpszClassName = "BorDlg_ChgIcon";
AWndClass.cbWndExtra = DLGWINDOWEXTRA;
}


void TIconDialog::SetupWindow()
{
int nError = 0;

TDialog::SetupWindow();

// Assign bitmaps for Find button
// other buttons are Borland standard so we don't have to do this
HINSTANCE hInstance = GetApplication()->hInstance;
HWND hWndButton = GetDlgItem(HWindow, ID_BTN_FIND);

hFindBTNBits[0] = LoadBitmap(hInstance, MAKEINTRESOURCE(ID_FindBTNNormalBMP));
hFindBTNBits[1] = LoadBitmap(hInstance, MAKEINTRESOURCE(ID_FindBTNPressedBMP));
hFindBTNBits[2] = LoadBitmap(hInstance, MAKEINTRESOURCE(ID_FindBTNFocusBMP));

SendMessage(hWndButton, BBM_SETBITS, 0, (LONG) (LPSTR) hFindBTNBits);

PTEdit_IconFileName->SetText((LPSTR)szIconSourceFile);

// put icons into listbox
nError = FillIconList();

if (!nError)
{
// the "current" icon is sent in, so set it.
PTListBox_Icon->SetSelIndex(*iSelectedIcon);

// setup stuff we'll need later.
HDC hDC = GetDC(HWindow);
hMemDC = CreateCompatibleDC(hDC);
hMyBMP = CreateCompatibleBitmap (hDC, PTListBox_Icon->Width(),
PTListBox_Icon->Height());
hOldBMP = (HBITMAP) SelectObject(hMemDC, hMyBMP);
ReleaseDC(HWindow, hDC);
}
else
{
MessageBox(NULL, "Error loading icons to List box.", "ERROR!", MB_OK);
}
}


TIconDialog::~TIconDialog()
{
// release icon memory!
ClearIconList();

// get rid of button bitmaps
DeleteObject(hFindBTNBits[0]);
DeleteObject(hFindBTNBits[1]);
DeleteObject(hFindBTNBits[2]);

DeleteObject(hWindowBrush);
DeleteObject(hHighlightBrush);

// select in old bitmap before killing our BMP and the DC
SelectObject(hMemDC, hOldBMP);
DeleteObject(hMyBMP);
DeleteDC(hMemDC);

// kill interface elements
delete PTBButton_Find;
delete PTEdit_IconFileName;
delete PTListBox_Icon;
}


BOOL TIconDialog::CanClose()
{
if ( PTEdit_IconFileName->IsModified() )
{
char szNewFileName[MAXFILENAME];

PTEdit_IconFileName->GetText(szNewFileName, MAXFILENAME+1);

if (ResetIconList(szNewFileName))
{
// clear the IsModified flag by resetting text
PTEdit_IconFileName->SetText(szIconSourceFile);
}
else
{
MessageBox(NULL, "Undoing name change...", "File selection error", MB_OK);
PTEdit_IconFileName->SetText(szIconSourceFile);
}
return FALSE;
}

*iSelectedIcon = PTListBox_Icon->GetSelIndex();
return TRUE;
}


void TIconDialog::WMMeasureItem( RTMessage Msg)
{
MEASUREITEMSTRUCT *ms;

ms = (MEASUREITEMSTRUCT *)Msg.LParam;
if (ms->CtlID == ID_ICONLIST)
{
ms->itemWidth = PTListBox_Icon->Width();
if ( !PTListBox_Icon->Height() ) // Calculate only once!
{
RECT rect;
int height;

GetWindowRect ( GetDlgItem(HWindow, ID_ICONLIST), &rect);
height = rect.bottom - rect.top;
PTListBox_Icon->Height( height );
}
ms->itemHeight = PTListBox_Icon->Height();
}
}


// returns 1 if file contains icons, 0 if there is a problem
int TIconDialog::IconsInFile(LPSTR szTestFile)
{
HICON hIcon;

hIcon = ExtractIcon(GetApplication()->hInstance, szTestFile, 0);

if ( (int) hIcon == 1)
{
MessageBox(NULL, "Not an appropriate file type.", "ERROR!", MB_OK);
return 0;
}
else if ((int) hIcon == NULL)
{
MessageBox(NULL, "No Icons in file!", "ERROR!", MB_OK);
return 0;
}
DeleteObject(hIcon);
return 1;
}


// returns 0 if all is well, 1 on error
int TIconDialog::FillIconList()
{
HICON hIcon;
HINSTANCE hInstance = GetApplication()->hInstance;
LRESULT lOK;

// don't really need to test here, since FindFiles tests before calling
// and SetupWindow should be using a file we know to be okay, but....
if (IconsInFile(szIconSourceFile))
{
cIconsInFile = (int) ExtractIcon(hInstance, szIconSourceFile, (UINT) -1);

for (UINT i=0; i {
hIcon = ExtractIcon(hInstance, szIconSourceFile, i);

lOK = SendDlgItemMsg(ID_ICONLIST, LB_ADDSTRING, 0, MAKELONG(hIcon, 0));
if (lOK==LB_ERR || lOK==LB_ERRSPACE)
{
MessageBox(NULL, "LB_ADDSTRING Error!", "ERROR!", MB_OK);
return 1;
}
}

// clear any selection
PTListBox_Icon->SetSelIndex(-1);

return 0;
}
else return 1;
}


void TIconDialog::ClearIconList()
{
HICON hIcon;

for (int i=0; i {
hIcon = (HICON) LOWORD(SendDlgItemMsg(ID_ICONLIST, LB_GETITEMDATA, i, 0L));
DestroyIcon(hIcon);
}

PTListBox_Icon->ClearList();
}


// returns 1 if all goes well, 0 otherwise
int TIconDialog::ResetIconList(LPSTR szNewFileName)
{
int iRC;

// if file name did not change, get out
iRC = lstrcmpi(szNewFileName, szIconSourceFile);
if (iRC == 0) return 1;

if (IconsInFile(szNewFileName))
{
HCURSOR hCurSave = SetCursor(LoadCursor(NULL, IDC_WAIT));
// set new object data values
lstrcpy(szIconSourceFile, szNewFileName);
*iSelectedIcon = -1;

// clear and refill list
ClearIconList();
// not sure what could go wrong, but anything could happen!
if ( FillIconList() )
{
SetCursor(hCurSave);
return 0;
}

// set edit control text and listbox selection
PTEdit_IconFileName->SetText((LPSTR)szIconSourceFile);
PTListBox_Icon->SetSelIndex(*iSelectedIcon);
// return cursor shape
SetCursor(hCurSave);
return 1;
}
else return 0;
}


void TIconDialog::WMDrawItem(RTMessage Msg)
{

LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)Msg.LParam;
HICON hIcon;

// Since we get a WM_DRAWITEM message for every owner-draw control,
// we want to handle only the ID_ICONLIST control.
// (not really a problem for this sample program...)

if (lpdis->CtlID == ID_ICONLIST)
{
DWORD dwBkColor, dwOldBkColor;
HBRUSH hOldBrush, hBackBrush;

if (lpdis->itemID == (UINT) -1)
{
DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
return;
}

if (lpdis->itemAction & ODA_FOCUS)
{
DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
return;
}

if (lpdis->itemState & ODA_SELECT)
{
DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
return;
}


if (lpdis->itemState & ODS_SELECTED)
{
dwBkColor = HIGHLIGHTCOLOR;
hBackBrush = hHighlightBrush;
}
else
{
dwBkColor = WINDOWCOLOR;
hBackBrush = hWindowBrush;
}

dwOldBkColor = SetBkColor (hMemDC, dwBkColor);
hOldBrush = (HBRUSH) SelectObject (hMemDC, hBackBrush);

PatBlt (hMemDC, 0,0,
lpdis->rcItem.right - lpdis->rcItem.left,
lpdis->rcItem.bottom - lpdis->rcItem.top,
PATCOPY);

hIcon = (HICON) LOWORD(lpdis->itemData);

//Draw the icon onto the MemoryDC Assume (left,top) coordinates are (0,0)
DrawIcon(hMemDC, ICONBORDER, ICONBORDER, hIcon);

BitBlt (lpdis->hDC,lpdis->rcItem.left, lpdis->rcItem.top,
lpdis->rcItem.right - lpdis->rcItem.left,
lpdis->rcItem.bottom - lpdis->rcItem.top,
hMemDC, 0,0, SRCCOPY);

SetBkColor (hMemDC, dwOldBkColor); // Reset background color
SelectObject (hMemDC, hOldBrush);

if (lpdis->itemState & ODS_FOCUS)
DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
}
}


void TIconDialog::HelpUser(RTMessage)
{
TBorDlgAbout *PAbout;

// temporarily move window to side so it AND Help window are visible
RECT tagRectDlgCoords;

GetWindowRect(this->HWindow, &tagRectDlgCoords);
int iDlgWidth = tagRectDlgCoords.right - tagRectDlgCoords.left;
int iDlgHeight = tagRectDlgCoords.bottom - tagRectDlgCoords.top;

SetWindowPos(this->HWindow, NULL, (int) 10, (int) 10,
iDlgWidth, iDlgHeight, SWP_NOSIZE);

// Do Help Dialog
PAbout = new TBorDlgAbout(this, HELPDLG);
GetApplication()->ExecDialog(PAbout);

// restore Dialer window to previous position
SetWindowPos(this->HWindow, NULL, tagRectDlgCoords.left,
tagRectDlgCoords.top, iDlgWidth, iDlgHeight, SWP_NOSIZE);
}


void TIconDialog::FindFiles(RTMessage)
{
char szNewFileName [MAXFILENAME];
TFileDialog * ptFileOpen;
int iFileRet;

lstrcpy(szNewFileName, "*.exe");
ptFileOpen = new TFileDialog(this, SD_FILEOPEN, szNewFileName, NULL);
iFileRet = GetApplication()->ExecDialog(ptFileOpen);

if (iFileRet == IDOK) ResetIconList(szNewFileName);
}


void TIconDialog::HandleListBoxMsg(RTMessage Msg)
{

if ( Msg.LP.Hi == LBN_DBLCLK )
{
// IDOK forces dialog box return to be IDOK for double click
CloseWindow(IDOK);
}
}


// TBorDlgAbout //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

LPSTR TBorDlgAbout::GetClassName()
{
return "BorDlg_About";
}


void TBorDlgAbout::GetWindowClass(WNDCLASS& AWndClass)
{
TDialog::GetWindowClass(AWndClass);

AWndClass.lpfnWndProc = BWCCDefDlgProc;
AWndClass.lpszClassName = "BorDlg_About";
AWndClass.cbWndExtra = DLGWINDOWEXTRA;
}


void TBorDlgAbout::SetupWindow()
{
RECT tagRectDlgCoords;

// Call the default
TDialog::SetupWindow();

// Center Window on Screen
int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

GetWindowRect(this->HWindow, &tagRectDlgCoords);
int iDlgWidth = tagRectDlgCoords.right - tagRectDlgCoords.left;
int iDlgHeight = tagRectDlgCoords.bottom - tagRectDlgCoords.top;

int iULX = (iScreenWidth - iDlgWidth)/2;
int iULY = (iScreenHeight - iDlgHeight)/2;

SetWindowPos(this->HWindow, NULL, iULX, iULY, iDlgWidth, iDlgHeight, SWP_NOSIZE);
}


// TTestWindow ///////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

TTestWindow::TTestWindow(PTWindowsObject AParent, LPSTR ATitle)
: TWindow(AParent, ATitle)
{
AssignMenu( TESTMENU );
}


void TTestWindow::SetupWindow()
{
// yeah, I know this is cheating, but it's only a test driver!
SetWindowPos(HWindow, NULL, 20, 20, 450, 300, SWP_NOZORDER);
}


void TTestWindow::CMTest(RTMessage)
{
char szIconFile[MAXFILENAME] = "c:\\icopik\\icopik.exe";
int iDlgRet, iIconPos = 6;
char szMsgString[150];

iDlgRet = GetApplication()->ExecDialog(new TIconDialog(this, ICONDLG,
szIconFile, iIconPos));

if (iDlgRet == IDOK)
{
wsprintf(szMsgString, "File: %s\nPosition: %i", szIconFile, iIconPos);
MessageBox(NULL, szMsgString, "And the winner is...", MB_OK);
}
else MessageBox(NULL, "No changes made!", "You cancelled", MB_OK);
}


// TTestApp //////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

void TTestApp::InitMainWindow()
{
MainWindow = new TTestWindow(NULL, Name);
BWCCGetVersion();
}


int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
TTestApp TestApp("Icon ListBox Tester", hInstance, hPrevInstance,
lpCmdLine, nCmdShow);
TestApp.Run();
return (TestApp.Status);
}


  3 Responses to “Category : C Source Code
Archive   : ICOPIK.ZIP
Filename : ICOPIK.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/