Skip navigation

The trackbar is notorious for giving people trouble when trying to get started with it.

It doesn’t help that the docs or the really poor example page don’t say ANYTHING about what I’m about these small but annoying issues.

MY TRACKBAR WON’T SHOW UP!!

Try looking up the error code. If its a 1407, that’s “Cannot find window class.” That happens because you didn’t call:

InitCommonControls();

error C2065: ‘TRACKBAR_CLASS’ : undeclared identifier!

You have to

#include <commctrl.h>

error LNK2019: unresolved external symbol __imp__InitCommonControls@0 referenced in function _WinMain@16

You have to

#pragma comment( lib, "comctl32.lib" )

Here’s a real quick example of how to get the trackbar working in a basic win32 app (no mfc or .net stuff), fast.

//////////////////////////////////////////
//                                      //
// Trackbars                            //
//                                      //
// You found this at bobobobo's weblog, //
// https://bobobobo.wordpress.com        //
//                                      //
// Creation date:  Feb 13/08            //
// Last modified:  Feb 13/08            //
//                                      //
//////////////////////////////////////////

#include <windows.h>
#include <commctrl.h>       // must have

#pragma comment( lib, "comctl32.lib" )  // must have

////////////////////////
// TRACKBARS!

// We want to add trackbars to our
// windows application.

#define IDCHC_TRACKBAR 4

/////////////////////
// GLOBALS
HWND g_hwnd;
HWND g_trackbarhwnd;
//
/////////////////////

// Function prototypes.
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );


////////////////////////////
// In a C++ Windows app, the starting point is WinMain().
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
    #pragma region part 1 - create a window
    // A.  Create the WNDCLASS
    WNDCLASS wc;
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hbrBackground = GetSysColorBrush( COLOR_APPWORKSPACE );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = TEXT("Philip");
    wc.lpszMenuName = 0; 
    wc.style = CS_HREDRAW | CS_VREDRAW;

    // B.  Register the WNDCLASS with Windows, THEN
    //     create the window.
    RegisterClass( &wc );
    g_hwnd = CreateWindow(
        TEXT("Philip"),
        TEXT("windows app"),
        WS_OVERLAPPEDWINDOW,
        10, 10,
        400, 400,
        NULL, NULL,
        hInstance, NULL );      

    ShowWindow(g_hwnd, iCmdShow );
	UpdateWindow(g_hwnd);
    #pragma endregion

    #pragma region add the trackbar
    InitCommonControls();
    g_trackbarhwnd = CreateWindow(
        TRACKBAR_CLASS, // USE PREDEFINED CLASS
                        // MUST #include  TO USE

        TEXT("da trackbar son"),    // doesn't matter what
                                    // you call it -

        WS_CHILD | WS_VISIBLE |
        // next are the Trackbar styles
        TBS_AUTOTICKS |        // trackbar has tick marks
        TBS_HORZ,              // horizontal orientation
        100, 100,
        200, 30,
        g_hwnd,//parent
        (HMENU)IDCHC_TRACKBAR,  // identifier
        hInstance,  //(HINSTANCE)GetWindowLong( g_hwnd, GWL_HINSTANCE ),
        NULL );

    if( g_trackbarhwnd == NULL )
    {
        TCHAR errbuf[300];
        wsprintf( errbuf, TEXT("error code #%d"), GetLastError() );
        FatalAppExit( 0, errbuf );
    }
    
    /////////////////
    // NOW, as SOON AS the track bar is created,
    // we pelt it with some messages to give it
    // additional information about how we want
    // it to be.  If your computer was INCREDIBLY (20MHz?)
    // slow, you'd see these changes taking place,
    // because the trackbar has already been created
    // and made visible on the main window itself
    // in the line above.
    SendMessage(g_trackbarhwnd,     // hwnd - handle to trackbar
                TBM_SETRANGE,       // message - a message to set the range
                true,               // wparam - redraw it?  yes.
                MAKELONG(0/*MIN RANGE*/, 10/*MAX RANGE*/));  // lparam
    
    // The only tricky part is lparam.
    
    // We have to use a tricky macro called MAKELONG.

    ///////////
    // SUPERFICIALLY:
    // You know how when you're processing a
    // WM_LBUTTONDOWN mouse message, the x AND
    // y position of the mouse are BOTH packed
    // into the single variable lparam?
/*
case WM_LBUTTONDOWN:
    int x = LOWORD( lparam );
    int y = HIWORD( lparam );
    break;
*/
    // WELL, MAKELONG( a, b ) works to PACK
    // a and b into a SINGLE variable of type
    // long.

    // It uses bitshifting to do so.
    // Look at its #define if interested.

    // You can send plenty of other messages
    // to your trackbar to customize its look and
    // behavior!

    SendMessage(g_trackbarhwnd, TBM_SETPAGESIZE, true, 1); 
    SendMessage(g_trackbarhwnd, TBM_SETTICFREQ, true, 0); 
    SendMessage(g_trackbarhwnd, TBM_SETPOS, true, 0); 
    SendMessage(g_trackbarhwnd, TBM_SETSEL, true, MAKELONG( 1, 5 ) );
                

    #pragma endregion

    #pragma region part 2 - enter message loop
    MSG msg;

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

    return msg.wParam;
}

LRESULT CALLBACK WndProc(   HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam ) 
{
    switch( message )
    {
    case WM_CREATE:
        Beep( 50, 10 );
        return 0;
        break;

    case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            hdc = BeginPaint( hwnd, &ps );
                // Nothing to draw.
            EndPaint( hwnd, &ps );
        }
        return 0;
        break;

    case WM_KEYDOWN:
        switch( wparam )
        {
        case VK_ESCAPE:
            PostQuitMessage( 0 );
            break;
        default:
            break;
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage( 0 ) ;
        return 0;
        break;
    }
 
    return DefWindowProc( hwnd, message, wparam, lparam );
}




/* 
     ____   __   __      __   __  ___
    / _  \ /  / /  /    /  /  \ \/  /
   / _/ / /  / /  /    /  /    \   /
  / _/ \ /  / /  /__  /  /__   /  /
 /_____//__/ /______//______/ /__/

*/

Visual Studio 2005 project files for Trackbar on Win32 hosted by esnips (thanks esnips!)

Advertisements

7 Comments

    • Mayank
    • Posted September 16, 2009 at 12:24 am
    • Permalink

    THANK YOU!! You are absolutely right — this information is incredibly hard to find. WHAT IS WITH THE CRAZY #pragmas from microsoft?!?!

    • HAPKAH
    • Posted July 6, 2010 at 7:18 am
    • Permalink

    Thanks man! I’ve gone through a lot of junky help pages on MSDN and forums that mentioned everything but adding “#pragma comment( lib, “comctl32.lib” )” string to link with comctl32.lib.

    This is a very valuable info, you’ve saved a really lot of time for a lot of people.
    Thanks!

  1. The source code to all of the common controls and dialogs, including trackbar, was amongst the leaked win2k source code that is available on TPB and many other filesharing sites.

    • Anonymous
    • Posted November 4, 2011 at 9:43 am
    • Permalink

    Thanks so much for posting this! That #pragma-stuff solved my problem!

    • Anonymous
    • Posted February 6, 2013 at 2:32 pm
    • Permalink

    Thank You… For This Example. It Helps Me To InitializeCommonControls

    • Anonymous
    • Posted December 19, 2013 at 6:52 pm
    • Permalink

    big help, tnx

    • NirajKakodkar
    • Posted October 16, 2015 at 3:37 pm
    • Permalink

    Thanks You very much, you are the life saver ..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: