is this bug or what ?

i wanted to make program that looks something like this
http://www.winprog.org/tutorial/images/dlg_two.gif

modeless dialog doesnt work
it is shown it has button, but if i click on it my main window starts to chnage it's positions and nth with toolbar happens
(it fails to activate)


.rc file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "resource.h"

#include "windows.h"

IDM_MYMENU MENU
BEGIN
	POPUP "File"
	BEGIN
		MENUITEM "Exit", ID_MYEXIT
	END

	POPUP "Info"
	BEGIN
		MENUITEM "Show Window", ID_SHOW
		MENUITEM "Hide Window", ID_HIDE
		MENUITEM "Grayed button", ID_GRAY , grayed
	END
END

IDR_TOOLBAR DIALOGEX 0,0, 200, 200
STYLE  DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "toolbar"
FONT 6, "arial"
BEGIN
	PUSHBUTTON "Press",ID_BTHI,25,25, 40,40
END


main file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "resource.h"
#include <windows.h>


char classname[8]="leClass";
HWND toolbar;

INT_PTR CALLBACK ToolProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
	switch (msg){
		case WM_COMMAND:
			switch (LOWORD (wParam)){
				case ID_BTHI :
					MessageBox (NULL, "Hello :)","Greetings", MB_OK | MB_ICONINFORMATION);
					break;
			}
			break;
		default :
			return FALSE;
	}
	return TRUE;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
	switch (msg){
	case WM_CREATE :{
		toolbar = CreateDialog (GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_TOOLBAR), hwnd, ToolProc);
		if (toolbar != NULL){
			ShowWindow (toolbar, SW_SHOW);
		}
		else
			MessageBox (NULL, "Failed to make toolbar", "Error", MB_OK| MB_ICONWARNING);
		}
		break;
	case WM_COMMAND :
		switch (LOWORD(wParam)) {
			case ID_SHOW:
				ShowWindow (toolbar, SW_SHOW);
				break;
			case ID_HIDE: 
				ShowWindow (toolbar,SW_HIDE);
				break;
			case ID_MYEXIT :
				PostMessage(hwnd, WM_CLOSE, 0,0);
				break;
		}
                break;
	case WM_CLOSE :
		DestroyWindow (hwnd);
		break;
	case WM_DESTROY :
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc (hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR size, int nCmdShow){
	HWND hwnd;
	MSG msg;
	WNDCLASSEX wc;

	wc.cbSize = sizeof (WNDCLASSEX);
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
	wc.hCursor = LoadCursor (NULL,IDC_ARROW);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	wc.hIconSm =  LoadIcon (NULL,IDI_APPLICATION);
	wc.lpszClassName = classname;
	wc.style =0;
	wc.lpszMenuName = MAKEINTRESOURCE (IDM_MYMENU);

	if(!RegisterClassEx(&wc)){
		MessageBox(NULL, "Failed to make window", "Error", MB_OK|MB_ICONWARNING);
	}

	hwnd =CreateWindowEx (WS_EX_CLIENTEDGE, classname, "le window", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, 400,500, NULL,NULL,hInstance,NULL);

	if (hwnd ==  NULL){
		MessageBox (hwnd, "Failed to make window", "Error", MB_OK|MB_ICONWARNING);
	}
	ShowWindow (hwnd, nCmdShow);
	UpdateWindow (hwnd);

	while (GetMessage(&msg,hwnd,0,0)>0){
		if (!IsDialogMessage (toolbar, &msg)){
		TranslateMessage (&msg);
		DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}


resource file
1
2
3
4
5
6
7
8
9
#define IDM_MYMENU 1001
#define ID_GRAY 1002
#define ID_MYEXIT 1053
#define ID_SHOW 1004
#define ID_HIDE 1005
#define IDR_TOOLBAR 1006
#define ID_BTHI 1007
#define ID_BTEXIT 1008
Last edited on
If CreateDialog() is failing, call GetLastError() to know why.

Also note that the tutorial is quite old, and now the Dialog Procedure has a different signature. Instead of BOOL, it returns INT_PTR. And yes, you still return TRUE or FALSE, but the signature changed.

Finally, note that the tutorial assumes an ANSI build. But even then, it uses function names and strings incorrectly. If you use function names ending with -A such as MessageBoxA() you need to use ANSI strings (you are using ANSI striings); if you use function names ending with -W such as MessageBoxW() you must use wide strings (L-prepended string literals, for example). Finally if you use function names that don't end in either such as MessageBox() (your case), you MUST enclose string literals with TEXT().
<my answer was missing the point>
Last edited on
i used this function (googled about getlasterror since i first time see this function, and yeah i am beginner)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void ReportLastError()
{
  LPCTSTR pszCaption = TEXT("Windows SDK Error Report");
  DWORD dwError      = GetLastError();

  if(NOERROR == dwError)
  {
    MessageBox(NULL, TEXT("No error"), pszCaption, MB_OK);
  }
  else
  {
    const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                  FORMAT_MESSAGE_IGNORE_INSERTS |
                                  FORMAT_MESSAGE_FROM_SYSTEM;

    LPVOID pTextBuffer = NULL;
    DWORD dwCount = FormatMessage(dwFormatControl, 
                                  NULL, 
                                  dwError, 
                                  0, 
                                  (LPTSTR) &pTextBuffer, 
                                  0, 
                                  NULL);
    if(0 != dwCount)
    {
      MessageBox(NULL, (LPCSTR)pTextBuffer, pszCaption, MB_OK|MB_ICONERROR);
      LocalFree(pTextBuffer);
    }
    else
    {
      MessageBox(NULL, TEXT("Unknown error"), pszCaption, MB_OK|MB_ICONERROR);
    }
  }
}


it throws me this error:
"the specified resource name cannot be found in the image file"
btw why i can't assign id's in rc file (just like i showed u in original post)
its like resource.h is not in included....

wut to do ...
Last edited on
Maybe you didn't include your RC file in your project. Because a RC file also needs to get compiled. Most times just adding it to the project makes it compile.
its in the project
Use a resource editor such as http://www.resedit.net/ to make sure your executable is indeed containing the required resources. If not, troubleshoot why your resource file isn't being compiled. I guess the simplest would be to start a new project making sure you perform all steps correctly.
IDE/OS?
Is your resource file in the "Resource files" section?
lol
i forgot to put rc in same folder with .cpp file
(i dont mean it project, i mean on projects location on desktop)

it works now, menu is created, toolbar is created, but still i can do nth with toolbar
not even move it, lol
mistakes?
code is same
btw i added this design info and it still doesn't work...
1
2
3
4
5
6
7
8
9
10
GUIDELINES DESIGNINFO DISCARDABLE 
BEGIN
    IDR_TOOLBAR, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 91
        TOPMARGIN, 7
        BOTTOMMARGIN, 45
    END
END


any help?
Your dialog procedure incorrectly returns TRUE for messages it is not handling itself.
^what do you mean?
after break; it goes to return TRUE
Last edited on
Exactly, it should return FALSE.
i tried switching TRUE and FALSE and it didnt help
it jsut made it worse
instead of toolbar with borders appears white window and still its not possible to move it or do sth with it

so can u give me at least link for tutorial which shows how to create modeless dialog in win 32 API that WORKS?
i found several and none of them work
Last edited on
lol
is that so hard to create win32 API modeless dialog? ..............
simply whatever i try, it doesnt work
Ok, now I took the time to check this out. Copied and pasted your code in VS2010, then had to change the Unicode settings (thank you very much for that :-/) and tested. Worked as you described, so I investigated.

I checked your message pump and found the error. You can calling GetMessage() with a specific HWND. This means that you are only pumping messages for that window ONLY. Therefore, no toolbar messages are ever pumped.

So, solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
	while ((ret = GetMessage(&msg,NULL,0,0)) != 0){
		if (ret == -1)
		{
			//An error occurred.  Deal with it.
		}
		else
		{
			if (!IsDialogMessage (toolbar, &msg)){
				TranslateMessage (&msg);
				DispatchMessage(&msg);
			}
		}
	}


Note the updated code. This is because in more recent versions of Windows, GetMessage() may return the value of -1 if an error occurs. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx , the Remarks section.
^thanks so much for taking ur time to check this
will try now to fix this

EDIT: TYTYTYTYTYSTYTYTY, THANK YOU SO MUCH
Last edited on
Topic archived. No new replies allowed.