实例介绍
【实例截图】
【核心代码】
#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; }
标签: 窗口
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论