📄 trackbar.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "TrackBar.hpp"
#include "Common.hpp"
#include "Debug.hpp"
#include "Layout.hpp"
#include "resource.h"
/*------------------------------------------------------------------------------
TrackBarImpl_t::TrackBarImpl_t
Constructor
------------------------------------------------------------------------------*/
TrackBarImpl_t::TrackBarImpl_t(
)
{
TRACE(ZONE_COMMON_CTOR);
m_pCallback = NULL;
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::~TrackBarImpl_t
Destructor
------------------------------------------------------------------------------*/
TrackBarImpl_t::~TrackBarImpl_t(
)
{
TRACE(ZONE_COMMON_CTOR);
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::ControlWindowProc
Handle messages
------------------------------------------------------------------------------*/
LRESULT
TrackBarImpl_t::ControlWindowProc(
UINT Message,
WPARAM wParam,
LPARAM lParam,
bool& Handled
)
{
LRESULT Result;
//by default assume we handled the message
Handled = true;
switch (Message)
{
case WM_SETFOCUS:
return OnSetFocus(wParam, lParam);
case WM_KILLFOCUS:
return OnKillFocus(wParam, lParam);
case WM_NOTIFY:
if ((reinterpret_cast<NMHDR*>(lParam)->hwndFrom == m_hwnd) &&
(reinterpret_cast<NMHDR*>(lParam)->code == NM_CUSTOMDRAW))
{
return OnCustomDraw(reinterpret_cast<NMCUSTOMDRAW*>(lParam));
}
Handled = false;
return 0;
case WM_CLOSE:
case WM_KEYDOWN:
Handled = ForwardMessageToParent(Message, wParam, lParam, &Result);
return Result;
case WM_COMMON_GETCALLBACK_PTR:
return OnGetCallback();
case WM_COMMON_SETCALLBACK_PTR:
return OnSetCallback(lParam);
default:
Handled = false;
return 0;
}
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::OnGetCallback
Returns the stored callback pointer
------------------------------------------------------------------------------*/
LRESULT
TrackBarImpl_t::OnGetCallback(
)
{
return (LRESULT)m_pCallback;
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::OnSetCallback
Sets the internal callback pointer to store
------------------------------------------------------------------------------*/
LRESULT
TrackBarImpl_t::OnSetCallback(
LPARAM lParam
)
{
m_pCallback = (INT_PTR)lParam;
return S_OK;
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::OnSetFocus
Notifies our parent that we have gained focus
------------------------------------------------------------------------------*/
LRESULT
TrackBarImpl_t::OnSetFocus(
WPARAM wParam,
LPARAM lParam
)
{
NotifyParent(VNM_SETFOCUS);
//Should the parent be notified?
LRESULT Result = DefWindowProc(WM_SETFOCUS, wParam, lParam);
//force a redraw NOW to reduce listbox item/trackbar inconsistencies
UpdateWindow(m_hwnd);
return Result;
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::OnKillFocus
Forces an immediate redraw when the trackbar loses focus
------------------------------------------------------------------------------*/
LRESULT
TrackBarImpl_t::OnKillFocus(
WPARAM wParam,
LPARAM lParam
)
{
LRESULT Result = DefWindowProc(WM_KILLFOCUS, wParam, lParam);
UpdateWindow(m_hwnd);
return Result;
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::OnCustomDraw
Handles NM_CUSTOMDRAW
------------------------------------------------------------------------------*/
LRESULT
TrackBarImpl_t::OnCustomDraw(
NMCUSTOMDRAW* pNMCustomDrawStruct
)
{
BOOL IsSelected = FALSE;
if (!pNMCustomDrawStruct)
{
ASSERT(0);
return 0;
}
switch (pNMCustomDrawStruct->dwDrawStage)
{
case CDDS_PREPAINT:
//if the stage is prepaint, notify the class that we want to
//be notified of custom drawing events
//remove focus from the control, so there is no extra focus-border-box
//around our control
pNMCustomDrawStruct->uItemState &= ~CDIS_FOCUS;
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
//custom paint the specified item
IsSelected = (GetFocus() == m_hwnd);
switch (pNMCustomDrawStruct->dwItemSpec)
{
//we only custom draw the channel and thumb - we leave ticks to the def wnd proc
case TBCD_CHANNEL:
DrawChannel(pNMCustomDrawStruct->hdc, IsSelected, &pNMCustomDrawStruct->rc);
return CDRF_SKIPDEFAULT;
case TBCD_THUMB:
DrawThumb(pNMCustomDrawStruct->hdc, IsSelected, &pNMCustomDrawStruct->rc);
return CDRF_SKIPDEFAULT;
default:
return CDRF_DODEFAULT;
}
default:
return 0;
}
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::DrawChannel
Helper function that draws the channel of the trackbar
------------------------------------------------------------------------------*/
HRESULT
TrackBarImpl_t::DrawChannel(
HDC hdc,
BOOL IsSelected,
__in const RECT* pRectangle
)
{
//load the background bitmap
HRESULT hr;
Bitmap_t& BitmapToUse = (IsSelected ? m_ChannelSelected : m_Channel);
if (!BitmapToUse)
{
hr = BitmapToUse.LoadBitmap(
GlobalData_t::s_ModuleInstance,
(IsSelected ? IDB_TRACKBAR_CHANNEL_SEL : IDB_TRACKBAR_CHANNEL)
);
if (FAILED(hr))
{
return hr;
}
}
PaintHelper_t paint;
hr = paint.Attach(hdc);
if (FAILED(hr))
{
return hr;
}
//Blt the bitmap to fit the rect
hr = paint.TileBlt(
&BitmapToUse,
pRectangle,
NULL,
Layout_t::TrackbarChannelTileLeft(),
Layout_t::TrackbarChannelTileTop(),
Layout_t::TrackbarChannelTileRight(),
Layout_t::TrackbarChannelTileBottom(),
Colors_t::DefaultTransparentColor()
);
if (FAILED(hr))
{
ASSERT(FALSE);
return hr;
}
return S_OK;
}
/*------------------------------------------------------------------------------
TrackBarImpl_t::DrawThumb
Draws the thumb of the trackbar
------------------------------------------------------------------------------*/
HRESULT
TrackBarImpl_t::DrawThumb(
HDC hdc,
BOOL IsSelected,
__in const RECT* pRectangle
)
{
HRESULT hr;
Bitmap_t& BitmapToUse = (IsSelected ? m_ThumbSelected : m_Thumb);
if (!BitmapToUse)
{
hr = BitmapToUse.LoadBitmap(
GlobalData_t::s_ModuleInstance,
(IsSelected ? IDB_TRACKBAR_THUMB_SEL : IDB_TRACKBAR_THUMB)
);
if (FAILED(hr))
{
return hr;
}
}
PaintHelper_t paint;
hr = paint.Attach(hdc);
if (FAILED(hr))
{
return hr;
}
//Blt the bitmap to fit the rect
hr = paint.TileBlt(
&BitmapToUse,
pRectangle,
NULL,
Layout_t::TrackbarThumbTileLeft(),
Layout_t::TrackbarThumbTileTop(),
Layout_t::TrackbarThumbTileRight(),
Layout_t::TrackbarThumbTileBottom(),
Colors_t::DefaultTransparentColor()
);
if (FAILED(hr))
{
ASSERT(FALSE);
return hr;
}
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -