Sign in to follow this  
psae0001

Create a Dialog Box w/o using resource file

Recommended Posts

Ok, in VC++ 6.0, it's so easy to create a dialog box. But have anyone tried to do that with out using the rescourc script file? I'm trying to do that, but somehow it just wired.

// testing.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	Test2(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_TESTING, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTING);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_TESTING);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_TESTING;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
	case WM_CREATE:
		CreateWindowEx (WS_EX_APPWINDOW, "Testing", "Test Dialog Box",
				WS_CAPTION | WS_OVERLAPPED | WS_CHILD | WS_VISIBLE | WS_SYSMENU,
				200, 200, 100, 100, hWnd,
				(HMENU) (0xFF00), hInst, 0);
		DialogBox (hInst, (LPCTSTR) 0xFF00, hWnd, (DLGPROC) Test2);
		return 0;

		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox (hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC) About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

// Mesage handler for about box.
LRESULT CALLBACK Test2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;
	
	case WM_CLOSE:
		EndDialog (hDlg, LOWORD (wParam));
		return TRUE;

	}
    return FALSE;
}


Share this post


Link to post
Share on other sites
You're trying to create a Dialog by sending "DialogBox()" the same HMENU as for your window??

DialogBox (hInst, (LPCTSTR) 0xFF00, hWnd, (DLGPROC) Test2);


That hardcoded "0xFF00" is the same identifier you used for your window.

What you're telling Windows to do is look in the .exe file for the resource associated with the number "0xFF00". Since you didn't define any, it won't make one!

From MSDN.

...The DialogBox macro creates a modal dialog box from a dialog box template resource.

Since you have none, you need to describe your dialog box somehow at runtime, or Windows won't know what to create. That's where you need DialogBoxIndirect().

You have to supply the parameters, if you don't want to use the resource editor!

HTH


Chad

Share this post


Link to post
Share on other sites
Hi, first time posting here. I learned this today myself. Go to your directory where your files are on the computer. Right-click on the .rc file and open it as a text file. Then you can add your code on the bottom.

Then go here to learn how to implement your own code.
http://www.winprog.org/tutorial/dialogs.html

When you save the file, and switch back to VC++, it'll ask if you want to reload the .rc file. Click yes, and it'll update the file. Then compile & test. It should work fine.

I hope this is what you wanted.

Share this post


Link to post
Share on other sites
Ok, I changed my code to create a sub-window (S-W) instead of a dialog box, and I got some success. However, it still not completely function as a window. I create my S-W at the WM_CREATE notice and use SetWindowLong to set the WNDPROC for it.

But, when I click the S-W title bar, it doesn't turn the color indicates the focus is on the S-W now. Also, I don't what kinda message it'll recieve...any ideas???



...

// The code on the main WNDPROC to create the S-W
case WM_CREATE:
temp1 = CreateWindowEx (WS_EX_APPWINDOW, szWindowClass, "Test Dialog Box",
WS_CAPTION | WS_OVERLAPPED | WS_CHILD | WS_VISIBLE | WS_SYSMENU,
300, 300, 200, 200, hWnd, (HMENU) (0xFF00), hInst, 0);
if (!temp1)
{
char temp[100];
sprintf (temp, "Can't create sub-window %d", GetLastError());
MessageBox (NULL, temp, ERROR, MB_OK);
}
else
{
ShowWindow (temp1, SW_SHOWNORMAL);
UpdateWindow (temp1);
SetWindowLong (temp1, GWL_WNDPROC, (LONG) Test2);
}
return 0;


.....

// The WNDPROC to handle the S-W, but it doesn't seem right
// although the WM_DESTORY seems working fine.

LRESULT CALLBACK Test2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
MessageBox (NULL, "Testing", ERROR, MB_OK);
return 0;

case WM_DESTROY:
SetWindowLong(hDlg, GWL_WNDPROC, (LONG) NULL);
return 0;

default:
return DefWindowProc(hDlg, message, wParam, lParam);
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this