DXUT中按钮使用de简易说明
大家好!
DXUT中按钮的使用
1. 首先,声明两个类型的指针,同时分配内存空间。
CDXUTDialogResourceManager *dialogManager = new CDXUTDialogResourceManager();
CDXUTDialog *dialog = new CDXUTDialog();
有一个非常有用的DXUT写好的UI类,对设置有很大帮助。
CD3DSettingsDlg *dialogSetting=new CD3DSettingsDlg();
2. 创建设备,Reset设备。
//在OnD3D9CreateDevice中创建设备
dialogSetting->OnD3D9CreateDevice(pd3dDevice);
dialogManager->OnD3D9CreateDevice(pd3dDevice);
// 在OnD3D9ResetDevice中Reset设备
dialogSetting->OnD3D9ResetDevice();
dialogManager->OnD3D9ResetDevice();
同时OnD3D9DestroyDevice中
dialogSetting->OnD3D9DestroyDevice();
dialogManager->OnD3D9DestroyDevice();
OnD3D9LostDevice中
dialogSetting->OnD3D9LostDevice();
dialogManager->OnD3D9LostDevice();
3. 写一个全局的函数InitDialog()用来初始化和设置(CDXUTDialog)dialog
//把dialogSetting和dialog交给dialogManager管理
dialogSetting->Init(dialogManager);
dialog->Init(dialogManager);
//然后添加Button,button1等式定义好的宏,int类型的值
dialog->AddButton(button1,L"窗口模式",0,0,120,25);
dialog->AddButton(button2,L"REF",0,25,120,25);
dialog->AddButton(button3,L"窗口模式",0,50,120,25);
dialog->AddCheckBox(cheakbox1,L"是否全屏?",0,75,120,25);
//调用回调函数处理各种点击事件
dialog->SetCallback(DialogEvent);
回调函数DialogEvent在DXUTgui中的定义
typedef VOID ( CALLBACK*PCALLBACKDXUTGUIEVENT )( UINT nEvent, int nControlID, CDXUTControl* pControl,void* pUserContext );
在winmain中调用InitDialog(),OnD3D9ResetDevice中设置dialog的位置和大小:
dialog->SetLocation(pBackBufferSurfaceDesc->Width-120,0);
dialog->SetSize(120,100);
在OnD3D9FrameRender中调用dialog->OnRender(fElapsedTime);到这里就可以显示按钮了。
4. 写出消息处理的回调函数DialogEvent
void CALLBACK DialogEvent(UINT nEvent, int nControlID, CDXUTControl* pControl,void* pUserContext)
{
//也可以对nEvent进行switch判断是哪一个事件
switch(nControlID)//对nControlID进行switch判断是点击了哪一个按钮
{
case button1:
::DXUTToggleFullScreen();
break;
case button2:
::DXUTToggleREF();
break;
case button3:
dialogSetting->SetActive(!dialogSetting->IsActive());
break;
case 4:
DXUTToggleFullScreen();
dialog->GetCheckBox(4)->SetChecked(::DXUTIsWindowed());
break;
}
}
MsgProc中设置消息的处理
*pbNoFurtherProcessing=dialogManager->MsgProc(hWnd,uMsg,wParam,lParam);
if(*pbNoFurtherProcessing)//如果有dialogManager的消息要先处理
return 0;
if(dialogS
etting->IsActive())//如果设置框在激活状态,处理dialogSetting消息
{
dialogSetting->MsgProc(hWnd,uMsg,wParam,lParam);
return 0;
}
*pbNoFurtherProcessing=dialog->MsgProc(hWnd,uMsg,wParam,lParam);
if(*pbNoFurtherProcessing)//最后有dialog的消息处理
return 0;
通过以上四部的设置就可以使用按钮并响应按钮事件了,但是对不同种类的按钮需要不同的操作,以下列举各种类型的按钮操作。
1. Button类型
这种比较简单,就如以上代码中的就行。
2. CheckBox类型 复选框
用的时候调用dialog->GetCheckBox(11)->GetChecked()判断是否被选中
3. ComboBox类型 下拉框
CDXUTComboBox *pCombo=new CDXUTComboBox();
dialog->AddComboBox(button3,0,50,120,25,0,false,&pCombo);
if(pCombo)
{
pCombo->SetDropHeight(100);
//可以通过这个指针做很多设置,其他类型的按钮也可以通过自己的这个指针对本按钮做设置
pCombo->AddItem( L"Combobox item (O)", ( LPVOID )0x11111111 );
pCombo->AddItem( L"Placeholder (O)", ( LPVOID )0x12121212 );
pCombo->AddItem( L"One more (O)", ( LPVOID )0x13131313 );
pCombo->AddItem( L"I can't get enough (O)", ( LPVOID )0x14141414 );
pCombo->AddItem( L"Ok, last one, I promise (O)", ( LPVOID )0x15151515 );
}
用的时候得到索引值,索引值从0开始
switch(dialog->GetComboBox(ComboBox)->GetSelectedIndex())
case 0:…………
case 1:…………
4. ListBox类型 列表框
用法同上ComboBox类型
5. EditBox类型 文本编辑框
HRESULT CDXUTDialog::AddEditBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault,CDXUTEditBox** ppCreated )
先判断EVENT_EDITBOX_STRINGS事件
if(EVENT_EDITBOX_STRINGS)//就是输入文字按下回车的事件
要用到输入文本的话dialog->GetEditBox(id)->GetText();
6. RadioButton类型 关联的单选按钮
HRESULT CDXUTDialog::AddRadioButton( int ID, UINT nButtonGroup, LPCWSTR strText, int x, int y, int width, int height, bool bChecked, UINT nHotkey, bool bIsDefault, CDXUTRadioButton** ppCreated )
可以设置多个RadioButton然后通过UINT nButtonGroup设置为一组,每组中只能有一个按钮处在被选中状态。
使用方法和checkbox类似。
7. Slider滑块类型
HRESULT CDXUTDialog::AddSlider( int ID, int x, int y, int width, int height, int min, int max, int value,bool bIsDefault, CDXUTSlider** ppCreated )
拨动滑块时可以通过dialog->GetSlider(id)->GetValue()得到当前的值。
8. Static 类型 静态文本
HRESULT CDXUTDialog::AddStatic( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault,CDXUTStatic** ppCreated )
dialog->AddStatic(9,"静态输出",0,220,120,25);
可以使用dialog->GetStatic(id)->SetText(LPWCSTR)设置静态输出的值,这个可以和以上的按钮文本关联起来。
以下是DXUTgui中的各种事件,有待研究。
#define EVENT_BUTTON_CLICKED 0x0101
#define EVENT_COMBOBOX_SELECTION_CHANGED 0x0201
#define EVENT_RADIOBUTTON_CHANGED 0x0301
#define EVENT_CHECKBOX_CHANGED 0x0401
#define EVENT_SLIDER_VALUE_CHANGED 0x0501
#define EVENT_EDITBOX_STRING 0x0601
// EVENT_EDITBOX_CHANGE is sent when the listbox content changes
// due to user input.
#define EVENT_EDITBOX_CHANGE 0x0602
#define EVENT_LISTBOX_ITEM_DBLCLK 0x0701
// EVENT_LISTBOX_SELECTION is fired off when the selection changes in
// a single selection list box.
#define EVENT_LISTBOX_SELECTION 0x0702
#define EVENT_LISTBOX_SELECTION_END 0x0703