Resource help

This is a discussion on Resource help within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to make this resource for a simple box, with an OK and a CANCEL button...but...for some reason it ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Resource help

    I'm trying to make this resource for a simple box, with an OK and a CANCEL button...but...for some reason it isn't showing up. I don't know what i'm doing wrong. I don't 'really' know how to make resources, but i thought i did it right. I have MSVC++...and here is my code.
    Code:
    BOOL MainDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    	case WM_CLOSE:
    		EndDialog(hWnd, 0);
    		return TRUE;
    	}
    	return FALSE;
    }
    DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1),NULL, (DLGPROC)MainDialogProc, 0);
    I would also like to add text to the box, and accept both the OK and CANCEL buttons.. I'm clueless, please help.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Looks ok. Sometimes msvc6 (if that's what you're using) can act a bit strangely. Create a new project and see if it still does the same.

    To change properties of controls etc go into the resource editor for the dialog and double-click on the controls. This should pop up a 'properties' window which you can use to set the control properties. Same for the dialog itself.

    To get 'notfications' from the dialog controls, you need to handle the WM_COMMAND message and look for the control identifiers (you can set these identifiers in the properties for each control as described above or just use the default ones that the resource editor generates). For an 'ok' button, the default id is 'IDOK' which is #defined as 1 (I think) and for a 'cancel' button, the default id is 'IDCANCEL' which is #defined as 2 (I think). So to trap response for button clicks, for example:
    Code:
    WM_COMMAND:
      {
      HWND hCntrl;  /*control handle, if any*/
      WORD wID;  /*control identifier*/
      WORD wNotify; /*notification message */
    
      wID=LOWORD(wParam);
      wNotify=HIWORD(wParam);
      hCntrl=(HWND)lParam;
    
      if (hCntrl)
        {
        /*notification msg is from a control */
        if (wNotify==BN_CLICKED) /* a button has been clicked*/
           {
           switch (uMsg)
             {
              case IDOK:
                /*ok button has been clicked */
               break;
              case IDCANCEL:
                /*cancel button has been clicked */
                break;
             }
           }
        }
      return 0;
      }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    eh, i don't really understand that code, could you please explain it...and where would i put it into a function to get it to work.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by Extol
    eh, i don't really understand that code, could you please explain it...and where would i put it into a function to get it to work.
    You would put that in your dialog procedure which you have called 'MainDialogProc'. You should read up on WM_COMMAND and BN_CLICKED on msdn.

    It seems to me that you probably need a quick primer on windows programming so try out winprog's which should give you a basic introduction suitable for use with your compiler.

    Also, if you are using msdn library for visual studio6 (which you will if you are using msvc6) then check out the 'Technical Articles' section. You should definitely take a look at 'Platform SDK->Windows Programming Guidelines->Win32 Programming->A generic win32 Application', also in msdn. 'Platform SDK->User Interface Services->Windowing' and 'Platform SDK->User Interface Services->Resources' should also prove useful.

    You might also consider getting a copy of Charles Petzold's 'Programming Windows 5th ed.' as it is an invaluable resource for win32 api programming.

    Good luck.

    edit: editing
    Last edited by Ken Fitlike; 04-20-2003 at 07:28 AM.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Alright, I finally got it to show up. I have a problem. How do I make my dialog box the active window, instead of having it in back of the console.
    Last edited by Extol; 04-20-2003 at 09:28 AM.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by Extol
    How do I make my dialog box the active window, instead of having it in back of the console.
    Create a win32 project, create your dialog resource, and instead of main use WinMain, for example, using your original code:
    Code:
    #include <windows.h>
    #include "resource.h" /*or whatever the path to your resource header is */
    
    BOOL MainDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    	case WM_CLOSE:
    		EndDialog(hWnd, 0);
    		return TRUE;
    	}
    	return FALSE;
    }
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR lpStr,int nCmdShow)
    {
    DialogBoxParam( GetModuleHandle(NULL),  MAKEINTRESOURCE(IDD_DIALOG1), NULL,  (DLGPROC)MainDialogProc,  0);
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    --------------------Configuration: Program10 - Win32 Debug--------------------
    Compiling...
    Program10.cpp
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/Program10.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Program10.exe - 2 error(s), 0 warning(s)


    I figure that error is because i have a Windows Console Application Project opened...but I have both Console and Windows things going on, so I kinda need the Console. I just want my resource to be the active window.
    Last edited by Extol; 04-20-2003 at 12:31 PM.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I just want my resource to be the active window.<<

    SetForegroundWindow.

    It would be arguably better to transfer the functionality of your console application to the windows gui application.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    I agree, but I don't really know how to go about all of it. And thank you for that link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 06:12 PM
  2. unmanaged resource
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2008, 03:23 AM
  3. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 12:07 AM
  4. resource problem/question
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 01-29-2003, 01:08 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21