Jump to content
  • Advertisement
Sign in to follow this  
Dookie

Simple (hopefully) slider control help...

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, I really need to know what I'm doing wrong here in my CPP program... I'm trying to get a slider control into my dialog box, but it doesn't work. Here's all of the code:
#include	<windows.h>
#include	<commdlg.h>
#include	"resource.h"
#include	"commctrl.h"


INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );


INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, INT nCmdShow )
{
	// Display the main dialog box.
	DialogBox( hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, MainDlgProc );

	return TRUE;
}


INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
	// HRESULT hr;
	HWND	hwndTrack;

	switch( msg ) 
	{
		case WM_INITDIALOG:
			hwndTrack = GetDlgItem( hDlg, IDC_SLIDER1 );
			SendMessage(hwndTrack,
				TBM_SETRANGE, 
				TRUE,					// redraw flag 
				MAKELONG(0, 1000));		// min. & max. positions
			SendMessage(hwndTrack, TBM_SETPOS, FALSE, 0);
			break;
		case WM_COMMAND:
			switch( LOWORD(wParam) )
			{
				case IDOK:
					EndDialog( hDlg, IDCANCEL );
					break;
				case IDCANCEL:
					EndDialog( hDlg, IDCANCEL );
					break;
				default:
					return FALSE;	// Didn't handle message
			}
			break;

		default:
			return FALSE;			// Didn't handle message
	}

	return TRUE;					// Handled message
}

And here's a pic to help illustrate what I've done: Not much to it. Here's how I've experimented... 1) Created a simple dialog box with an OK and CANCEL button on it (no slider control). 2) Wrote the code for my dialog box to do something when OK or CANCEL was clicked (EndDialog()). 3) Compiled it. 4) Ran the program, and a dialog appeared with an OK and CANCEL button on it. 5) Pressed OK on dialog box, and it disappeared. And here's where I started having my problem... 5) Added a 'slider control' to my dialog box (IDC_SLIDER1). 6) Compiled the code. 7) Ran the program, no dialog box appeared and the program returned to my CPP IDE as if I'd clicked the dialog box's OK button. 8) Added code for the slider control (set its limits in 'WM_INITDIALOG'). 9) Compiled the code. 10) Ran the program, still no dialog box appeared. The program simply returned to my CPP GDI as if I'd clicked the dialog box's OK button, but no dialog box ever appeared. 11) Deleted the slider control from my dialog box. 12) Compiled the code. 13) Ran the program, and a dialog appeared with an OK and CANCEL button on it. You can see that when I add a slider control to my simple dialog box, it won't run. Then if I delete that slider control, it runs fine. Feel free to copy this code and paste it into your CPP compiler program; create a dialog box with an OK and CANCEL button on it and it'll work fine, add a slider control and it won't run at all. How do I make a slider control work in this dialog box? Thanks in advance for the help!

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the reply, Nychold! Is this what you wanted to see?


// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END

3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOG1 DIALOGEX 0, 0, 186, 95
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "&OK",IDOK,129,7,50,14
PUSHBUTTON "&Cancel",IDCANCEL,129,24,50,14
CONTROL "",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH |
TBS_NOTICKS | WS_TABSTOP,7,68,84,15
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_DIALOG1, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 179
TOPMARGIN, 7
BOTTOMMARGIN, 88
END
END
#endif // APSTUDIO_INVOKED

#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the trackbar control is part of the common control's dll. you need to call InitCommonControls or InitCommonControlsEx somewhere before you load the dialog in order to use them. this will register the window class that the control windows use for processing msgs.

Share this post


Link to post
Share on other sites
Thanks for the reply, ap. I plugged that function into my program, but it doesn't work... If I try to use 'InitCommonControlsEx' or 'InitCommonControls', I get a linker error. I think it's because I need the library 'Commctrl.lib'. So I tried to find Commctrl.lib on my harddrive, but it's not there. If using those functions are the only way to get a trackbar to work, then I'm out of luck. Pretty frustrating... Anyhoo, since that's the case, I'm working on a different way to get around my trackbar problem. It's ugly, but at least it works.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dookie
Thanks for the reply, ap. I plugged that function into my program, but it doesn't work... If I try to use 'InitCommonControlsEx' or 'InitCommonControls', I get a linker error. I think it's because I need the library 'Commctrl.lib'. So I tried to find Commctrl.lib on my harddrive, but it's not there.


It's "comctl32.lib"

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
the trackbar control is part of the common control's dll. you need to call InitCommonControls or InitCommonControlsEx somewhere before you load the dialog in order to use them. this will register the window class that the control windows use for processing msgs.


That's right! Although, I think I remember that you don't actually have to call it. Just have it in the code somewhere. I remember writing this stuff in Assembly years ago, and always putting it after my WinMain function returned control to the computer.

EDIT: I just confirmed it with the following code:


//---------------------------------------------------------------------------
#include <stdio.h>
#include <windows.h>
#include <commctrl.h>
//---------------------------------------------------------------------------

LRESULT CALLBACK WinProc(HWND hWin,UINT uMsg,WPARAM wParam,LPARAM lParam);

#pragma argsused
int main(int argc, char **argv)
{
HINSTANCE hInst=GetCurrentProcess();
WNDCLASS wc=
{
0,
WinProc,
0,0,
hInst,
LoadIcon(NULL,IDI_APPLICATION),
LoadCursor(NULL,IDC_ARROW),
(HBRUSH)GetStockObject(BLACK_BRUSH),
NULL,
"BlahBlahBlah"
};

if (!RegisterClass(&wc))
{
printf("Unable to register class! Exiting.\n");
return 0;
}

HWND hWin=CreateWindow("BlahBlahBlah","Window",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInst,NULL);
if (!hWin)
{
printf("Unable to create window. Exiting.\n");
return 0;
}

ShowWindow(hWin,SW_SHOW);
UpdateWindow(hWin);

MSG msg;

while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
InitCommonControls();
}

LRESULT CALLBACK WinProc(HWND hWin,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
CreateWindow(TRACKBAR_CLASS,"Trackbar",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | TBS_AUTOTICKS | WS_BORDER,
0,70,200,300,hWin,NULL,GetCurrentProcess(),NULL);
break;

case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hWin,uMsg,wParam,lParam);
}
//---------------------------------------------------------------------------



[Edited by - Nychold on September 9, 2005 8:26:48 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Nychold
That's right! Although, I think I remember that you don't actually have to call it. Just have it in the code somewhere. I remember writing this stuff in Assembly years ago, and always putting it after my WinMain function returned control to the computer.


i've noticed that too! but it doesn't always seem to work when the control is tied to a dialog box that gets loaded from WinMain that doesn't have a main msg pump, as is the case of the OP's code. i've seen so many of these posts here on GameDev and calling InitCommonControls always solves the problem.

Share this post


Link to post
Share on other sites
Hey Verg, that did the trick. Thanks! I just misspelled 'Commctrl.lib' (copied that text directly from Microsoft's msdn website - huh), and after I put in your proper spelling of the library (comctl32.lib) it compiled and ran just fine. Coolness, now I can get rid of those hideously-out-of-place looking horizontal scroll bars and replace them with actual slider controls! Thanks again for the help, I really appreciate it.

I'd recommend anybody trying to put a trackbar into their program to read this thread. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!