Code:
// ConDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "commdlg.h"
#include "richedit.h"
#include "resource1.h"
#include "resource.h"
// Function Prototypes
BOOL WINAPI MainDlgProc( HWND, UINT, WPARAM, LPARAM );
BOOL SaveEditAsStream( HWND );
DWORD CALLBACK RTFSaveStreamCallback( DWORD, LPBYTE, LONG, LONG * );
BOOL LoadEditAsStream( HWND );
DWORD CALLBACK RTFLoadStreamCallback( DWORD, LPBYTE, LONG, LONG * );
BOOL GetNewTextColor( HWND );
void CreateMenubar( HWND );
void CloseTheFile(HWND);
// Global Variables
HINSTANCE hInst; // Program instance handle
HANDLE hRichEdit; // RICHED32.DLL handle
DWORD dwTextColor; // Current text color
char *initialText = "This editor will allow you to view tests that you have created. The following \n"
"options are available by clicking “File” from the menu:\n\n"
"\tTo open a test, select the “Open” option from the menu.\n\n"
"\tIf you change the results of a test, you can save the changes by selecting \n\t\tthe “Save” option from the menu.\n\n"
"\tYou may also close the file and discard any changes by selecting “Close” from the menu.\n\n"
"\tTo return to the main program, select “Exit” from the menu.\n";
char currentFileName;
// Program Entry Points
BOOL APIENTRY DllMain( HINSTANCE hInstance/*HANDLE hModule*/,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
hInst = hInstance; // Save program instance in global
MessageBox( NULL, initialText, "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
// Call main dialog box
/**/ CreateDialog( NULL, MAKEINTRESOURCE( MAIN_DLG ), NULL, MainDlgProc );
// This is the line that should launch the main dialog window
MessageBox( NULL, "After call for MainDlgProc", "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
return( TRUE );
}
// Main Dialog Proc
BOOL WINAPI MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
static HICON hIcon;
static HICON hIconSm;
MessageBox( NULL, "We're in MainDlgProc", "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
switch( msg )
{
case WM_INITDIALOG:
{
CHARFORMAT cf;
CreateMenubar(hDlg);
if( GetDlgItem( hDlg, RICHTEXT_EDIT ) ) // Sanity Check
{
dwTextColor = 0x00000000;
memset( &cf, 0, sizeof(CHARFORMAT) ); // Initialize structure
cf.cbSize = sizeof(CHARFORMAT); // Initialize RichEdit
cf.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE; // control structure
cf.crTextColor = dwTextColor;
cf.yHeight = 32;
strcpy( cf.szFaceName, "Courier" );
// Set the icon
hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, 0);
hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, 0);
// Set character formatting and background color
SendDlgItemMessage( hDlg, RICHTEXT_EDIT, EM_SETCHARFORMAT, 4, (LPARAM)&cf );
SendDlgItemMessage( hDlg, RICHTEXT_EDIT, EM_SETBKGNDCOLOR, TRUE, 0 );
SendMessage(hDlg, WM_SETICON, IDI_ICON1 , (LPARAM)hIcon);
SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
break;
}
return( TRUE ); // End of dialog initialization code
}
case WM_COMMAND:
if( wParam == /*ID_FILE_CLOSE*/ 102 ) // "Color" button pressed
{
CloseTheFile(hDlg);
SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) ); // Back to edit control
break;
}
if( wParam == /*ID_FILE_SAVE*/ 103) // "Save" button pressed
{
SaveEditAsStream( hDlg ); // Save rich edit contents to file
SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) ); // Back to edit control
break;
}
if( wParam == /*ID_FILE_OPEN*/ 101) // "Load" button pressed
{
LoadEditAsStream( hDlg ); // Load RTF file into richedit
SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) ); // Back to edit control
break;
}
if( wParam == 50001 )
{
GetNewTextColor( hDlg );
SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) ); // Back to edit control
break;
}
if( wParam == ID_FILE_EXIT ) // Close dialog if Esc pressed
{
EndDialog( hDlg, TRUE );
return( TRUE );
}
break;
case WM_DESTROY:
DestroyIcon(hIcon);
DestroyIcon(hIconSm);
PostQuitMessage(0);
break;
}
return( FALSE );
} // End of MainDlgProc
// ==========================================================
// Set up menu
void CreateMenubar(HWND hwnd) { // Set up the menu
HMENU hMenubar;
HMENU hMenu, cMenu;
MessageBox( NULL, "CreateMenubar", "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
hMenubar = CreateMenu();
hMenu = CreateMenu();
cMenu = CreateMenu();
// Menu Options -------------------------------------------
// File Options
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, TEXT("&File"));
AppendMenu(hMenu, MF_STRING, /*ID_FILE_OPEN*/ 101, TEXT("&Open"));
AppendMenu(hMenu, MF_STRING, /*ID_FILE_CLOSE*/ 102, TEXT("&Close"));
AppendMenu(hMenu, MF_STRING, /*ID_FILE_SAVE*/ 103, TEXT("&Save&"));
AppendMenu(hMenu, MF_STRING, ID_FILE_EXIT, TEXT("E&xit"));
// Settings Options
//AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)cMenu, TEXT("Se&ttings"));
//AppendMenu(cMenu, MF_STRING, ID_SETTINGS_FONT, TEXT("Font Co&lor"));
//AppendMenu(cMenu, MF_STRING, 50002, TEXT("&Background Color"));
SetMenu(hwnd, hMenubar);
}
// ==============================================================
The main progam only loads and unloads the dll: