分配了一个熟悉Slider控件使用的任务,这两天在折腾Slider,其实它就是一个滑动条,用于调节某范围的数值,值的类型是float。为了显示滑动时数值的变化,我还做了一个TextBlock控件,当Slider滑动的时候,TextBlock就显示出相应的数值。
除此之外,还要相应滚轮按键的消息。滚轮向左或者向右滑动,滑块也随着滑动,文本框的数值也跟着变化。
下面贴出代码来:
SliderTest.h文件如下:
#include
//#include
#include "pwinuser.h"
#include "xamlruntime.h"
#include "xrdelegate.h"
#include "xrptr.h"
#include
void FloatToWString(float num ,std::wstring *s)
{
std::wstringstream wss;
wss << num;
wss >> *s;
}
class SliderEventHandler
{
protected:
IXRSliderPtr Slider;
IXRTextBlockPtr TextBlock;
public:
SliderEventHandler(IXRSliderPtr& S,IXRTextBlockPtr& T)
{
Slider=S;
TextBlock=T;
}
HRESULT Slider_Change(IXRDependencyObject* source,XRValueChangedEventArgs* args)
{
//MessageBox(NULL,TEXT("Slider_Change!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);
RETAILMSG(1,(TEXT("ValueChange \r\n")));
float f_value=0;
Slider->GetValue(&f_value);
//RETAILMSG(1,(TEXT("f_value = %.9f \r\n"),f_value));
std::wstring ss;
FloatToWString(f_value,&ss);
TextBlock->SetText(ss.c_str());
RETAILMSG(1,(TEXT("ss = %s \r\n"),ss.c_str()));
return S_OK;
}
HRESULT Slider_KeyDown(IXRDependencyObject* source,XRKeyEventArgs* args)
{
RETAILMSG(1,(TEXT("XRKeyEventArgs.Handled= %d \r\n"),args->Handled));
RETAILMSG(1,(TEXT("XRKeyEventArgs.PlatformKeyCode= %d \r\n"),args->PlatformKeyCode));
//MessageBox(NULL,TEXT("Slider_KeyDown!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);
RETAILMSG(1,(TEXT("KeyDown \r\n")));
float f_value=0;
Slider->GetValue(&f_value);
//RETAILMSG(1,(TEXT("f_value = %.9f \r\n"),f_value));
std::wstring ss;
FloatToWString(f_value,&ss);
RETAILMSG(1,(TEXT("ss = %s \r\n"),ss.c_str()));
//针对滚轮左键
//if(args->PlatformKeyCode == 38)
//{
// RETAILMSG(1,(TEXT("PlatformKeyCode==38***************** \r\n")));
// //Slider->SetValue(max(f_value-0.2,0));
// Slider->SetValue(f_value-0.2);
// //Slider->SetValue(min(f_value+0.1,100));
// Slider->GetValue(&f_value);
// //RETAILMSG(1,(TEXT("f_value = %.9f \r\n"),f_value));
// std::wstring ss;
// FloatToWString(f_value,&ss);
// RETAILMSG(1,(TEXT("ss = %s \r\n"),ss.c_str()));
//}
//针对滚轮右键
//else if (args->PlatformKeyCode == 40)
//{
// RETAILMSG(1,(TEXT("PlatformKeyCode==40***************** \r\n")));
// //Slider->SetValue(min(f_value+0.2,100));
// Slider->SetValue(f_value+0.2);
// //Slider->SetValue(max(f_value-0.1,0));
// Slider->GetValue(&f_value);
// //RETAILMSG(1,(TEXT("f_value = %.9f \r\n"),f_value));
// std::wstring ss;
// FloatToWString(f_value,&ss);
// RETAILMSG(1,(TEXT("ss = %s \r\n"),ss.c_str()));
//}
//
return S_OK;
}
HRESULT Slider_KeyUp(IXRDependencyObject* source,XRKeyEventArgs* args)
{
RETAILMSG(1,(TEXT("XRKeyEventArgs.Handled= %d \r\n"),args->Handled));
RETAILMSG(1,(TEXT("XRKeyEventArgs.PlatformKeyCode= %d \r\n"),args->PlatformKeyCode));
//MessageBox(NULL,TEXT("Slider_KeyUp!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);
RETAILMSG(1,(TEXT("KeyUp \r\n")));
float f_value=0;
Slider->GetValue(&f_value);
//RETAILMSG(1,(TEXT("f_value = %.9f \r\n"),f_value));
std::wstring ss;
FloatToWString(f_value,&ss);
RETAILMSG(1,(TEXT("ss = %s \r\n"),ss.c_str()));
return S_OK;
}
};
SliderTest.cpp文件如下:
#include "SliderTest.h"
#pragma comment(lib, "xamlruntime.lib")
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
if (!XamlRuntimeInitialize())
{
return -1;
}
RETAILMSG(1, (TEXT("1\r\n")));
HRESULT retcode;
IXRApplicationPtr pApplication = NULL;
if (FAILED(retcode=GetXRApplicationInstance(&pApplication)))
{
return -1;
}
RETAILMSG(1, (TEXT("2\r\n")));
XRXamlSource xamlsrc;
//xamlsrc.SetFile(L"");
xamlsrc.SetFile(L" Card\\Page.xaml");
//xamlsrc.SetFile(L"");
RETAILMSG(1, (TEXT("3\r\n")));
XRWindowCreateParams wp;
ZeroMemory(&wp, sizeof(XRWindowCreateParams));
wp.Style = WS_OVERLAPPED | WS_SYSMENU;
wp.pTitle = L"Slider Test";
wp.Left = 0;
wp.Top = 0;
RETAILMSG(1, (TEXT("4\r\n")));
IXRVisualHostPtr vhost = NULL;
if (FAILED(retcode=pApplication->CreateHostFromXaml(&xamlsrc, &wp, &vhost)))
{
return -1;
}
RETAILMSG(1, (TEXT("5\r\n")));
IXRFrameworkElementPtr root;
if (FAILED(retcode=vhost->GetRootElement(&root)))
{
return -1;
}
RETAILMSG(1, (TEXT("6\r\n")));
IXRSliderPtr Slider;
if (FAILED(retcode=root->FindName(TEXT("Slider"), &Slider)))
{
return -1;
}
RETAILMSG(1, (TEXT("7\r\n")));
Slider->SetMinimum(0);
Slider->SetMaximum(100);
Slider->SetValue(50);
// to transfer the UI focus to this control.
bool bvalue=0;
Slider->Focus(&bvalue);
IXRTextBlockPtr TextBlock;
if (FAILED(retcode=root->FindName(TEXT("TextBlock"), &TextBlock)))
{
return -1;
}
RETAILMSG(1, (TEXT("8\r\n")));
TextBlock->SetText(L"50");
//***************************************************************************
SliderEventHandler handlerSlider(Slider,TextBlock);
//SliderEventHandler handlerSlider;
IXRDelegate>* clickdelegateSlider;
if (FAILED(retcode=CreateDelegate(&handlerSlider,&SliderEventHandler::Slider_Change,&clickdelegateSlider)))
{
return -1;
}
if (FAILED(retcode=Slider->AddValueChangedEventHandler(clickdelegateSlider)))
{
return -1;
}
IXRDelegate* keydowndelegateSlider;
if (FAILED(retcode=CreateDelegate(&handlerSlider,&SliderEventHandler::Slider_KeyDown,&keydowndelegateSlider)))
{
return -1;
}
if (FAILED(retcode=Slider->AddKeyDownEventHandler(keydowndelegateSlider)))
{
return -1;
}
IXRDelegate* keyupdelegateSlider;
if (FAILED(retcode=CreateDelegate(&handlerSlider,&SliderEventHandler::Slider_KeyUp,&keyupdelegateSlider)))
{
return -1;
}
if (FAILED(retcode=Slider->AddKeyUpEventHandler(keyupdelegateSlider)))
{
return -1;
}
//***************************************************************************
UINT exitcode;
if (FAILED(retcode=vhost->StartDialog(&exitcode)))
{
return -1;
}
clickdelegateSlider->Release( );
keydowndelegateSlider->Release( );
keyupdelegateSlider->Release( );
return 0;
}
问题:
Slider_KeyDown这个成员函数有点问题。就是微软的这个Slider控件很智能。我还没有添加滚轮键消息处理,此控件就已经可以响应滚轮键左键右键的消息(向左是增加数值0.1,向右是减小数值0.1)。这个真是太牛了。。。但是我想实现向左减小数值,向右怎增大数值,于是我就增加了滚轮按键消息处理函数Slider_KeyDown。在这个函数中我判断是左键还是右键,如果是左键我就减小0.2,如果是右键我就增加0.2.这样就可以实现向左减小0.1向右增加0.1.
但是。。。还有问题。。。我只能调节到0.1~99.9范围,而不是0~100.郁闷啊。。。微软太智能了。。。只要滚轮键一动,他就会响应自带的消息处理,也会响应我增加的消息处理。。。这个问题还是没有解决。。。有会的帮帮忙啊,不胜感激~~~