[Win32, C++] Trackbar - Getting Tick Position [SOLVED]

Started by
3 comments, last by hkBattousai 17 years, 6 months ago
	trSpeed.SetInitials(10, 10, 250, 25);
	trSpeed.hInstance = hInstance;
	trSpeed.hWndParent = hWnd;
	trSpeed.Create();
	trSpeed.SetRange(0, 19);
	trSpeed.SetTickFrequency(1);
	trSpeed.SetPosition(5);
	
	int a = (int) trSpeed.GetTickPosition();
	char b[32];
	_itoa(a, b, 10);
	MessageBox(NULL, SetText(b), "Title", NULL);



I want to get tick position of a trackbar represented by trSpeed class, but the code above ALWAYS returns 25. Am I doing something wrong? Trackbar class defined as :
class CWnd
{
	public:
	DWORD	dwExstyle;
	char	lpClassName[MAX_LOADSTRING];
    	char	lpWindowName[MAX_LOADSTRING];
	DWORD	dwstyle;
	int	x;
	int	y;
	int	nWidth;
	int	nHeight;
	int	nRight;
	int	nBottom;
	HWND	hWndParent;
	HMENU	hMenu;
	HINSTANCE hInstance;
	LPVOID	lpParam;
	HWND	hWnd;
	
	HFONT hFont;
	HANDLE hImage;
	CHAR szImageFile[MAX_LOADSTRING];
	
	CWnd()
	{
		dwExstyle		= 0;
		strcpy(lpClassName, "ClassName");
		strcpy(lpWindowName, "Caption");
		dwstyle			= WS_VISIBLE | WS_CHILD;
		x			= 5;
		y			= 5;
		nWidth			= 100;
		nHeight			= 100;
		hWndParent		= HWND_DESKTOP;
		hMenu			= NULL;
		lpParam			= NULL;
	}

	VOID SetInitials(INT x, INT y, INT nWidth, INT nHeight)
	{
		this->x = x;
		this->y = y;
		this->nWidth = nWidth;
		this->nHeight = nHeight;
	}
	void Create(void)
	{
		hWnd = CreateWindowEx(	(DWORD)		dwExstyle,
					(LPCTSTR)	lpClassName,
					(LPCTSTR)	lpWindowName,
					(DWORD)		dwstyle,
					(int)		x,
					(int)		y,
					(int)		nWidth,
					(int)		nHeight,
					(HWND)		hWndParent,
					(HMENU)		hMenu,
					(HINSTANCE)	hInstance,
    					(LPVOID)	lpParam);
		DWORD ErrCode = GetLastError();
		if (hWnd == NULL)
		{
			MessageBox(NULL, "Error creating control.", "Error", MB_OK);
			DebugTest(ErrCode);
			//PostQuitMessage(0);
		}
		nRight = x + nWidth;
		nBottom = y + nHeight;
	}
	void Enable(bool State)
	{
		EnableWindow(hWnd, State);
	}
	char * GetText(unsigned long int MaxChars)
	{
		char * Buffer = new char[MaxChars];
		SendMessage(hWnd, WM_GETTEXT, (WPARAM) MaxChars, (LPARAM) Buffer);
		return Buffer;
		delete Buffer;
	}
	void SetText(char * Buffer)
	{
		if(!SendMessage(hWnd, WM_SETTEXT, (WPARAM) NULL, (LPARAM) Buffer))
		{
			DWORD dwError = GetLastError();
			DebugTest("Error setting text"); DebugTest(dwError);
		}
	}
	//void SetImage(void)
	//{
	//	hImage = LoadImage(hInstance, szImageFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	//	SendMessage(hWnd, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hImage);
	//}
	void SetFont(char * szFontName, int nSize)
	{
		hFont = CreateFont(	(int)		nSize,				// nHeight				height of font
					(int)		0,					// nWidth				average character width
					(int)		0,					// nEscapement			ngle of escapement
					(int)		0,					// nOrientation			base-line orientation angle
					(int)		FW_DONTCARE,		// fnWeight				font weight
					(DWORD)		FALSE,				// fdwItalic			italic attribute option
					(DWORD)		FALSE,				// fdwUnderline			underline attribute option
					(DWORD)		FALSE,				// fdwStrikeOut			strikeout attribute option
					(DWORD)		DEFAULT_CHARSET,	// fdwCharSet			character set identifier
					(DWORD)		OUT_DEFAULT_PRECIS,	// fdwOutputPrecision	output precision
					(DWORD)		CLIP_DEFAULT_PRECIS,// fdwClipPrecision		clipping precision
					(DWORD)		DEFAULT_QUALITY,	// fdwQuality			output quality
					(DWORD)		DEFAULT_PITCH | FF_DONTCARE,// fdwPitchAndFamily	itch and family
					(LPCTSTR)	szFontName);		// lpszFace				typeface name
		SendMessage(hWnd, WM_SETFONT, (WPARAM) hFont, TRUE);
	}
	void Show(void){ShowWindow(hWnd, SW_SHOW);}
	void Hide(void){ShowWindow(hWnd, SW_HIDE);}
};

class CTrackbar : public CWnd
{
	public:
	CTrackbar()
	{
		strcpy(lpClassName, TRACKBAR_CLASS);
		dwstyle |= TBS_AUTOTICKS;
	}
	void SetRange(LONG lMinimum, LONG lMaximum){SendMessage(hWnd, TBM_SETRANGE, TRUE, (LPARAM) MAKELONG(lMinimum, lMaximum));}
	void SetPosition(LONG lPosition){SendMessage(hWnd, TBM_SETPOS, TRUE, (LPARAM) lPosition);}
	void SetTick(LONG lPosition){SendMessage(hWnd, TBM_SETTIC, 0, (LPARAM) lPosition);}
	void SetTickFrequency(WORD wFreq){SendMessage(hWnd, TBM_SETTICFREQ, (WPARAM) (WORD) wFreq, 0);}
	LRESULT GetTickPosition(void){return SendMessage(hWnd, TBM_GETTICPOS, (WPARAM) (WORD) 0, 0);}
};




[Edited by - Battousai on October 4, 2006 2:44:45 PM]
Advertisement
You want to use the TBM_GETPOS message in CTrackbar::GetTickPosition, not TBM_GETTICPOS. TBM_GETTICPOS gets the position for a given tick position (in your case, you are asking for the 0th tick position), whereas TBM_GETPOS gets the position of the current tick (takes no args).

http://windowssdk.msdn.microsoft.com/en-us/library/ms650389.aspx
This doesn't work :
	case TB_THUMBPOSITION:		// here is code to handle this message		break;

Then what message is sent when user moves the thumb?
TB_THUMBTRACK also doesn't work.
The control sends the WM_HSCROLL or the WM_VSCROLL notification when the slider is moved.

http://windowssdk.msdn.microsoft.com/en-us/library/ms650378.aspx

Look at the "Trackbar Notification Messages" section
Raven Software :: Tools Programmer
[Ravensoft] [My Site] [Full Sail]
Thank you.
New code is :
		case WM_HSCROLL:			switch (LOWORD(wParam))			{				case SB_ENDSCROLL:				case SB_LEFT:				case SB_RIGHT:				case SB_LINELEFT:				case SB_LINERIGHT:				case SB_PAGELEFT:				case SB_PAGERIGHT:				case SB_THUMBPOSITION:				case SB_THUMBTRACK:					MainSpeed();   // event handler					break;			}			break;

This topic is closed to new replies.

Advertisement