Windows控件-滚动条的简单示例

/**
	Scroll bar class test
*/

#include <windows.h>
#include "ScrollBarBaseHeader.h"

//--------------------------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------------------------
HINSTANCE   g_hInst = NULL;
HWND        g_hWnd = NULL;
const	int BarNum = 3;

//--------------------------------------------------------------------------------------
// Forward declarations
//--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK    WndProc( HWND, UINT, WPARAM, LPARAM );


//--------------------------------------------------------------------------------------
// Entry point to the program. Initializes everything and goes into a message processing 
// loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------------------
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
    UNREFERENCED_PARAMETER( hPrevInstance );
    UNREFERENCED_PARAMETER( lpCmdLine );

    if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
        return 0;

    // Main message loop
    MSG msg = {0};
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    return ( int )msg.wParam;
}


//--------------------------------------------------------------------------------------
// Register class and create window
//--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{
    // Register class
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof( WNDCLASSEX );
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )0 );
    wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
    wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"SBCT";
    wcex.hIconSm = LoadIcon( wcex.hInstance, ( LPCTSTR )0 );
    if( !RegisterClassEx( &wcex ) )
        return E_FAIL;

    // Create window
    g_hInst = hInstance;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    g_hWnd = CreateWindow( L"SBCT", L"Scroll Bar Class Test", WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
                           NULL );
    if( !g_hWnd )
        return E_FAIL;

    ShowWindow( g_hWnd, nCmdShow );

    return S_OK;
}


//--------------------------------------------------------------------------------------
// Called every time the application receives a message
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    PAINTSTRUCT ps;
    HDC hdc;
    static HWND ScrollBar[BarNum];						//这里的滚动条和画刷变量应该是静态的
    static HBRUSH sBrush[BarNum];						//因为,我们需要在程序的生命周期内一
										//直使用他们
    int i,barColorVal[BarNum] = { 255, 255, 255};		

    switch( message )
    {
	case WM_CREATE:
		for( i = 0; i < BarNum; ++i )
		{
			//创建滚动条
			ScrollBar[i] = CreateWindow( L"scrollbar", 0,
							WS_VISIBLE | WS_CHILD |
							SBS_HORZ ,
							0, 0+20*i, 200, 10,
							hWnd, (HMENU)i, g_hInst, 0 );
			SetScrollRange( ScrollBar[i], SB_CTL, 0, 255, false );
			SetScrollPos( ScrollBar[i], SB_CTL, 0, false );
		}
			
		//创建用于滚动条着色的画刷
		sBrush[0] = CreateSolidBrush( RGB( barColorVal[0], 0, 0 ) );
		sBrush[1] = CreateSolidBrush( RGB( 0, barColorVal[1], 0 ) );
		sBrush[2] = CreateSolidBrush( RGB( 0, 0, barColorVal[2] ) );

		return 0;
		
		//这个消息,用来返回,用于绘制滚动条的画刷。
		//或者可以简单的理解为,为滚动条上色。
	case WM_CTLCOLORSCROLLBAR:
          	i = GetWindowLong ((HWND) lParam, GWL_ID) ;
         	 return (LRESULT)sBrush[i];

        case WM_DESTROY:
			for( i = 0; i < BarNum; ++i )
			{
				DeleteObject( sBrush[i] );
			}
            PostQuitMessage( 0 );
            break;

        default:
            return DefWindowProc( hWnd, message, wParam, lParam );
    }

    return 0;
}


简单的滚动条控件小实例,还未完善,滚动功能还没加进去。

吐槽一下代码的排版,太郁闷了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章