Sign in to follow this  
Johannes1991

Visual 2008 Dialog box

Recommended Posts

I've made a dialog box in visual 2008 in c++. But i have no idea how to use it or create it. in the box people can write things that i want to save into a string and use it later. been googling around a bit but no answers. i was looking at msdn and i found a DialogBox(), am i going to use that? Help would be GREAT!

Share this post


Link to post
Share on other sites
It is not trivial in Win32. You are on the right track with DialogBox() but there is a bit more to it.

Basically you need to provide a window procedure for your dialog box, which is similar to your normal window's WndProc. This is the procedure that then recieves messages from Windows while the dialog is open.

Your parameters to DialogBox() are then the instance of your application (passed to WinMain() or you can retrieve it with GetModuleHandle(NULL)), the resource ID of your dialog box resource, the HWND of the parent window and finally the above procedure.

To close the dialog, you call EndDialog() somewhere in its window procedure, passing as its one parameter the exit code you want DialogBox() to return.

All the code examples you need are here on MSDN. If you get stuck and have more specific questions, post back.

Share this post


Link to post
Share on other sites
Are you using plain Win32 or are you building an MFC application? MFC is build on top of Win32 and I find it a little easier to use, however it is probably best if you start with Win32 to understand what is going on first. MSDN has good code examples as mentined by Aardvajk

Share this post


Link to post
Share on other sites
From some of my code:

#include "AboutDialog.h"
#include "Resource.h"
#include <stdio.h>

//=================================================================================================

INT_PTR CALLBACK AboutDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
// Center window
RECT rcParent, rcDialog;
HWND hWndParent = GetParent(hWnd);
GetWindowRect(hWnd, &rcDialog);
int nWidthDialog = rcDialog.right - rcDialog.left;
int nHeightDialog = rcDialog.bottom - rcDialog.top;
GetWindowRect(hWndParent, &rcParent);
int nWidthParent = rcParent.right - rcParent.left;
int nHeightParent = rcParent.bottom - rcParent.top;
POINT ptNew;
ptNew.x = rcParent.left + (nWidthParent - nWidthDialog) / 2;
ptNew.y = rcParent.top + (nHeightParent - nHeightDialog) / 2;
SetWindowPos(hWnd, NULL, ptNew.x, ptNew.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
return TRUE;
}
break;

case WM_COMMAND:
{
if(LOWORD(wParam) == IDOK)
EndDialog(hWnd, IDOK);
}
break;
}
return FALSE;
}

//=================================================================================================

void ShowAboutDialog(HINSTANCE hInstance, HWND hWndParent)
{
DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_ABOUT), hWndParent,
AboutDialogProc, 0);
}

//=================================================================================================

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