我想在特定的位置画个位图,要用什么方法可以做到?

解决方案 »

  1.   

    在OnPaint中画出你要的东西就行了.
    如果是MFC对话框程序,默认就已经生成了这个代码,你稍微改改.
      

  2.   

    获得对话框的HDC,
    HDC hdc = GetDC(hDlg);
    再用BitBlt在这个hdc中指定坐标贴图即可。BitBlt函数原形如下:
    BOOL BitBlt(
      HDC hdcDest, // handle to destination DC
      int nXDest,  // x-coord of destination upper-left corner
      int nYDest,  // y-coord of destination upper-left corner
      int nWidth,  // width of destination rectangle
      int nHeight, // height of destination rectangle
      HDC hdcSrc,  // handle to source DC
      int nXSrc,   // x-coordinate of source upper-left corner
      int nYSrc,   // y-coordinate of source upper-left corner
      DWORD dwRop  // raster operation code
    );
      

  3.   

    请问下hDlg这个参数是什么类型的。。
      

  4.   

    GetDC的参数是窗口句柄,HWND
    如果是MFC,记得用::GetDC
      

  5.   

    哦哦,我就是用MFC搞个对话框程序,也是用::GetDC么?
      

  6.   

    MFC的窗口、对话框、子控件都是继承自CWnd的,CWnd有个成员变量m_hWnd保存句柄.
    比如你要用来获取窗口DC
    HDC hDC=::GetDC(m_hWnd);//记得要用ReleaseDC释放.
    不不推荐这种方式,还是在OnPaint里绘制更合理.
    void CMainDlg::OnPaint()
    {
    if (IsIconic())
    {
    ......//MFC自动生成的代码若干,这里不列出
    }
    else
    {
    CPaintDC dc(this); 
    dc...... //你可以用这个dc来绘制了
    }
    }
      

  7.   

    HWND hwnd = pwnd->GetSafeHwnd(); 
      

  8.   


    这个hDlg表示对话框的句柄,为HWND类型。
      

  9.   

    您好 我也有相同的问题 我按照您说的方法在OnPaint中写了画图的代码,编译无错,但是运行后对话框中没有任何显示。是我的OnPaint函数没有被激发嘛?还是我的问题?我代码如下:void CLineTestDlg::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
    {
    CDialog::OnPaint();
    CPaintDC dc(this); CPoint pt1,pt2;
    pt1.x=5;pt1.y=5;
    pt2.x=100;pt2.y=100;
    dc.MoveTo(pt1.x,pt1.y);
    dc.LineTo(pt2.x,pt2.y);

    }
    }