c++ - Fit string inside specified rectangle -


i have string needs drawn inside rectangle.

the problem lies in fact string can big fit inside.

how can adjust font size string can fit inside?

i have read docs gdi , found nothing. still keep searching on internet, hoping find or idea of own...

gdi+ option too...

the following code posted in response comment user jonathan potter:

#include <windows.h> #include <windowsx.h>    #include <commctrl.h> #include <stdio.h>      // swprintf_s() #include <math.h> #include <gdiplus.h> #include <string> using namespace gdiplus;  // enable visual styles #pragma comment( linker, "/manifestdependency:\"type='win32' \                          name='microsoft.windows.common-controls' version='6.0.0.0' \                          processorarchitecture='*' publickeytoken='6595b64144ccf1df' \                          language='*'\"")  // link common controls library #pragma comment(lib, "comctl32.lib")  #pragma comment(lib, "gdiplus.lib")  //global variables hinstance hinst;  // main window procedure lresult callback wndproc(hwnd hwnd, uint msg, wparam wparam, lparam lparam) {     switch (msg)     {     case wm_paint:     {         paintstruct ps = { 0 };         rect rcclient = { 0 };         hdc hdc = beginpaint(hwnd, &ps);          getclientrect(hwnd, &rcclient);          int pagewidth = rcclient.right - rcclient.left,             pageheight = rcclient.bottom - rcclient.top;          hfont font = null, oldfont = null;          // target rectangle, text should fit inside         rectangle(hdc, 0, 0, pagewidth / 4, pageheight / 10);          size sz;          gettextextentpoint32(hdc, l"this long string might not fit specified rectangle",             lstrlen(l"this long string might not fit specified rectangle"), &sz);          if (sz.cx > (pagewidth / 4))         {              // current font             logfont lf;             getobject(getcurrentobject(hdc, obj_font), sizeof(lf), &lf);              // scale             lf.lfheight = muldiv(lf.lfheight, (pagewidth / 4), sz.cx);              font = createfontindirect(&lf);              oldfont = selectfont(hdc, font);         }          setbkmode(hdc, transparent);         settextcolor(hdc, rgb(255, 0, 0));          // draw text in test rectangle          rect rctext = { 0 };          rctext.left = 0;         rctext.top = 0;         rctext.right = pagewidth / 4;         rctext.bottom = pageheight / 10;          drawtextex(hdc,             l"this long string might not fit specified rectangle",             wcslen(l"this long string might not fit specified rectangle"),             &rctext, dt_singleline | dt_center | dt_vcenter | dt_noclip, null);          if (font != null)         {             selectfont(hdc, oldfont);             deletefont(font);         }          endpaint(hwnd, &ps);     }         return 0l;     case wm_size:     {         invalidaterect(hwnd, null, true);     }         return 0l;     case wm_close:         ::destroywindow(hwnd);         return 0l;     case wm_destroy:     {         ::postquitmessage(0);     }         return 0l;     default:         return ::defwindowproc(hwnd, msg, wparam, lparam);     }     return 0; }  // winmain int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline,     int ncmdshow) {     // store hinstance in global variable later use     hinst = hinstance;      wndclassex wc;     hwnd hwnd;     msg msg;      // register main window class     wc.cbsize = sizeof(wndclassex);     wc.style = 0;     wc.lpfnwndproc = wndproc;     wc.cbclsextra = 0;     wc.cbwndextra = 0;     wc.hinstance = hinst;     wc.hicon = loadicon(hinstance, idi_application);     wc.hcursor = loadcursor(null, idc_arrow);     wc.hbrbackground = getsyscolorbrush(color_window);     wc.lpszmenuname = null;     wc.lpszclassname = l"main_window";     wc.hiconsm = loadicon(hinstance, idi_application);      if (!registerclassex(&wc))     {         messagebox(null, l"window registration failed!", l"error!",             mb_iconexclamation | mb_ok);          return 0;     }      // initialize common controls     initcommoncontrolsex iccex;     iccex.dwsize = sizeof(initcommoncontrolsex);     iccex.dwicc = icc_listview_classes | icc_standard_classes;     initcommoncontrolsex(&iccex);      gdiplusstartupinput gdiplusstartupinput;     ulong_ptr gdiplustoken;     gdiplusstartup(&gdiplustoken, &gdiplusstartupinput, null);      // create main window     hwnd = createwindowex(0, l"main_window", l"autofit text inside rectangle",         ws_overlappedwindow, 50, 50, 200, 200, null, null, hinstance, 0);      showwindow(hwnd, ncmdshow);     updatewindow(hwnd);      while (getmessage(&msg, null, 0, 0) > 0)     {         translatemessage(&msg);         dispatchmessage(&msg);     }      gdiplusshutdown(gdiplustoken);     return msg.wparam; } 

you're looking drawtext:

int drawtext(_in_     hdc     hdc,              _inout_  lpctstr lpchtext,              _in_     int     ncount,              _inout_  lprect  lprect,              _in_     uint    uformat             ); 

you specify rectangle, , ensures text not drawn outside of rectangle. has dt_calcrect flag if need calculate rectangle based on text , current selected font. or can use dt_end_ellipsis, dt_path_ellipsis or dt_word_ellipsis flag truncate drawing of text ellipsis added user can see when text longer rectangle.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -