www.518cj.net
#include "stdafx.h"
#include "resource.h"
#include "bigint.h"
#include "app.h"
/*
為什么布局上就能出問題?
*/
layoutOfRoll_t Tapp::get_layoutOfRoll()
{
uiMain_t& ui = m_uiMain;
RECT rc = ui.rects[ELE_ROLLAREA];
int w = rc.right - rc.left;
int h = rc.bottom - rc.top;
wstring longestStr;
WCHAR strSeqno[48] = { 0 };
seqno2str(1, m_awards[m_pgs.iAward].quota, strSeqno);
if (ui.roll_bSeqno) longestStr = strSeqno;
longestStr += m_cxrs.longestCxr;
//最長的和計算的寬高比可能不對,結果導致
// 例如寬高比 1:1 ,類似正方形了
double longestWhr = calc_whr(longestStr.c_str());
double pctColGap = ui.pctColGap(m_cxrs.bPhotoMode, longestWhr);
double pctRowGap = ui.pctRowGap(m_cxrs.bPhotoMode, m_cxrs.photoWhr);
int maxColNum = m_pgs.numOfRolling;
if (maxColNum > MAX_COL_NUM)
maxColNum = MAX_COL_NUM;
//----------------------------------------------------------------------- 最大的 cell_w, cell_h
layoutOfRoll_t ly = { 0 };
for (int colNum = 1; colNum <= maxColNum; colNum++)
{
int rowNum = m_pgs.numOfRolling / colNum;
if (m_pgs.numOfRolling % colNum != 0)
rowNum++;
double w1, w2;
if (m_cxrs.bPhotoMode && ui.roll_bFlnm && ui.roll_bFlnmOut)
{
w1 = (double)w / (colNum + pctColGap*(colNum - 1));
w2 = (double)h / (rowNum + pctRowGap*(rowNum - 1) + ui.pctFlnmAreah()*rowNum) * m_cxrs.photoWhr;
}
else if (!m_cxrs.bPhotoMode)
{
w1 = (double)w / (colNum + pctColGap*(colNum - 1));
w2 = (double)h / (rowNum + pctRowGap*(rowNum - 1)) * longestWhr;
}
else
{
w1 = (double)w / (colNum + pctColGap*(colNum - 1));
w2 = (double)h / (rowNum + pctRowGap*(rowNum - 1)) * m_cxrs.photoWhr;
}
/*
w1 w2應該選哪一個, 下面的似乎不對
*/
double cell_w = w1 > w2 ? w2 : w1;
double cell_h = m_cxrs.bPhotoMode ? (cell_w / m_cxrs.photoWhr) : (cell_w / longestWhr);
if (cell_w > ly.cell_w)
{
ly.cell_w = cell_w;
ly.cell_h = cell_h;
ly.colNum = colNum;
ly.rowNum = rowNum;
}
else if (cell_w == ly.cell_w) { ; }
else break;
}//for
// 下面的代碼是關鍵出問題的:
// 你根據一個box上位置的字體 計算的 寬高比,得出的cell,這個寬高比錯成什么樣還不知道
//
// 根據這個cell ,和str+font,得到的實價的cell,變小了, 換了一個正常的字體或什么
//
//
//
//
//-----------------------------------------------------------------------
//實際的 cell_w, cell_h, body_lf, flnm_areah, flnm_lf, flnm_lineh
if (m_cxrs.bPhotoMode)
{
if (ly.cell_w > m_cxrs.photoSize*MAX_PHOTO_ZOOM || ly.cell_h > m_cxrs.photoSize*MAX_PHOTO_ZOOM)
{
double r = ly.cell_w / (m_cxrs.photoSize*MAX_PHOTO_ZOOM);
if (ly.cell_w < ly.cell_h) r = ly.cell_h / (m_cxrs.photoSize*MAX_PHOTO_ZOOM);
ly.cell_w = ly.cell_w / r;
ly.cell_h = ly.cell_h / r;
}
if (ui.roll_bFlnm)
{
ly.flnm_areah = ly.cell_h * ui.pctFlnmAreah();
ly.flnm_lf = get_lfOfActl(m_paintMain.memDc, longestStr.c_str(), ui.roll_font, ly.cell_w, ly.flnm_areah, NULL, &ly.flnm_lineh);
}
}
else ly.body_lf = get_lfOfActl(m_paintMain.memDc, longestStr.c_str(), ui.roll_font, ly.cell_w, ly.cell_h, &ly.cell_w, &ly.cell_h);
// 字體超出虛線呢, 在這里計算 lf 的時候
//
//----------------------------------------------------------------------- 其它計算
//為什么百分比也覺得不對呢, 應該是沒有apply吧
ly.colGap = ly.cell_w * pctColGap;
ly.rowGap = ly.cell_h * pctRowGap;
double x_remain = w - ly.cell_w*ly.colNum - ly.colGap*(ly.colNum - 1);
if (ui.roll_align == ALIGN_NEAR) ly.offx = 0.0;
else if (ui.roll_align == ALIGN_CENTER) ly.offx = x_remain / 2.0;
else ly.offx = x_remain;
double y_remain = h - ly.cell_h*ly.rowNum - ly.rowGap*(ly.rowNum - 1);
if (m_cxrs.bPhotoMode && ui.roll_bFlnm && ui.roll_bFlnmOut)
y_remain -= ly.flnm_areah*ly.rowNum;
if (ui.roll_valign == ALIGN_NEAR) ly.offy = 0.0;
else if (ui.roll_valign == ALIGN_CENTER) ly.offy = y_remain / 2.0;
else ly.offy = y_remain;
return ly;
}
void Tapp::hitTest(const RECT* rects, const BOOL* flagsOfShow, int rectNum, int x, int y, int* which, LONG* type)
{
POINT pt;
pt.x = x;
pt.y = y;
for (int i = 0; i < rectNum; i++)
{
if (flagsOfShow[i] && PtInRect(&rects[i], pt))
{
*which = i;
RECT rc = rects[i];
rc.left += ELE_EDGE;
rc.bottom = rc.top + ELE_EDGE;
rc.right -= ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_T; return; } //top
rc = rects[i];
rc.left = rc.right - ELE_EDGE;
rc.top += ELE_EDGE;
rc.bottom -= ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_R; return; } //right
rc = rects[i];
rc.left += ELE_EDGE;
rc.top = rc.bottom - ELE_EDGE;
rc.right -= ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_B; return; } //bottom
rc = rects[i];
rc.top += ELE_EDGE;
rc.right = rc.left + ELE_EDGE;
rc.bottom -= ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_L; return; } //left
rc = rects[i];
rc.right = rc.left + ELE_EDGE;
rc.bottom = rc.top + ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_LT; return; } //left-top
rc = rects[i];
rc.left = rc.right - ELE_EDGE;
rc.bottom = rc.top + ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_RT; return; } //right-top
rc = rects[i];
rc.left = rc.right - ELE_EDGE;
rc.top = rc.bottom - ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_RB; return; } //right-bottom
rc = rects[i];
rc.top = rc.bottom - ELE_EDGE;
rc.right = rc.left + ELE_EDGE;
if (PtInRect(&rc, pt)) { *type = HIT_LB; return; } //left-bottom
*type = HIT_IN;
return;
}
}//for
*which = -1;
*type = HIT_OUT;
}
//字體高地起伏是怎么引起的!
//
//
//
void Tapp::paint_str(HDC hDc, const WCHAR* str, LOGFONT& lf, COLORREF color, RECT& rc, WORD align, WORD valign, BOOL bGridFit)
{
if (!str) return;
if (rc.right - rc.left <= 1 || rc.bottom - rc.top <= 1) return;
StringFormat fmt;// = StringFormat::GenericTypographic();
//StringDigitSubstituteUser 改為None
// fmt.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces);
fmt.SetFormatFlags(StringFormatFlagsNoWrap | StringFormatFlagsNoClip | StringFormatFlagsMeasureTrailingSpaces);
if (align == ALIGN_NEAR) fmt.SetAlignment(StringAlignmentNear);
else if (align == ALIGN_CENTER) fmt.SetAlignment(StringAlignmentCenter);
else fmt.SetAlignment(StringAlignmentFar);
fmt.SetLineAlignment(StringAlignmentNear);
RectF rcF;
rcF.X = rc.left;
rcF.Y = rc.top;
rcF.Width = rc.right - rc.left;
rcF.Height = rc.bottom - rc.top;
//StringFormatFlagsDirectionRightToLeft
/*
fmt ff font lf 里面的 LANGID 是什么意思
lf - 選字體的邏輯,用到語言字符集
font 從lf里來的而已
ff 是 family, 不是具體的一個字體,是一類,有相同的as des cellline...
fmt 是字串的格式? ??? 因為宋體可以表示很多語言,所以,還是fmt原來的默認的好。
那么,為什么 區域改為 臺灣或英文到中文,就解決問題了呢? 難道是 ff 搞錯了導致的嗎
因為fmt不應該影響布局的啊。
*/
/*
如果是ff出錯導致id,為什么有的cell對有的錯,大部分對
ff如果初始化失敗,然后 r 的數值會?
ff失敗的結果,不穩定嗎,否則很難評價
1. 有的不可見了, r的范圍完全超出
2. 有的 Y稍微偏移了。
3. 正常的又怎么回事 跟偏移的同時存在
*/
//這是因為GDI+只能使用矢量字體???????? 所以列舉的時候要
// FontFamily ff(lf.lfFaceName);
FontFamily ff;
// FontFamily ff(L"不能");
Font gfont(hDc, &lf);
//lf 里面有字符集, 但是ff里面沒有
gfont.GetFamily(&ff); //上面的ff可能不準,下面的準確
SolidBrush bru(Color(GetRValue(color), GetGValue(color), GetBValue(color)));
// SolidBrush bru(Color(100, GetRValue(color), GetGValue(color), GetBValue(color)));
Graphics g(hDc);
int style = FontStyleRegular;
if (lf.lfItalic) style = FontStyleItalic;
if (lf.lfWeight == FW_BOLD) style = FontStyleBold;
if (lf.lfItalic && lf.lfWeight == FW_BOLD) style = FontStyleBoldItalic;
double r = (double)(ff.GetCellAscent(style) + ff.GetCellDescent(style)) / (double)ff.GetLineSpacing(style);
//難道每次得到的不同嗎, 如果是0不例外,r的數值太大,然后看不到字體
//看到和偏差同時存在的是什么情況啊?
//難道是有換行符嗎? \n
//
//
//
if (align == ALIGN_NEAR) rcF.Width += 500.0;
else if (align == ALIGN_FAR) { rcF.X -= 500.0; rcF.Width += 500.0; }
else { rcF.X -= 500.0; rcF.Width += 1000.0; }
if (valign == ALIGN_NEAR) rcF.Height += 500.0;
else if (valign == ALIGN_CENTER) {
rcF.Y -= 500.0;
rcF.Height += 1000.0;
rcF.Y += ((rcF.Height - gfont.GetHeight(&g)*r) / 2.0);
}
else {
rcF.Y -= 500.0;
rcF.Height += 500.0;
rcF.Y += (rcF.Height - gfont.GetHeight(&g)*r);
}
if (bGridFit) g.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit); //TextRenderingHintAntiAliasGridFit TextRenderingHintAntiAlias TextRenderingHintClearTypeGridFit
else g.SetTextRenderingHint(TextRenderingHintAntiAlias); //TextRenderingHintAntiAlias
g.DrawString(str, -1, &gfont, RectF(rcF.X,rcF.Y,rcF.Width,rcF.Height), &fmt, &bru);
// g.DrawString(str, -1, &gfont, rcF, &fmt, &bru);
}
void Tapp::paint_img(HDC hDc, Timg* img, RECT& rc, double maxZoom, WORD align, WORD valign, BOOL bStretch, BOOL bEllipse)
{
RECT rc_actl = rc;
if (!bStretch)
rc_actl = get_rcOfActl(img, rc, maxZoom, align, valign);
if (rc_actl.right - rc_actl.left <= 1 || rc_actl.bottom - rc_actl.top <= 1) return;
HRGN rgn = NULL;
if (bEllipse)
{
rgn = CreateEllipticRgn(rc_actl.left, rc_actl.top, rc_actl.right, rc_actl.bottom);
SelectClipRgn(hDc, rgn);
}
img->draw(hDc, rc_actl);
if (rgn) {
SelectClipRgn(hDc, NULL);
DeleteObject(rgn);
}
}
//maxZoom = -1 不限制放大倍數
RECT Tapp::get_rcOfActl(Timg* img, RECT& rc, double maxZoom, WORD align, WORD valign)
{
RECT rc_actl = rc;
if (!img) goto END;
if (rc.bottom - rc.top <= 1 || rc.right - rc.left <= 1) goto END;
if (!img->isLoad()) goto END;
{
double area_w = rc.right - rc.left;
double area_h = rc.bottom - rc.top;
double area_whr = (double)area_w / (double)area_h;
double img_whr = (double)img->m_w / (double)img->m_h;
//縮小倍數 rate
double rate = (double)img->m_h / (double)area_h;
if (img_whr > area_whr)
rate = (double)img->m_w / (double)area_w;
if (maxZoom > 0.0 && rate < 1.0 / maxZoom)
rate = 1.0 / maxZoom;
double actl_w = img->m_w / rate;
double actl_h = img->m_h / rate;
double l, t, r, b;
if (align == ALIGN_NEAR) l = rc.left;
else if (align == ALIGN_CENTER) l = rc.left + (area_w - actl_w) / 2.0;
else l = rc.right - actl_w;
if (valign == ALIGN_NEAR) t = rc.top;
else if (valign == ALIGN_CENTER) t = rc.top + (area_h - actl_h) / 2.0;
else t = rc.bottom - actl_h;
r = l + actl_w;
b = t + actl_h;
l = floor(l + 0.5);
t = floor(t + 0.5);
r = floor(r + 0.5);
b = floor(b + 0.5);
if (l < rc.left) l = rc.left;
if (t < rc.top) t = rc.top;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/26930.html
標籤:界面
