Spinner
微調器是複合控制項,其中包含遞增按鈕、遞減按鈕和編輯控制項,全部都用來為應用程式提供十進位值。
詳細資料
下列螢幕擷取畫面說明功能區微調程式。
微調項屬性
功能區架構會定義 Spinner 控制項 的屬性索引鍵 集合。
一般而言,Spinner 屬性會在功能區 UI 中更新,方法是透過呼叫 IUIFramework::InvalidateUICommand 方法,使與控制項相關聯的命令失效。 無效事件是由 IUICommandHandler::UpdateProperty 回呼方法所定義的屬性更新處理。
IUICommandHandler::UpdateProperty回呼方法不會執行,而且應用程式會查詢更新的屬性值,直到架構需要屬性為止。 例如,當索引標籤啟動時,以及功能區 UI 中顯示的控制項,或顯示工具提示時。
注意
在某些情況下,可以透過 IUIFramework::GetUICommandProperty 方法擷取屬性,並使用 IUIFramework::SetUICommandProperty 方法進行設定。
下表列出與 Spinner 控制項相關聯的屬性索引鍵。
屬性索引鍵 | 備註 |
---|---|
UI_PKEY_DecimalPlaces | 只能透過失效來更新。 |
UI_PKEY_DecimalValue | 支援 IUIFramework::GetUICommandProperty 和 IUIFramework::SetUICommandProperty。
注意:如果與控制項相關聯的命令透過對 IUIFramework::InvalidateUICommand的呼叫失效,則當傳遞為旗標的值時 UI_INVALIDATIONS_VALUE ,架構會查詢此屬性。 |
UI_PKEY_Enabled | 支援 IUIFramework::GetUICommandProperty 和 IUIFramework::SetUICommandProperty。 |
UI_PKEY_FormatString | 只能透過失效來更新。 |
UI_PKEY_Increment | 只能透過失效來更新。 |
UI_PKEY_Keytip | 只能透過失效來更新。 |
UI_PKEY_Label | 只能透過失效來更新。 |
UI_PKEY_LargeHighContrastImage | 只能透過失效來更新。 |
UI_PKEY_LargeImage | 只能透過失效來更新。 |
UI_PKEY_MaxValue | 只能透過失效來更新。 |
UI_PKEY_MinValue | 只能透過失效來更新。 |
UI_PKEY_RepresentativeString | 只能透過失效來更新。 |
UI_PKEY_SmallHighContrastImage | 只能透過失效來更新。 |
UI_PKEY_SmallImage | 只能透過失效來更新。 |
UI_PKEY_TooltipDescription | 只能透過失效來更新。 |
UI_PKEY_TooltipTitle | 只能透過失效來更新。 |
下列程式碼區段示範如何在 IUICommandHandler::UpdateProperty 方法中更新 Spinner 控制項的各種屬性。
//
// FUNCTION: UpdateProperty()
//
// PURPOSE: Called by the Ribbon framework when a command property needs
// to be updated.
//
// COMMENTS: This function is used to provide new command property values for
// the spinner when requested by the Ribbon framework.
//
STDMETHODIMP CCommandHandler::UpdateProperty(
UINT nCmdID,
REFPROPERTYKEY key,
const PROPVARIANT* ppropvarCurrentValue,
PROPVARIANT* ppropvarNewValue)
{
UNREFERENCED_PARAMETER(ppropvarCurrentValue);
HRESULT hr = E_NOTIMPL;
if (nCmdID == IDR_CMD_SPINNER_RESIZE)
{
// Set the minimum value
if (IsEqualPropertyKey(key, UI_PKEY_MinValue))
{
ZeroMemory(ppropvarNewValue, sizeof(*ppropvarNewValue));
ppropvarNewValue->vt = VT_DECIMAL;
VarDecFromR8(-10.0, &ppropvarNewValue->decVal);
hr = S_OK;
}
// Set the maximum value
else if (IsEqualPropertyKey(key, UI_PKEY_MaxValue))
{
ZeroMemory(ppropvarNewValue, sizeof(*ppropvarNewValue));
ppropvarNewValue->vt = VT_DECIMAL;
VarDecFromR8(10.0, &ppropvarNewValue->decVal);
hr = S_OK;
}
// Set the increment
else if (IsEqualPropertyKey(key, UI_PKEY_Increment))
{
ZeroMemory(ppropvarNewValue, sizeof(*ppropvarNewValue));
ppropvarNewValue->vt = VT_DECIMAL;
VarDecFromR8(2.0, &ppropvarNewValue->decVal);
hr = S_OK;
}
// Set the number of decimal places
else if (IsEqualPropertyKey(key, UI_PKEY_DecimalPlaces))
{
hr = InitPropVariantFromUInt32(1, ppropvarNewValue);
hr = S_OK;
}
// Set the format string
else if (IsEqualPropertyKey(key, UI_PKEY_FormatString))
{
hr = InitPropVariantFromString(L"px", ppropvarNewValue);
hr = S_OK;
}
// Set the representative string
else if (IsEqualPropertyKey(key, UI_PKEY_RepresentativeString))
{
hr = InitPropVariantFromString(L"AAAAAAA", ppropvarNewValue);
hr = S_OK;
}
}
return hr;
}
備註
如果 Spinner 的最小值 (UI_PKEY_MinValue) 初始化為 0.0,則應用程式應該確保控制項所提供的任何後續值不等於 -0.0 (負零) 。 如果 Spinner 提供 -0.0 的值,應用程式應該使用 IUIFramework::SetUICommandProperty 方法,將此值重設為 0.0 (正零) ,如下列 IUICommandHandler::Execute 方法範例所示。
注意
如果未執行此測試,且保留未更正的值,控制項的編輯欄位會顯示字串 「Auto」。
//
// FUNCTION: Execute()
//
// PURPOSE: Called by the Ribbon framework when a command is executed by the user.
// For this sample, when an increment or decrement button is pressed or
// a new value is entered in the Spinner edit field.
//
STDMETHODIMP CCommandHandler::Execute(
UINT nCmdID,
UI_EXECUTIONVERB verb,
const PROPERTYKEY* key,
const PROPVARIANT* ppropvarValue,
IUISimplePropertySet* pCommandExecutionProperties)
{
UNREFERENCED_PARAMETER(pCommandExecutionProperties);
HRESULT hr = E_NOTIMPL;
if (verb == UI_EXECUTIONVERB_EXECUTE)
{
RenderParam param;
g_renderer.GetRenderParam(¶m);
if (nCmdID == IDR_CMD_SPINNER_RESIZE)
{
// Spinner value is negative.
if (!(ppropvarValue->decVal.sign == 0))
{
// Check if the value supplied by the Spinner is -0
// and correct the value if necessary.
// If this value is left uncorrected, the edit field
// of the control will display the string "Auto" when
// UI_PKEY_MinValue is set to 0.
if (ppropvarValue->decVal.Lo64 == 0)
{
// Initialize a new PROPVARIANT structure.
PROPVARIANT m_varNewVal;
PropVariantInit(&m_varNewVal);
// The replacement DECIMAL value.
DECIMAL m_dVal;
hr = VarDecFromI4(0, &m_dVal);
if (FAILED(hr))
{
return hr;
}
// Initialize the new DECIMAL value.
UIInitPropertyFromDecimal(UI_PKEY_DecimalValue, m_dVal, &m_varNewVal);
// Set the UI_PKEY_DecimalValue to the new DECIMAL value.
hr = g_pFramework->SetUICommandProperty(nCmdID, UI_PKEY_DecimalValue, m_varNewVal);
if (FAILED(hr))
{
return hr;
}
}
// Decrease size of shape in document space.
param.iShapeSizeIncrement = -ppropvarValue->intVal;
}
// Spinner value is positive.
else
{
// Increase size of shape in document space.
param.iShapeSizeIncrement = ppropvarValue->intVal;
}
}
g_renderer.UpdateRenderParam(param);
}
return hr;
}