Date: 2014aug28
OS: Windows
Keywords: dialog, based
Q. How can I make a win32/win64 application that has a dialog as the main
window?
A. You can NOT just do:
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
// WRONG WRONG WRONG!
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), 0, DialogProc);
}
You need to do this:
#include "stdafx.h"
#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>
#include "resource.h"
#pragma comment(linker, \
"\"/manifestdependency:type='Win32' "\
"name='Microsoft.Windows.Common-Controls' "\
"version='6.0.0.0' "\
"processorArchitecture='*' "\
"publicKeyToken='6595b64144ccf1df' "\
"language='*'\"")
#pragma comment(lib, "ComCtl32.lib")
static HINSTANCE ghInstance = NULL;
static void SetIcons(HWND hDlg)
{
const HICON hIconSmall = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_SMALL));
SendMessage(hDlg, WM_SETICON, WPARAM(ICON_SMALL), LPARAM(hIconSmall));
const HICON hIconBig = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_MYAPP));
SendMessage(hDlg, WM_SETICON, WPARAM(ICON_BIG), LPARAM(hIconBig));
}
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
SetIcons(hDlg);
return TRUE;
case WM_CLOSE:
DestroyWindow(hDlg);
return TRUE;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDCANCEL:
SendMessage(hDlg, WM_CLOSE, 0, 0);
return TRUE;
}
break;
}
return FALSE;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HWND hDlg;
MSG msg;
BOOL ret;
ghInstance = hInstance;
InitCommonControls();
hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAIN), 0, DialogProc, 0);
ShowWindow(hDlg, nCmdShow);
while ((ret = GetMessage(&msg, 0, 0, 0)) != 0)
{
if (ret == -1)
{
return -1;
}
if (!IsDialogMessage(hDlg, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}