📄 dxutgui.cpp
字号:
WCHAR wszOutput[256];
wcsncpy( wszOutput, m_wszCaption, 256 );
wszOutput[255] = 0;
if( m_bMinimized )
wcsncat( wszOutput, L" (Minimized)", 256 - lstrlenW( wszOutput ) );
DrawText( wszOutput, &m_CapElement, &rc, true );
}
// If the dialog is minimized, skip rendering
// its controls.
if( !m_bMinimized )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt(i);
// Focused control is drawn last
if( pControl == s_pControlFocus )
continue;
pControl->Render( pd3dDevice, fElapsedTime );
}
if( s_pControlFocus != NULL && s_pControlFocus->m_pDialog == this )
s_pControlFocus->Render( pd3dDevice, fElapsedTime );
}
DXUTGetGlobalDialogResourceManager()->m_pSprite->End();
DXUTGetGlobalDialogResourceManager()->m_pStateBlock->Apply();
return S_OK;
}
//--------------------------------------------------------------------------------------
VOID CDXUTDialog::SendEvent( UINT nEvent, bool bTriggeredByUser, CDXUTControl* pControl )
{
// If no callback has been registered there's nowhere to send the event to
if( m_pCallbackEvent == NULL )
return;
// Discard events triggered programatically if these types of events haven't been
// enabled
if( !bTriggeredByUser && !m_bNonUserEvents )
return;
m_pCallbackEvent( nEvent, pControl->GetID(), pControl );
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddFont( LPCWSTR strFaceName, LONG height, LONG weight )
{
// See if this font already exists
for( int i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt(i);
if( 0 == _wcsnicmp( pFontNode->strFace, strFaceName, MAX_PATH-1 ) &&
pFontNode->nHeight == height &&
pFontNode->nWeight == weight )
{
return i;
}
}
// Add a new font and try to create it
DXUTFontNode* pNewFontNode = new DXUTFontNode();
if( pNewFontNode == NULL )
return -1;
ZeroMemory( pNewFontNode, sizeof(DXUTFontNode) );
wcsncpy( pNewFontNode->strFace, strFaceName, MAX_PATH-1 );
pNewFontNode->nHeight = height;
pNewFontNode->nWeight = weight;
m_FontCache.Add( pNewFontNode );
int iFont = m_FontCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3dDevice )
CreateFont( iFont );
return iFont;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetFont( UINT index, LPCWSTR strFaceName, LONG height, LONG weight )
{
// Make sure the list is at least as large as the index being set
UINT i;
for( i=m_Fonts.GetSize(); i <= index; i++ )
{
m_Fonts.Add( -1 );
}
int iFont = DXUTGetGlobalDialogResourceManager()->AddFont( strFaceName, height, weight );
m_Fonts.SetAt( index, iFont );
return S_OK;
}
//--------------------------------------------------------------------------------------
DXUTFontNode* CDXUTDialog::GetFont( UINT index )
{
if( NULL == DXUTGetGlobalDialogResourceManager() )
return NULL;
return DXUTGetGlobalDialogResourceManager()->GetFontNode( m_Fonts.GetAt( index ) );
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddTexture( LPCWSTR strFilename )
{
// See if this texture already exists
for( int i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
if( 0 == _wcsnicmp( pTextureNode->strFilename, strFilename, MAX_PATH-1 ) )
{
return i;
}
}
// Add a new texture and try to create it
DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
if( pNewTextureNode == NULL )
return -1;
ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
wcsncpy( pNewTextureNode->strFilename, strFilename, MAX_PATH-1 );
m_TextureCache.Add( pNewTextureNode );
int iTexture = m_TextureCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3dDevice )
CreateTexture( iTexture );
return iTexture;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strFilename )
{
// Make sure the list is at least as large as the index being set
for( UINT i=m_Textures.GetSize(); i <= index; i++ )
{
m_Textures.Add( -1 );
}
int iTexture = DXUTGetGlobalDialogResourceManager()->AddTexture( strFilename );
m_Textures.SetAt( index, iTexture );
return S_OK;
}
//--------------------------------------------------------------------------------------
DXUTTextureNode* CDXUTDialog::GetTexture( UINT index )
{
if( NULL == DXUTGetGlobalDialogResourceManager() )
return NULL;
return DXUTGetGlobalDialogResourceManager()->GetTextureNode( m_Textures.GetAt( index ) );
}
//--------------------------------------------------------------------------------------
bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
bool bHandled = false;
// If caption is enable, check for clicks in the caption area.
if( m_bCaption )
{
static bool bDrag;
if( uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK )
{
POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
if( mousePoint.x >= m_x && mousePoint.x < m_x + m_width &&
mousePoint.y >= m_y && mousePoint.y < m_y + m_nCaptionHeight )
{
bDrag = true;
SetCapture( DXUTGetHWND() );
return true;
}
} else
if( uMsg == WM_LBUTTONUP && bDrag )
{
POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
if( mousePoint.x >= m_x && mousePoint.x < m_x + m_width &&
mousePoint.y >= m_y && mousePoint.y < m_y + m_nCaptionHeight )
{
ReleaseCapture();
bDrag = false;
m_bMinimized = !m_bMinimized;
return true;
}
}
}
// If the dialog is minimized, don't send any messages to controls.
if( m_bMinimized )
return false;
// If a control is in focus, it belongs to this dialog, and it's enabled, then give
// it the first chance at handling the message.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
// If the control MsgProc handles it, then we don't.
if( s_pControlFocus->MsgProc( uMsg, wParam, lParam ) )
return true;
}
switch( uMsg )
{
case WM_ACTIVATEAPP:
// Call OnFocusIn()/OnFocusOut() of the control that currently has the focus
// as the application is activated/deactivated. This matches the Windows
// behavior.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
if( wParam )
s_pControlFocus->OnFocusIn();
else
s_pControlFocus->OnFocusOut();
}
break;
// Keyboard messages
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
{
// If a control is in focus, it belongs to this dialog, and it's enabled, then give
// it the first chance at handling the message.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
if( s_pControlFocus->HandleKeyboard( uMsg, wParam, lParam ) )
return true;
}
// Not yet handled, see if this matches a control's hotkey
// Activate the hotkey if the focus doesn't belong to an
// edit box.
if( uMsg == WM_KEYUP && ( !s_pControlFocus ||
( s_pControlFocus->GetType() != DXUT_CONTROL_EDITBOX
&& s_pControlFocus->GetType() != DXUT_CONTROL_IMEEDITBOX ) ) )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt( i );
if( pControl->GetHotkey() == wParam )
{
pControl->OnHotkey();
return true;
}
}
}
// Not yet handled, check for focus messages
if( uMsg == WM_KEYDOWN )
{
// If keyboard input is not enabled, this message should be ignored
if( !m_bKeyboardInput )
return false;
switch( wParam )
{
case VK_RIGHT:
case VK_DOWN:
if( s_pControlFocus != NULL )
{
OnCycleFocus( true );
return true;
}
break;
case VK_LEFT:
case VK_UP:
if( s_pControlFocus != NULL )
{
OnCycleFocus( false );
return true;
}
break;
case VK_TAB:
if( s_pControlFocus == NULL )
{
FocusDefaultControl();
}
else
{
bool bShiftDown = ((GetAsyncKeyState( VK_SHIFT ) & 0x8000) != 0);
OnCycleFocus( !bShiftDown );
}
return true;
}
}
break;
}
// Mouse messages
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_XBUTTONDBLCLK:
case WM_MOUSEWHEEL:
{
// If not accepting mouse input, return false to indicate the message should still
// be handled by the application (usually to move the camera).
if( !m_bMouseInput )
return false;
POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
mousePoint.x -= m_x;
mousePoint.y -= m_y;
// If caption is enabled, offset the Y coordinate by the negative of its height.
if( m_bCaption )
mousePoint.y -= m_nCaptionHeight;
// If a control is in focus, it belongs to this dialog, and it's enabled, then give
// it the first chance at handling the message.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
if( s_pControlFocus->HandleMouse( uMsg, mousePoint, wParam, lParam ) )
return true;
}
// Not yet handled, see if the mouse is over any controls
CDXUTControl* pControl = GetControlAtPoint( mousePoint );
if( pControl != NULL && pControl->GetEnabled() )
{
bHandled = pControl->HandleMouse( uMsg, mousePoint, wParam, lParam );
if( bHandled )
return true;
}
else
{
// Mouse not over any controls in this dialog, if there was a control
// which had focus it just lost it
if( uMsg == WM_LBUTTONDOWN &&
s_pControlFocus &&
s_pControlFocus->m_pDialog == this )
{
s_pControlFocus->OnFocusOut();
s_pControlFocus = NULL;
}
}
// Still not handled, hand this off to the dialog. Return false to indicate the
// message should still be handled by the application (usually to move the camera).
switch( uMsg )
{
case WM_MOUSEMOVE:
OnMouseMove( mousePoint );
return false;
}
break;
}
}
return false;
}
//--------------------------------------------------------------------------------------
CDXUTControl* CDXUTDialog::GetControlAtPoint( POINT pt )
{
// Search through all child controls for the first one which
// contains the mouse point
for( int i=0; i < m_Controls.GetSize(); i++ )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -