2 커밋 76cac8a629 ... 12cde80804

작성자 SHA1 메시지 날짜
  duanjianjun 12cde80804 Merge branch 'master' of http://gitz.zhixinhuixue.net:18880/zxhx-client-tool/pdfproject 2 년 전
  duanjianjun dbd0905b4b 字符串宽高 2 년 전
2개의 변경된 파일61개의 추가작업 그리고 0개의 파일을 삭제
  1. 59 0
      MFCApplication1/CvxText.cpp
  2. 2 0
      MFCApplication1/CvxText.h

+ 59 - 0
MFCApplication1/CvxText.cpp

@@ -12,6 +12,65 @@ void GetStringSize(HDC hDC, const char* str, int* w, int* h)
 	if (h != 0) *h = size.cy;
 }
 
+cv::Size GetTextSize(const char* str, int fontSize, const char* fn, bool italic, bool underline)
+{
+	HDC hDC = CreateCompatibleDC(0);
+	LOGFONTA lf;
+	lf.lfHeight = -fontSize;
+	lf.lfWidth = 0;
+	lf.lfEscapement = 0;
+	lf.lfOrientation = 0;
+	lf.lfWeight = 5;
+	lf.lfItalic = italic;   //斜体
+	lf.lfUnderline = underline; //下划线
+	lf.lfStrikeOut = 0;
+	lf.lfCharSet = DEFAULT_CHARSET;
+	lf.lfOutPrecision = 0;
+	lf.lfClipPrecision = 0;
+	lf.lfQuality = PROOF_QUALITY;
+	lf.lfPitchAndFamily = 0;
+	strcpy_s(lf.lfFaceName, fn);
+
+	HFONT hf = CreateFontIndirectA(&lf);
+	HFONT hOldFont = (HFONT)SelectObject(hDC, hf);
+
+
+	int strBaseW = 0, strBaseH = 0;
+	int singleRow = 0;
+	char buf[1 << 12];
+	strcpy_s(buf, str);
+	char *bufT[1 << 12];  // 这个用于分隔字符串后剩余的字符,可能会超出。
+	//处理多行
+	{
+		int nnh = 0;
+		int cw, ch;
+
+		const char* ln = strtok_s(buf, "\n", bufT);
+		while (ln != 0)
+		{
+			GetStringSize(hDC, ln, &cw, &ch);
+			strBaseW = max(strBaseW, cw);
+			strBaseH = max(strBaseH, ch);
+
+			ln = strtok_s(0, "\n", bufT);
+			nnh++;
+		}
+		singleRow = strBaseH;
+		strBaseH *= nnh;
+
+	}
+
+	SelectObject(hDC, hOldFont);
+	DeleteObject(hf);
+	DeleteDC(hDC);
+
+	cv::Size size;
+	size.width = strBaseW;
+	size.height = strBaseH;
+	return size;
+}
+
+
 void putTextZH(cv::Mat &dst, cv::Size & rSize, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline)
 {
 	CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));

+ 2 - 0
MFCApplication1/CvxText.h

@@ -7,6 +7,8 @@
 using namespace std;
 
 void GetStringSize(HDC hDC, const char* str, int* w, int* h);
+//»ñÈ¡×Ö·û´®µÄ¿í¸ß
+cv::Size GetTextSize(const char* str, int fontSize, const char *fn = "Arial", bool italic = false, bool underline = false);
 void putTextZH(cv::Mat &dst, cv::Size & rSize, const char* str, cv::Point org, cv::Scalar color, int fontSize,
 	const char *fn = "Arial", bool italic = false, bool underline = false);