基本信息
源码名称:c++ 动态建立一个窗口
源码大小:0.02M
文件格式:.rar
开发语言:C/C++
更新时间:2020-06-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍



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