I had a quick look and play around the other night.
Perhaps this code will be of some use to you. I've used it to demonstrate (a) checking for the return value of a dialog, before deciding which action to take. (b) open multiple windows, each dialog may then open another dialog or be closed itself.
(c) a way of using DialogBoxParam to pass info to the dialog being created, such that it can be initialized in a particular way.
The main dialog's 3 buttons just open child dialogs or exit the program. The child dialog box I'e used has been designed to have a brownish background. You get one of these when you hit the Login button. This child dialog will then create another copy of itself if you hit OK, or will close itself if you hit close.
As for the 'Test' button, that opens up the same dialog, but passes it info such as the window title and the background colour. Hitting OK on this dialog will open up another of the 'default' dialog boxes - that is to say, the new one will have a brown background.
You can easily (a) convert the images to BMP format and use LoadImage or (b) use GDI+ to open jpg/png images by filling a Gdiplus::Bitmap object from a disk file. You can use the following function to load anything natively supported by windows. Just don't forget to initialize Gdi+ before you call it!
HBITMAP mLoadImageFile(wchar_t *filename)
{
HBITMAP result = NULL;
Bitmap bitmap(filename, false);
bitmap.GetHBITMAP(0, &result);
return result;
}
Anyway, that's enough chatter. Here's the code. If you've any questions, feel free to ask.
main.cpp
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
HINSTANCE hInst;
typedef struct DLGDATA
{
wchar_t *windowText;
COLORREF bkgCol;
} *pDlGDATA;
BOOL CALLBACK LoginDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
HBRUSH bkBrush;
pDlGDATA refData;
refData = (pDlGDATA)lParam;
if (refData != NULL)
{
bkBrush = CreateSolidBrush( refData->bkgCol );
SetProp(hwndDlg, "bkBrush", (HANDLE)bkBrush);
SetWindowTextW(hwndDlg, refData->windowText );
}
else
{
bkBrush = CreateSolidBrush( RGB(137, 48, 14) );
SetProp(hwndDlg, "bkBrush", (HANDLE)bkBrush);
}
}
return TRUE;
case WM_NCDESTROY:
{
HBRUSH bkBrush;
bkBrush = (HBRUSH) GetProp(hwndDlg, "bkBrush");
if (bkBrush)
{
DeleteObject(bkBrush);
RemoveProp(hwndDlg, "bkBrush");
}
}
return 0;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_ERASEBKGND:
{
HBRUSH mBrush;
RECT clientRect;
GetClientRect(hwndDlg, &clientRect);
mBrush = (HBRUSH) GetProp(hwndDlg, "bkBrush");
if (mBrush)
FillRect( (HDC) wParam, &clientRect, mBrush);
else
FillRect( (HDC) wParam, &clientRect, (HBRUSH)GetStockObject(WHITE_BRUSH));
}
return 1;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDOK:
ShowWindow(hwndDlg, SW_HIDE);
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)LoginDlgProc);
ShowWindow(hwndDlg, SW_SHOW);
break;
case IDCANCEL:
EndDialog(hwndDlg,-2);
break;
}
}
return TRUE;
}
return FALSE;
}
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
ShowWindow(hwndDlg, SW_HIDE);
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)LoginDlgProc);
ShowWindow(hwndDlg, SW_SHOW);
break;
case IDC_BUTTON2:
EndDialog(hwndDlg,-2);
break;
case IDC_BUTTON3:
{
ShowWindow(hwndDlg, SW_HIDE);
DLGDATA mData;
mData.bkgCol = RGB(50,200,60);
mData.windowText = L"lParam-passed data modified dialog";
DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwndDlg, (DLGPROC)LoginDlgProc, (LPARAM)&mData );
ShowWindow(hwndDlg, SW_SHOW);
}
break;
}
}
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}
resource.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#define DLG_MAIN 100
#define IDD_DIALOG1 101
#define IDC_BUTTON1 1000
#define IDC_BUTTON2 1001
#define IDC_BUTTON3 1002
resource.rc
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 277, 230
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Prison Management System"
FONT 8, "Ms Shell Dlg"
{
CONTROL "", IDC_STATIC, WC_STATIC, SS_BLACKFRAME, 53, 51, 171, 119
PUSHBUTTON "Login", IDC_BUTTON1, 53, 189, 50, 14
PUSHBUTTON "Exit", IDC_BUTTON2, 174, 189, 50, 14
LTEXT "A computerized prison management system", IDC_STATIC, 67, 27, 142, 8, SS_LEFT
PUSHBUTTON "Test", IDC_BUTTON3, 113, 193, 50, 14
}
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 186, 95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
DEFPUSHBUTTON "OK", IDOK, 129, 7, 50, 14
PUSHBUTTON "Cancel", IDCANCEL, 129, 24, 50, 14
}
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1 RT_MANIFEST ".\\manifest.xml"