forked from microsoft/WindowsAppSDK-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TxtHelper.h
123 lines (97 loc) · 3.08 KB
/
TxtHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//--------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// File: TxtHelper.h
//
// Text helper class for drawing txt
//
//--------------------------------------------------------------------------------------
#include <windows.h>
#include <strsafe.h>
class CTextHelper
{
public:
CTextHelper( ID3DXFont* pFont, ID3DXSprite* pSprite, int nLineHeight );
void SetInsertionPos( int x, int y ) { m_pt.x = x; m_pt.y = y; }
void SetForegroundColor( D3DXCOLOR clr ) { m_clr = clr; }
void Begin();
HRESULT DrawFormattedTextLine( const WCHAR* strMsg, ... );
HRESULT DrawTextLine( const WCHAR* strMsg );
HRESULT DrawFormattedTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg, ... );
HRESULT DrawTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg );
void End();
protected:
ID3DXFont* m_pFont;
ID3DXSprite* m_pSprite;
D3DXCOLOR m_clr;
POINT m_pt;
int m_nLineHeight;
};
//--------------------------------------------------------------------------------------
CTextHelper::CTextHelper( ID3DXFont* pFont, ID3DXSprite* pSprite, int nLineHeight )
{
m_pFont = pFont;
m_pSprite = pSprite;
m_clr = D3DXCOLOR(1,1,1,1);
m_pt.x = 0;
m_pt.y = 0;
m_nLineHeight = nLineHeight;
}
//--------------------------------------------------------------------------------------
HRESULT CTextHelper::DrawFormattedTextLine( const WCHAR* strMsg, ... )
{
WCHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
StringCchVPrintf( strBuffer, 512, strMsg, args );
strBuffer[511] = L'\0';
va_end(args);
return DrawTextLine( strBuffer );
}
//--------------------------------------------------------------------------------------
HRESULT CTextHelper::DrawTextLine( const WCHAR* strMsg )
{
if( NULL == m_pFont )
return E_INVALIDARG;
HRESULT hr;
RECT rc;
SetRect( &rc, m_pt.x, m_pt.y, 0, 0 );
hr = m_pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
if( FAILED(hr) )
return hr;
m_pt.y += m_nLineHeight;
return S_OK;
}
HRESULT CTextHelper::DrawFormattedTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg, ... )
{
WCHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
StringCchVPrintf( strBuffer, 512, strMsg, args );
strBuffer[511] = L'\0';
va_end(args);
return DrawTextLine( rc, dwFlags, strBuffer );
}
HRESULT CTextHelper::DrawTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg )
{
if( NULL == m_pFont )
return E_INVALIDARG;
HRESULT hr;
hr = m_pFont->DrawText( m_pSprite, strMsg, -1, &rc, dwFlags, m_clr );
if( FAILED(hr) )
return hr;
m_pt.y += m_nLineHeight;
return S_OK;
}
//--------------------------------------------------------------------------------------
void CTextHelper::Begin()
{
if( m_pSprite )
m_pSprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE );
}
void CTextHelper::End()
{
if( m_pSprite )
m_pSprite->End();
}