DXUT 示例中是用DXUTRes.cpp 中的 g_DXUTGUITextureSrcData存储控件图片内存数据, 然后导入程序使用.
该g_DXUTGUITextureSrcData中的数据是从dxutcontrols.dds转换过来的. dxutcontrols.dds可以到directx 安装目录里搜到.
使用自己的控件图片
其中添加了圆和箭头按钮
第一步
在DXUTgui.h中 CDXUTDialogResourceManager类中添加函数声明
int AddControlsTexture( LPCWSTR strFilename );
第二步
在DXUTgui.cpp中添加函数实现
int CDXUTDialogResourceManager::AddControlsTexture( LPCWSTR strFilename )
{
DXUTTextureNode* pNewTextureNode = new DXUTTextureNode;
if( pNewTextureNode == NULL )
return -1;
ZeroMemory( pNewTextureNode, sizeof( DXUTTextureNode ) );
pNewTextureNode->bFileSource = true;
wcscpy_s( pNewTextureNode->strFilename, MAX_PATH, strFilename );
m_TextureCache.GetAt(0) = pNewTextureNode;
if( m_pd3d9Device )
CreateTexture9( 0 );
return 0;
}
第三步
添加自己的控件函数(以箭头按钮举例)
在DXUTgui.h中CDXUTDialog类的声明中添加
HRESULT AddArrowButton( int ID, LPCWSTR strText, int x, int y, int width, int height, UINT nHotkey=0 /*热键*/ ,//按钮
bool bIsDefault=false/* 默认焦点*/, CDXUTArrowButton** ppCreated=NULL );
在DXUTgui.cpp中添加实现
HRESULT CDXUTDialog::AddArrowButton( int ID, LPCWSTR strText, int x, int y, int width, int height, UINT nHotkey,
bool bIsDefault, CDXUTArrowButton** ppCreated )
{
HRESULT hr = S_OK;
CDXUTArrowButton* pButton = new CDXUTArrowButton( this );
if( ppCreated != NULL )
*ppCreated = pButton;
if( pButton == NULL )
return E_OUTOFMEMORY;
hr = AddControl( pButton );
if( FAILED( hr ) )
return hr;
// Set the ID and list index
pButton->SetID( ID );
pButton->SetText( strText );
pButton->SetLocation( x, y );
pButton->SetSize( width, height );
pButton->SetHotkey( nHotkey );
pButton->m_bIsDefault = bIsDefault;
return S_OK;
}
在DXUTgui.h中添加
class CDXUTArrowButton : public CDXUTStatic
{
public:
CDXUTArrowButton( CDXUTDialog* pDialog = NULL );
virtual bool HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam );
virtual bool HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam );
virtual void OnHotkey()
{
if( m_pDialog->IsKeyboardInputEnabled() ) m_pDialog->RequestFocus( this );
m_pDialog->SendEvent( EVENT_BUTTON_CLICKED, true, this );
}
virtual BOOL ContainsPoint( POINT pt )
{
return PtInRect( &m_rcBoundingBox, pt );
}
virtual bool CanHaveFocus()
{
return ( m_bVisible && m_bEnabled );
}
virtual void Render( float fElapsedTime );
protected:
bool m_bPressed;
};
在DXUTgui.cpp中添加实现
CDXUTArrowButton::CDXUTArrowButton( CDXUTDialog* pDialog )
{
m_Type = DXUT_CONTROL_ARROWBUTTON;
m_pDialog = pDialog;
m_bPressed = false;
m_nHotkey = 0;
}
bool CDXUTArrowButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( !m_bEnabled || !m_bVisible )
return false;
switch( uMsg )
{
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_SPACE:
m_bPressed = true;
return true;
}
}
case WM_KEYUP:
{
switch( wParam )
{
case VK_SPACE:
if( m_bPressed == true )
{
m_bPressed = false;
m_pDialog->SendEvent( EVENT_BUTTON_CLICKED, true, this );
}
return true;
}
}
}
return false;
}
bool CDXUTArrowButton::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam )
{
if( !m_bEnabled || !m_bVisible )
return false;
switch( uMsg )
{
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
{
if( ContainsPoint( pt ) )
{
// Pressed while inside the control
m_bPressed = true;
SetCapture( DXUTGetHWND() );
if( !m_bHasFocus )
m_pDialog->RequestFocus( this );
return true;
}
break;
}
case WM_LBUTTONUP:
{
if( m_bPressed )
{
m_bPressed = false;
ReleaseCapture();
if( !m_pDialog->m_bKeyboardInput )
m_pDialog->ClearFocus();
// Button click
if( ContainsPoint( pt ) )
m_pDialog->SendEvent( EVENT_BUTTON_CLICKED, true, this );
return true;
}
break;
}
};
return false;
}
void CDXUTArrowButton::Render( float fElapsedTime )
{
int nOffsetX = 0;
int nOffsetY = 0;
DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL;
if( m_bVisible == false )
{
iState = DXUT_STATE_HIDDEN;
}
else if( m_bEnabled == false )
{
iState = DXUT_STATE_DISABLED;
}
else if( m_bPressed )
{
iState = DXUT_STATE_PRESSED;
nOffsetX = 1;
nOffsetY = 2;
}
else if( m_bMouseOver )
{
iState = DXUT_STATE_MOUSEOVER;
nOffsetX = -1;
nOffsetY = -2;
}
else if( m_bHasFocus )
{
iState = DXUT_STATE_FOCUS;
}
// Background fill layer
//TODO: remove magic numbers
CDXUTElement* pElement = m_Elements.GetAt( 0 );
float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f;
RECT rcWindow = m_rcBoundingBox;
OffsetRect( &rcWindow, nOffsetX, nOffsetY );
// Blend current color
pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate );
pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate );
m_pDialog->DrawSprite( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );
m_pDialog->DrawText( m_strText, pElement, &rcWindow );
// Main button
pElement = m_Elements.GetAt( 1 );
// Blend current color
pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate );
pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate );
m_pDialog->DrawSprite( pElement, &rcWindow, DXUT_NEAR_BUTTON_DEPTH );
m_pDialog->DrawText( m_strText, pElement, &rcWindow );
}
在CDXUTDialog::InitDefaultElements()中添加
//箭头
SetRect( &rcTexture, 270, 20, 380, 54 );
Element.SetTexture( 0, &rcTexture );
Element.SetFont( 0 );
Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB( 150, 255, 255, 255 );
Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB( 200, 255, 255, 255 );
Element.FontColor.States[ DXUT_STATE_MOUSEOVER ] = D3DCOLOR_ARGB( 255, 0, 0, 0 );
// Assign the Element
SetDefaultElement( DXUT_CONTROL_ARROWBUTTON, 0, &Element );
//-------------------------------------
// - Fill layer划过时的图片
//-------------------------------------
SetRect( &rcTexture, 380, 20, 490, 54 );
Element.SetTexture( 0, &rcTexture, D3DCOLOR_ARGB( 0, 255, 255, 255 ) );
Element.TextureColor.States[ DXUT_STATE_MOUSEOVER ] = D3DCOLOR_ARGB( 160, 255, 255, 255 );
Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB( 60, 0, 0, 0 );
Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB( 30, 255, 255, 255 );
// Assign the Element
SetDefaultElement( DXUT_CONTROL_ARROWBUTTON, 1, &Element );//DXUT_CONTROL_ARROWBUTTON在DXUTgui.h的enum DXUT_CONTROL_TYPE中添加
使用自己控件图片
在SimpleSample.cpp中的void InitApp()中添加代码如下
g_SettingsDlg.Init( &g_DialogResourceManager );//原有代码
g_HUD.Init( &g_DialogResourceManager );//原有代码
g_SampleUI.Init( &g_DialogResourceManager );//原有代码
g_DialogResourceManager.AddControlsTexture(L"tomb.dds");//控件图片
g_HUD.AddArrowButton( IDC_TEMPGRAPH, L"曲 线 图",730, 70, 100, 50, 0,true);