My resource.h file:

#define IDR_MYMENU 101
#define IDI_MYICON 201
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_ABOUT 9003
#define IDD_TOOLBAR 9004
#define IDC_PRESS 9005
#define IDC_OTHER 9006
#define ID_DIALOG_SHOW 9007
#define ID_DIALOG_HIDE 9008
#define ID_HELP_ABOUT 9009
#define IDD_ABOUT 9010

--------------------------------------------------------------------------------------
My resource.rc file:

#include "windows.h" 
#include "resource.h"
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "Sample Dialog"
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "&Ok",IDC_PRESS,7,7,84,14
PUSHBUTTON "&Cancel",IDC_OTHER,7,31,84,14
END
/* Sample menu */
IDR_MYMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Stuff"
BEGIN
MENUITEM "&Show", ID_DIALOG_SHOW
MENUITEM "&Hide", ID_DIALOG_HIDE
END
END

------------------------------------------------------------------------------------------------
My windows.c file:

#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
HWND g_hToolbar = NULL;
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
 
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
 
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"The title of my window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,240,120,NULL,NULL,hInstance,NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
if(!IsDialogMessage(g_hToolbar, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
 
}
return Msg.wParam;
}
 
 
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRESS:
MessageBox(hwnd, "Hi!", "This is a message", 
MB_OK | MB_ICONEXCLAMATION);
break;
case IDC_OTHER:
MessageBox(hwnd, "Bye!", "This is also a message", 
MB_OK | MB_ICONEXCLAMATION);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
 
 
 
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 
switch(msg)
{
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
 
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_DIALOG_SHOW, "&Show");
AppendMenu(hSubMenu, MF_STRING, ID_DIALOG_HIDE, "&Hide");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Operations");
SetMenu(hwnd, hMenu);
/*hIcon = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
else
MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);
hIconSm = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
else
MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);*/
 
g_hToolbar=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),
hwnd, ToolDlgProc);
 
if(g_hToolbar != NULL)
{ 
ShowWindow(g_hToolbar, SW_SHOW);
}
else
{
MessageBox(hwnd,"CreateDialog returned NULL","Warning!",MB_OK | MB_ICONINFORMATION);
} 
}
break;
 
case WM_COMMAND:
switch(LOWORD(wParam))
{ 
 
case ID_FILE_EXIT:
break;
 
case ID_DIALOG_SHOW:
MessageBox(hwnd, "Bye!", "This is also a message", 
MB_OK | MB_ICONEXCLAMATION);
ShowWindow(g_hToolbar, SW_SHOW);
break;
case ID_DIALOG_HIDE:
ShowWindow(g_hToolbar, SW_HIDE);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd); 
break;
 
case WM_DESTROY:
DestroyWindow(g_hToolbar);
PostQuitMessage(0);
break;
 
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

---------------------------------------------------------------------------------------------------------------

The above code compiled successfully.. but i got error that "Create dialog returned null"..
can any one tell where exactly the problem lies...
then how to display dialog box???

Thanks in advance..

please edit your post to use code tags -- all that unformatted code hurts my eyes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.