Manually draw text

This is what I presently have, the m_full merely holds the buffer length not the string length
(unless m_len happens to be the same when growth handler is used)
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
#ifndef WIDE_DEFINED
typedef wchar_t wide;
#define WIDE_DEFINED
#endif
typedef struct zxCHAR_struct
{
  char* m_text;
  uint  m_len;
  uint  m_full;
} zxCHAR;
typedef struct zxWIDE_struct
{
  wide* m_text;
  uint  m_len;
  uint  m_full;
} zxWIDE;
#ifdef UNICODE
typedef wide zxChar;
#define zxTEXT zxWIDE
#else
typedef char zxChar;
#define zxTEXT zxCHAR
#endif
uint zx_mswSetText( HWND hdl, zxTEXT* text, int line, int pos, HBRUSH bg )
{
  RECT cRect = {0}, rect = {0};
  POINT pt;
  TEXTMETRIC tm;
  HDC  dc = GetDC( NULL );
  if ( !dc )
    return 0u;
  GetWindowRect( hdl, &rect );
  GetClientRect( hdl, &cRect );
  pt.x = ( rect.right - rect.left ) - cRect.right;
  pt.y = ( rect.bottom - rect.top ) - cRect.bottom;
  rect.left   += pt.x;
  rect.right  -= pt.x;
  rect.top    += pt.y;
  rect.bottom -= pt.y;
  GetTextMetrics( dc, &tm );
  SetCaretPos( ( pos * tm.tmAveCharWidth ) - ( pt.x >> 1),
    ( line * tm.tmHeight ) + ( pt.y >> 1 ) );
  FillRect( dc, &rect, bg );
  DrawText( dc, text->m_text, text->m_len, &rect,
    DT_LEFT | DT_SINGLELINE | DT_END_ELLIPSIS | DT_VCENTER );
  ReleaseDC( NULL, dc );
  return 1u;
}

The caret still needs a bit of fine tuning but otherwise this seems to be perfect for forcing windows to produce text (I tried getting windows to handle it but that failed at every turn). I'm posting this just for people to use if they wish. If anyone fix's the caret before I do then feel free to post it.

*Note only been tested on an "EDIT" class.

Edit: Resolved dbl caret issue and got the caret as close to the text as possible.
Last edited on
Topic archived. No new replies allowed.