除非我使「static CPaintDC dc(this);」線不會畫?但這不好,因爲它最終會出錯,而且圖形也不會保留在屏幕上。dc.LineTo不使用OnPaint(),除非我移動鼠標並將窗口移出視圖?
不知道我做錯了
注:我有一個調用這個每100ms(x和y遞增) THX
void CGraphicsDlg::OnPaint()
{
CString s;
CPaintDC dc(this);// device context for painting
if (IsIconic())
{
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1)/2;
int y = (rect.Height() - cyIcon + 1)/2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else if(x==0)
{
s.Format("%d", x);
edXa->SetWindowText(s);
dc.MoveTo(20,400);
}
else if (x>0){
s.Format("%d", x);
edXb->SetWindowText(s);
dc.LineTo(20 + x, 40); // doesn't draw unless I make "static CPaintDC dc(this);" <- which will error out
}
CDialog::OnPaint();
}
void CGraphicsDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==1){
srand((unsigned)time(NULL));
//y = rand() % 100;
y++;
x++;
OnPaint();
}
}
我這樣做,當x == 0的條件是......你可以在代碼中看到 – jdl 2012-04-03 21:17:00
什麼你的意思是「筆入DC」? thx – jdl 2012-04-03 21:17:58
@jdl,創建一個CPen對象並使用'dc.SelectObject'來選擇它。你有代碼來執行'MoveTo',但你需要同時執行這兩個操作。下一次OnPaint被調用時,'MoveTo'將會丟失。 – 2012-04-03 21:30:30