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;
}


簡單的滾動條控件小實例,還未完善,滾動功能還沒加進去。

吐槽一下代碼的排版,太鬱悶了。


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