用MFC生成單檔案模版,在視圖視窗畫一些方格
1.整個Draw_Rectangle函式都沒進行賦值運算,為什么變數m_Width和m_Height會變為0呢?
2.繪制的方格等圖形,怎么才能隨著視窗的拉伸成比列變化呢?
謝謝!
代碼如下:
在View類中加入變數
public:
// 比列因子
double m_Factor;
// 寬度
int m_Width;
// 高度
int m_Height;
void Draw_Rectangle(CDC* pDC, int Cx, int Cy, int m_Width2, int m_Height2);
OnDraw函式,賦值
void CExampleView::OnDraw(CDC* pDC)
{
CExampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機資料添加繪制代碼
CRect m_Rect;
GetClientRect(m_Rect);
double m_ViewHeight = m_Rect.bottom - m_Rect.top;
m_Factor = m_ViewHeight/120000;
m_Width = (int)(m_Width * m_Factor);
m_Height = (int)(m_Height * m_Factor);
int Center_X = (int)(m_ViewHeight/2);
int Center_Y = (int)(m_ViewHeight/2);
Draw_Rectangle(pDC, Center_X, Center_Y, m_Width, m_Height);
}
函式實作:
void CExampleView::Draw_Rectangle(CDC* pDC, int Cx, int Cy, int m_Width2, int m_Height2)
{
int LeftTop_X,LeftTop_Y;
for(int i = -2; i < 2; i++)
{
for(int j= -3; j< 3; j++)
{
LeftTop_X = Cx + i*m_Width2;
LeftTop_Y = Cy + j*m_Height2;
pDC->Rectangle(LeftTop_X,LeftTop_Y,LeftTop_X+m_Width2,LeftTop_Y+m_Height2);
}
}
CString str;
str.Format(L"%d,%d",m_Width2,m_Height2);
MessageBox(str,L"m_Width and m_Height");
}
uj5u.com熱心網友回復:
估計是 m_Factor 強制取整 導致的為什么要除以120000,太大了吧
uj5u.com熱心網友回復:
設定斷點單步除錯uj5u.com熱心網友回復:
// 寬度int m_Width;
// 高度
int m_Height;
未初始化 ?
uj5u.com熱心網友回復:
好像都沒賦值啊uj5u.com熱心網友回復:
謝謝各位!int m_Width 和 int m_Height;都初始化了,在建構式里為成員變數賦值了.
CExampleView::CExampleView()
: m_Factor(0)
, m_Width(0)
, m_Height(0)
{
// TODO: 在此處添加構造代碼
m_Height = 7000;
m_Width = 5500;
}
uj5u.com熱心網友回復:
肯定不是強制取整導致的,除以120000后,用斷點除錯,可以看見,m_Height = 45 m_Width = 35呢
用這兩個值在客戶區畫出來的矩形還不小
謝謝,問題還沒解決,麻煩大家再幫幫忙!
uj5u.com熱心網友回復:
變數出現的地方不多,你都搜索下,都加上斷點除錯下,看看哪里變了uj5u.com熱心網友回復:
m_Width = (int)(m_Width * m_Factor);7000*120000=840000000;
不在 int 內
uj5u.com熱心網友回復:
矩形是不是沒畫在視窗內?uj5u.com熱心網友回復:
我除錯了一下,資料正常,就是沒顯示。uj5u.com熱心網友回復:
謝謝各位正如伊航說的,不管是用斷點除錯還是怎么使用監視,就是沒影像顯示,
圖形也肯定是在客戶區里的,就是不知道怎么的m_Width和m_Height運行中值突然變成了0
另外, m_Factor是一個處于0~1之間的比例因子,m_Width = (int)(m_Width * m_Factor)肯定在int范圍內
現在我想辦法避開了這個坑,但還是沒琢磨透為什么會如此,歡迎大家繼續討論!
uj5u.com熱心網友回復:
GetClientRect(&m_Rect);uj5u.com熱心網友回復:
沒問題 !
void CExampleView::Draw_Rectangle(CDC* pDC, int Cx, int Cy, int m_Width2, int m_Height2)
{
int LeftTop_X,LeftTop_Y;
for(int i = -2; i < 2; i++)
{
for(int j= -3; j< 3; j++)
{
LeftTop_X = Cx + i*m_Width2;
LeftTop_Y = Cy + j*m_Height2;
pDC->Rectangle(LeftTop_X,LeftTop_Y,LeftTop_X+m_Width2,LeftTop_Y+m_Height2);
}
}
CString str;
str.Format("%d=m_Width,%d=m_Height\n",m_Width2,m_Height2);
afxDump << str;// 11=m_Width,14=m_Height
}
除錯代碼:
afxDump << str;
那個 MessageBox(str,L"m_Width and m_Height"); 把 你 畫的 覆寫了 !
uj5u.com熱心網友回復:
找到問題了!
void CExampleView::OnDraw(CDC* pDC)
{
CExampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if (!pDoc)
return;
// TODO: 在此處為本機資料添加繪制代碼
CRect m_Rect;
GetClientRect(m_Rect);
double m_ViewHeight = m_Rect.bottom - m_Rect.top;
m_Factor = m_ViewHeight/120000;
// m_Height = 7000;
// m_Width = 5500;
#define WIN_HEIGHT 7000
#define WIN_WIDTH 5500
m_Width = (int)(WIN_WIDTH * m_Factor);// not m_Width *
m_Height = (int)(WIN_HEIGHT * m_Factor);// not m_Height *
// 否則 第二次重繪時 m_Height =0 !
int Center_X = (int)(m_ViewHeight/2);
int Center_Y = (int)(m_ViewHeight/2);
Draw_Rectangle(pDC, Center_X, Center_Y, m_Width, m_Height);
}
越來越小 !!
m_Width = (int)(m_Width * m_Factor);
m_Height = (int)(m_Height * m_Factor);
uj5u.com熱心網友回復:
或者
void CExampleView::OnDraw(CDC* pDC)
{
CExampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if (!pDoc)
return;
// TODO: 在此處為本機資料添加繪制代碼
CRect m_Rect;
GetClientRect(m_Rect);
double m_ViewHeight = m_Rect.bottom - m_Rect.top;
m_Factor = m_ViewHeight/120000;
int Width = (int)(m_Width * m_Factor);// not m_Width *
int Height = (int)(m_Height * m_Factor);// not m_Height *
// 否則 第二次重繪時 m_Height =0 !
int Center_X = (int)(m_ViewHeight/2);
int Center_Y = (int)(m_ViewHeight/2);
Draw_Rectangle(pDC, Center_X, Center_Y, Width, Height);
}
uj5u.com熱心網友回復:
這邏輯有問題啊,pDC畫圖的前兩個引數應該是一個點的絕對坐標,你一直傳遞的引數是寬度和高度,還要那么復雜的運算。傳遞的xy應該是相對于clientrect左上角的坐標,rect.left和rect.top加上偏移量,難為你能算到顯示出來……uj5u.com熱心網友回復:
CRect rect;GetClientRect(rect);
double sizezorn = 0.1;
int hight = (int)((rect.bottom-rect.top) / 20);
int width = (int)((rect.right - rect.left) / 10);
CPoint centpoint(int(rect.right/2),int(rect.bottom/2));
for (int i = 0; i != 4; ++i)
{
for (int j = -3; j != 3; ++j)
pDC->Rectangle(centpoint.x - j * width, centpoint.y, centpoint.x - j * width + width, centpoint.y + hight);
centpoint.y += hight;
}
uj5u.com熱心網友回復:
OnDraw或OnPaint里不要使用MessageBox,否則你永遠畫不出來uj5u.com熱心網友回復:
搞清楚 形參 和 實參 區別void CExampleView::Draw_Rectangle(CDC* pDC, int Cx, int Cy, int m_Width2, int m_Height2)
int m_Width2, int m_Height2 ,通常 m_表示 模塊 變數 ! 那個 2 毫無意義 !
這2個 形參 定義 有問題,
應該這樣:
void Draw_Rectangle(CDC* pDC, int Cx, int Cy, int Width, int Height) {。。。。}
uj5u.com熱心網友回復:
試試m_factor/12000在factor后面加上0.0 ?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/45305.html
標籤:基礎類
上一篇:各位大神。初次接觸libssh請教有什么資料可以入門
下一篇:急求大神!新手小白一臉懵逼。。。
