Support Board
Date/Time: Tue, 26 Nov 2024 12:32:54 +0000
[Programming Help] - How to center text using sc.Graphics.DrawText?
View Count: 484
[2023-11-24 01:02:39] |
LudaTrades - Posts: 27 |
#include "sierrachart.h"
SCDLLName("OpenGL GDI Centered Text Test") // http://msdn.microsoft.com/en-nz/library/windows/desktop/dd145203%28v=vs.85%29.aspx void DrawToChart(HWND WindowHandle, HDC DeviceContext, SCStudyInterfaceRef sc); SCSFExport scsf_DrawToChartTest_CenteredText(SCStudyInterfaceRef sc) { if (sc.SetDefaults) { // Set the configuration and defaults sc.GraphName = "OpenGL GDI Centered Text Test"; sc.GraphRegion = 0; sc.AutoLoop = 0; return; } sc.p_GDIFunction = DrawToChart; } void DrawToChart(HWND WindowHandle, HDC DeviceContext, SCStudyInterfaceRef sc ) { //Text SCString TextToDraw; TextToDraw.Format("This text should be centered."); n_ACSIL::s_GraphicsFont GraphicsFont; n_ACSIL::s_GraphicsColor TextGraphicsColor; GraphicsFont.m_Height = 16; GraphicsFont.m_Weight = FW_BOLD; TextGraphicsColor.SetRGB(255, 255, 255); sc.Graphics.SetTextFont(GraphicsFont); sc.Graphics.SetTextAlign(TA_CENTER); sc.Graphics.SetTextColor(TextGraphicsColor); //Rectangle n_ACSIL::s_GraphicsRectangle TextRectangle; n_ACSIL::s_GraphicsColor BackgroundGraphicsColor; BackgroundGraphicsColor.SetRGB(64, 128, 0); sc.Graphics.SetBackgroundColor(BackgroundGraphicsColor); TextRectangle.Left = 100; TextRectangle.Top = 100; TextRectangle.Right = 500; TextRectangle.Bottom = 500; // Draw the text centered //int32_t (SCDLLCALL* DrawText)(const SCString& Text, n_ACSIL::s_GraphicsRectangle& r_Rectangle, const uint32_t Flags); int result = sc.Graphics.DrawText(TextToDraw, TextRectangle, DT_CENTER | DT_VCENTER | DT_SINGLELINE); return; } Using these center flags from the Microsoft GDI docs and also sc.Graphics.SetTextAlign but I'm unable to get the text to center. |
Sierra_openGL_GDI_centered_text.png / V - Attached On 2023-11-24 01:02:06 UTC - Size: 9.79 KB - 117 views |
[2023-11-24 21:06:20] |
norvik_ - Posts: 106 |
With winapi DT_VCENTER|DT_CENTER |DT_SINGLELINE flag should been used for centered text.
|
[2023-11-24 23:33:42] |
LudaTrades - Posts: 27 |
With winapi DT_VCENTER|DT_CENTER |DT_SINGLELINE flag should been used for centered text.
I did use these flags, they did not work. See the above code and screenshot. |
[2023-11-25 19:43:16] |
LudaTrades - Posts: 27 |
After some trial and error I got this to work with DrawTextInRectangle: //int32_t (SCDLLCALL* DrawTextInRectangle)(const SCString& Text, const int32_t TextAnchorX, const int32_t TextAnchorY, const n_ACSIL::s_GraphicsRectangle& Rectangle, const uint32_t Options);
sc.Graphics.DrawTextInRectangle(TextToDraw, TextAnchorX, TextAnchorY, TextRectangle, 2); Setting a 2 for the "Options" parameter filled the whole rectangle and I was able to center with custom X and Y anchors. The option must be set or the rectangle will only draw around the text and not the rectangle coordinates. |
[2023-11-26 20:15:21] |
User719512 - Posts: 264 |
Calculate the starting coordinates based on GetTextSize() and/or GetTextSizeWithFont() functions. For horizontally centered, subtract half from X.
|
To post a message in this thread, you need to log in with your Sierra Chart account: