Login Page - Create Account

Support Board


Date/Time: Sun, 09 Mar 2025 00:40:37 +0000



Post From: DrawText (winapi) problem in sc.p_GDIFunction, possible bug.

[2022-02-18 19:44:16]
User978306 - Posts: 4
Hi, Support.

Trying to create text output at MainPriceGraph, but can not..

Sample SierraChart study:

void  draw_text_function(HWND hwnd, HDC hdc, SCStudyInterfaceRef sc);

SCSFExport scsf_draw_text_test(SCStudyInterfaceRef sc)
{
  if (sc.SetDefaults)
  {
    sc.GraphName = "draw text test";

    sc.UpdateAlways = 1;
    sc.AutoLoop = 0;
    sc.GraphRegion = 0;
    sc.p_GDIFunction = draw_text_function;

    return;
  }
}

void draw_text_function(HWND hwnd, HDC hdc, SCStudyInterfaceRef sc)
{
  RECT rc;
  ::GetClientRect(hwnd, &rc);
  ::InflateRect(&rc, -300, -300);

  int old_mode = ::SetMapMode(hdc, MM_TEXT);

  TCHAR font_face[] = "Consolas";
  long font_height = -MulDiv(18, ::GetDeviceCaps(hdc, LOGPIXELSY), 72);

  HFONT font = ::CreateFontA(font_height, 0, 0, 0,
    FW_SEMIBOLD, 0, 0, 0, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
    DEFAULT_QUALITY, FF_DONTCARE | DEFAULT_PITCH, font_face);

  if (font == nullptr)
    sc.AddMessageToLog("Create study font operatoin error!", 1);

  HGDIOBJ old_font = ::SelectObject(hdc, font);
  std::stringstream ss;
  ss.str("test output string");

  int old_bk_mode = ::SetBkMode(hdc, TRANSPARENT);
  ::SetTextColor(hdc, RGB(255, 0, 0));

  int result = ::DrawTextA(hdc, ss.str().c_str(),
    (int)ss.str().length(), &rc, DT_VCENTER | DT_CENTER | DT_SINGLELINE);

  HPEN pen = ::CreatePen(PS_SOLID, 1, RGB(204, 204, 204));
  ::SelectObject(hdc, ::GetStockObject(NULL_BRUSH));
  HGDIOBJ old_pen = ::SelectObject(hdc, pen);

  Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);

  ::DeleteObject(font);
  ::DeleteObject(pen);
  
  ::SetBkMode(hdc, old_bk_mode);
  ::SelectObject(hdc, old_font);
  ::SelectObject(hdc, old_pen);
  ::SetMapMode(hdc, old_mode);

  ::DeleteObject(font);
  ::DeleteObject(pen);
}

First attachment contains screenshot of chart with applied study, can see empty rectangle only.
For check, I have created a template VS windows application project and placed inside WM_PAINT block absolutely same code from draw_text_function , have a correct output, can be found in second attachment.

Code:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
            RECT rc;
            GetClientRect(hWnd, &rc);
            InflateRect(&rc, -300, -300);

            WCHAR lpsz_face[] = L"Consolas";
            long font_height = -MulDiv(26, ::GetDeviceCaps(hdc, LOGPIXELSY), 72);

            HFONT font = ::CreateFontW(font_height, 0, 0, 0,
              FW_SEMIBOLD, 0, 0, 0, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
              CLEARTYPE_NATURAL_QUALITY, FF_DONTCARE | DEFAULT_PITCH, lpsz_face);

            HGDIOBJ old_font = ::SelectObject(hdc, font);
        
            std::wstringstream ss;
            ss << L"sample text output";

            int old_bk_mode = ::SetBkMode(hdc, TRANSPARENT);
            ::SetTextColor(hdc, RGB(255, 0, 0));

            int result = ::DrawTextW(hdc, (LPWSTR)ss.str().c_str(), (int)ss.str().length(),
              &rc, DT_CENTER| DT_VCENTER | DT_SINGLELINE);

            HPEN pen = ::CreatePen(PS_SOLID, 1, RGB(204, 204, 204));
            ::SelectObject(hdc, ::GetStockObject(NULL_BRUSH));
            ::SelectObject(hdc, pen);

            Rectangle(hdc, rc.left,rc.top,rc.right,rc.bottom);
            
            ::DeleteObject(font);
            ::DeleteObject(pen);

EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

imagefirst_attachment.png / V - Attached On 2022-02-18 19:38:28 UTC - Size: 14.29 KB - 179 views
imagesecond_attachment.png / V - Attached On 2022-02-18 19:40:57 UTC - Size: 32.57 KB - 183 views