Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass a button value back? 1

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
0
0
GB
Hi all, Ive created a four button dialog box as a function and I want to return the number or name of the button pushed.

This is what I have;

Code:
#include <windows.h>
#include <tchar.h>

#define IDC_BUTTON1  1201
#define IDC_BUTTON2  1202
#define IDC_BUTTON3  1203
#define IDC_BUTTON4  1204
#define IDC_STATIC_1 1200


HINSTANCE InstanceHandle;
CHAR ButtonClicked = 'A';
TCHAR BoxText[] = _T("Filename");


LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
   { 
   case WM_CREATE:
        
      CreateWindow("STATIC",
                   BoxText, // Initial Text
                   WS_CHILD | WS_VISIBLE | SS_LEFT,  // Style
                   5, 5, 100, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_STATIC_1), // ID
                   InstanceHandle,  // The application
                   0);

      CreateWindow("BUTTON",
                   "Yes",                        // Button Text
                   WS_CHILD | WS_VISIBLE,  // Style
                   20, 70, 60, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_BUTTON1), // ID
                   InstanceHandle,  // The application
                   0);
                   
      CreateWindow("BUTTON",
                   "No",                        // Button Text
                   WS_CHILD | WS_VISIBLE,  // Style
                   90, 70, 60, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_BUTTON2), // ID
                   InstanceHandle,  // The application
                   0);
                   
      CreateWindow("BUTTON",
                   "Not to All",                        // Button Text
                   WS_CHILD | WS_VISIBLE,  // Style
                   200, 70, 60, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_BUTTON3), // ID
                   InstanceHandle,  // The application
                   0);
                   
      CreateWindow("BUTTON",
                   "Yes to All",                        // Button Text
                   WS_CHILD | WS_VISIBLE,  // Style
                   360, 70, 60, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_BUTTON4), // ID
                   InstanceHandle,  // The application
                   0);
      break;
            
   case WM_COMMAND:

      if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_BUTTON1)
      {
         ButtonClicked == '1';
      }
      if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_BUTTON2)
      {
         ButtonClicked == '2';
      }
      if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_BUTTON3)
      {
         ButtonClicked == '3';
      }
      if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_BUTTON4)
      {
         ButtonClicked == '4';
      }
         break;
   }
      return DefWindowProc(hwndDlg, msg, wParam, lParam);
}

extern "C"

int __declspec(dllexport) WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, INT nCmdShow)
{
   InstanceHandle = hInstance;

   WNDCLASS wc;
   memset(&wc, 0, sizeof(WNDCLASS));
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
   wc.lpfnWndProc = DialogProc;
   wc.hInstance = InstanceHandle;
   wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
   wc.lpszClassName = "ClassName";
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   if(!RegisterClass(&wc))
      return FALSE;
         
   HWND WindowHandle = CreateWindow("ClassName",
                                    "ClassName", // Caption text
                                    WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
                                    WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | 
                                    WS_BORDER | WS_SYSMENU,
                                    100, 100, 450, 150,  // Position
                                    NULL,
                                    NULL,
                                    InstanceHandle,
                                    0);
   MSG Msg;
   while (GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
   {
      if(!IsDialogMessage(WindowHandle, &Msg))
      {
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
   }
  return 0;
}

At present I can only exit the form and get a zero returned! I want to start the dialog, get a response from the user and then close the form.

What am I doign wrong?, any help appreciated (as Im lost).
 
Hi there - I know this is not the most elegant solution but you could try changing your winMain function so it looks a bit like this:

Code:
int __declspec(dllexport) WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, INT nCmdShow)
{
   InstanceHandle = hInstance;
   ButtonClicked = 0;

   WNDCLASS wc;
   memset(&wc, 0, sizeof(WNDCLASS));
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
   wc.lpfnWndProc = DialogProc;
   wc.hInstance = InstanceHandle;
   wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
   wc.lpszClassName = "ClassName";
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   if(!RegisterClass(&wc))
      return FALSE;
         
   HWND WindowHandle = CreateWindow("ClassName",
                                    "ClassName", // Caption text
                                    WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
                                    WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION |
                                    WS_BORDER | WS_SYSMENU,
                                    100, 100, 450, 150,  // Position
                                    NULL,
                                    NULL,
                                    InstanceHandle,
                                    0);
   MSG Msg;
   while (GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
   {
      if(!IsDialogMessage(WindowHandle, &Msg))
      {
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
	  else
	  {
		if(ButtonClicked != 0)	
		{
			TRACE("Button value is %c\n", ButtonClicked);
			CloseWindow(WindowHandle);
			return ButtonClicked;
		}
	  }
   }
  return 0;
}

I hope that is some kind of help to you.
M
 

M,

You rock! Works fine now, I had to change ButtonClicked from a char to an int but it flies now. Since I had previously set ButtonClicked as a CHAR, most of my experiments to get it working were actually failing as a result of that.

Is there any way when the dialog closes for it to just disappear? (when it closes it kind of shrinks away to the bottom right corner of the screen).
 

Thats fine now too.

One thing I just noticed is that when I compile to this a DLL and call it five times in succession, it only shows the dialog on the first call.

When I call the DLL, I write out the return value, I see the dialog once, hit button 1 and then see "10000" appear on the command line. Do I need to release the windowhandle somehow?
 
Well it's hard for me to advise because I don't know exactly how your calling your function inside the dll but in general terms Yes. It's always a good idea to clear up when you have finished with things.

It also may help if you break out of the while loop when you have picked up the button click so any further messages don't get delt with. (I'm kinda vague on the way it fundimentally works so that may not be needed.)

M
 

ok and tbanks for your help, have a star.
 
You are very welcome - I hope the rest of your system comes together smoothly.

M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top