Visual C++ 6.0 Sliders in Dialog Boxes

This is a discussion on Visual C++ 6.0 Sliders in Dialog Boxes within the C++ Programming forums, part of the General Programming Boards category; I have created a dialog box that has a checkbox and want to put a slider in it as well. ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    Visual C++ 6.0 Sliders in Dialog Boxes

    I have created a dialog box that has a checkbox and want to put a slider in it as well. I don't know how to get information from the slider I have put into the dialog box and I don't know how to set the ticks on the slider. I have the checkbox working correctly and use the following functions to get the information from it:

    ::CheckDlgButton
    ::IsDlgButtonChecked

    What functions would I use to get information from the slider? And how can I set the slider's ticks?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    If using Win32 API - Trackbar Controls
    If using MFC - Using CSliderCtrl

    gg

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Ok, I tried putting the Win32 API function in my code and I get the following problems:

    1. it says 'hwndTrack' is an undeclared identifier
    2. I can't figure out what I'm supposed to put for the instance (where it has g_hinst in the example)

    How can I get this function to work?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Well, the examples aren't always meant to be a copy-and-paste solution, but this one is fairly close.

    'hwndTrack' should be a local var so just declare it locally with "HWND hwndTrack;".
    'g_inst' represents your module's HINSTANCE to be associated with the window. If your module is an EXE (and not a DLL) then you can use the value passed to WinMain() or use the return value of GetModuleHandle(NULL).

    gg

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Ok, I fixed the hwndTrack error, but I still can't figure out the instance. How would I find the HINSTANCE associated with my dialog window?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Quote Originally Posted by Codeplug
    'g_inst' represents your module's HINSTANCE to be associated with the window. If your module is an EXE (and not a DLL) then you can use the value passed to WinMain() or use the return value of GetModuleHandle(NULL).

    gg
    Use one or the other. The return value from GetModuleHandle(NULL) can be used as an HINSTANCE.

    gg

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Quote Originally Posted by The Letter J
    How would I find the HINSTANCE associated with my dialog window?
    Quote Originally Posted by Codeplug
    use the return value of GetModuleHandle(NULL).
    .....

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    My program is a simple dll, so I used GetModuleHandle(NULL) and got the following error:

    cannot convert parameter 10 from 'const int' to 'struct HMENU__ *'

    Here is how I put it in:

    Code:
    hwndTrack = CreateWindowEx( 
            0,                             // no extended styles 
            TRACKBAR_CLASS,                // class name 
            "Trackbar Control",            // title (caption) 
            WS_CHILD | WS_VISIBLE | 
            TBS_AUTOTICKS | TBS_ENABLESELRANGE,  // style 
            10, 10,                        // position 
            200, 30,                       // size 
            gOptionsDlg,                       // parent window 
            IDC_SLIDER1,             // control identifier 
            GetModuleHandle(NULL),                       // instance 
            NULL                           // no WM_CREATE parameter 
            );

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Would it help to know that I used the dialog boxes in the resource creator in VC++ 6.0 as opposed to coding it in raw C++?

  10. #10
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Staticly cast GetModuleHandle(NULL) to HMENU:

    (HMENU)GetModuleHandle(NULL)

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Ok, I did that and got the following error:

    'CreateWindowExA' : cannot convert parameter 10 from 'const int' to 'struct HMENU__ *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    Here is how I put it in:
    Code:
    hwndTrack = CreateWindowEx( 
            0,                             // no extended styles 
            TRACKBAR_CLASS,                // class name 
            "Trackbar Control",            // title (caption) 
            WS_CHILD | WS_VISIBLE | 
            TBS_AUTOTICKS | TBS_ENABLESELRANGE,  // style 
            10, 10,                        // position 
            200, 30,                       // size 
            gOptionsDlg,                       // parent window 
            IDC_SLIDER1,             // control identifier 
            (HMENU)GetModuleHandle(NULL),                       // instance 
            NULL                           // no WM_CREATE parameter 
            );

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to cast the control identifier (which, incidentally, is parameter 10):
    Code:
    hwndTrack = CreateWindowEx( 
            0,                             // no extended styles 
            TRACKBAR_CLASS,                // class name 
            "Trackbar Control",            // title (caption) 
            WS_CHILD | WS_VISIBLE | 
            TBS_AUTOTICKS | TBS_ENABLESELRANGE,  // style 
            10, 10,                        // position 
            200, 30,                       // size 
            gOptionsDlg,                       // parent window 
            (HMENU) IDC_SLIDER1,             // control identifier 
            GetModuleHandle(NULL),                       // instance 
            NULL                           // no WM_CREATE parameter 
            );
    GetModuleHandle(NULL) will give you the handle of the process or exe, not the current DLL. This was mentioned by Codeplug:
    Quote Originally Posted by Codeplug
    'g_inst' represents your module's HINSTANCE to be associated with the window. If your module is an EXE (and not a DLL) then you can use the value passed to WinMain() or use the return value of GetModuleHandle(NULL).
    If you need the handle of a DLL, you should store it in DllMain, or use another method. However, on NT/2000/XP the hInstance argument of CreateWindowEx is ignored, so your program may still work.
    Last edited by anonytmouse; 12-28-2004 at 07:34 PM.

  13. #13
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    If you view this reference you will see that parameter 10 is HMENU and 11 is HINSTANCE. Don't cast the 11th parameter as HMENU, rather cast the 10th.

    If you aren't using a resource, define the variable right after including header files and/or put this code in your own header file. For instance:
    Code:
    #define IDC_SLIDER1 10
    This defines IDC_SLIDER1. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Ok, I fixed that part, but I am now getting 2 completely new errors:

    error LNK2001: unresolved external symbol __imp__InitCommonControls@0
    fatal error LNK1120: 1 unresolved externals

    I have no idea what either of these two mean, can anyone help me out? This is really starting to get frustrating.

  15. #15
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    You aren't linking to comctl32.lib which this API is defined in. This library is not included by default so you will either need to add it to the libraries on your command line, or add it in your VC++ project settings on the Link tab.

    You can add it in the source code by doing the following:
    Code:
    #include <commctrl.h>			// Include header
    
    #pragma comment(lib, "comctl32.lib")	// Include library
    - Stack Overflow
    Last edited by Stack Overflow; 12-28-2004 at 07:45 PM. Reason: Fixed typo.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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