这是我程序,我想把网格只画在规定的地方(静态文本框中),可是这个网格大小和规定的一样,就是位置在对话框左上角
请问我如何才可以把网格会在想画的区域里。
CPaintDC dc(this); // device context for painting
CWnd*pWnd=GetDlgItem(IDC_STATIC1); 
CDC*pDC;
pDC=pWnd->GetDC();
CRect Recto;
 pWnd->GetClientRect(&Recto);
CBrush bgBrush(BLACK_BRUSH);
dc.SelectObject(bgBrush);
dc.Rectangle(Recto);
CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255));
dc.SelectObject(PenBlue);for(int x = 0; x < Recto.Width(); x += 20)
{
dc.MoveTo(x, 0);
dc.LineTo(x, Recto.Height());
}
for(int y = 0; y < Recto.Height(); y += 20)
{
dc.MoveTo(0, y);
dc.LineTo(Recto.Width(), y);
}

解决方案 »

  1.   

    看到你用静态文本来画,又不重新生成新的静态文本类,表示悲剧就要上演了
    到QQ去我教你如何用CWnd 类自建控件吧.很快我就有一颗星了,那是后我就离开CSDN了,恐怕你也找不到我了
    最后一次帮你其实我给你的那个程序很全,可能你现在看不懂
      

  2.   

    要理解逻辑坐标(相对坐标),和物理坐标(屏幕坐标)
    GetWindowRect 返回的是相对于屏幕左上角的物理坐标
    pwnd->ScreenToRect  把物理坐标变换成相对于调用这个函数的窗体左上角的坐标
    pwnd->GetclientRect 获得的相对于这个控件或者窗体的(客户区)左上角的坐标
    此例你用的控件无非客户区,所以返回的是控件的相对坐标  (0, 0, x, y);因为你用的对话框的dc,所以你使用的坐标必须是相对于对话框的坐标
    而不是相对于控件本身的坐标。下面例子可以运行,但是这是很ugly的做法,最好还是用子类化控件的做法。void CNetDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting 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
    {
    CPaintDC dc(this); // device context for painting

    CWnd *pWnd = GetDlgItem(IDC_STATIC1); 
    CRect Recto;
    pWnd->GetWindowRect(Recto);  // be care!!!!
    ScreenToClient(Recto); // hide the static, if not, the default will cover your paint
    pWnd->ShowWindow(FALSE); // draw the back
    CBrush bgBrush(RGB(0, 0, 0));
    dc.SelectObject(&bgBrush);
    dc.Rectangle(Recto);

    // draw the grid
    CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255));
    dc.SelectObject(PenBlue);

    for(int x = Recto.left; x < Recto.right; x += 20)
    {
    dc.MoveTo(x, Recto.top);
    dc.LineTo(x, Recto.bottom);
    } for(int y = Recto.top; y < Recto.bottom; y += 20)
    {
    dc.MoveTo(Recto.left, y);
    dc.LineTo(Recto.right, y);
    }

    CDialog::OnPaint();
    }
    }