如何在对话框的指定区域画图

legendhui 2005-03-08 08:34:40
我想在对话框的指定区域里画图,不知道该怎么作?
我现在画出来的不是在我想要的地方显示的,请问该怎么做?
...全文
617 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
沒給出初始化代碼,我以爲你明白,呵呵
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
對象要先創建,上面沒給出創建代碼

if(m_dcMem.GetSafeHdc() != NULL)
m_dcMem.DeleteDC();
if(m_bmMem.GetSafeHandle() != NULL)
m_bmMem.DeleteObject();

CDC *dc = GetDC();
m_dcMem.CreateCompatibleDC( dc );
m_bmMem.CreateCompatibleBitmap(dc , rc.Width() , rc.Height() );
m_dcMem.SelectObject(m_bmMem);

用完了記得釋放資源,可以參照我後面的回復"再給你一個讀文件打點到内存DC然後畵出的例子"
asksknow 2005-03-08
  • 打赏
  • 举报
回复
如果你的问题得不到满意回答;
如果你是专家,如果你想变知识为财富;
欢迎来到问知商城www.ask110.com
legendhui 2005-03-08
  • 打赏
  • 举报
回复
lanstar200(待我景天大侠斩妖除魔) ( ) 信誉:105

你的这句,运行是跳出

m_bmMem.GetObject(sizeof(BITMAP) , &bm);
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
再給你一個讀文件打點到内存DC然後畵出的例子

CFile file;
if(!file.Open("E:\\1.bmp" , CFile::modeRead | CFile::shareDenyNone , NULL)) return;
BITMAPFILEHEADER bfh;
DWORD dwDataSize = file.GetLength() - sizeof(BITMAPFILEHEADER);
LPBITMAPINFO lpbi = (LPBITMAPINFO)malloc(file.GetLength());

file.Read(&bfh , sizeof(BITMAPFILEHEADER));
file.Read(lpbi , file.GetLength() - sizeof(BITMAPFILEHEADER));

file.Close();

LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpbi;
LPBYTE lpBits = (LPBYTE)lpbi + sizeof(BITMAPINFOHEADER);

CDC *pDC = GetDC();
CDC dcMem;
CBitmap bmMem;
dcMem.CreateCompatibleDC(pDC);
bmMem.CreateCompatibleBitmap(pDC , lpbih->biWidth , lpbih->biHeight);

dcMem.SelectObject(bmMem);

COLORREF color = 0;
int i,j;
int r,g,b;
for(i=0;i<lpbih->biHeight;i++)
{
for(j=0;j<lpbih->biWidth;j++)
{
b = (*lpBits) << 16;
lpBits ++;
g = (*lpBits) << 8;
lpBits ++;
r = (*lpBits);
lpBits ++;

color = b | g | r;

dcMem.SetPixel(j , lpbih->biHeight - i , color);
}
}

pDC->BitBlt(0,0,lpbih->biWidth,lpbih->biHeight,&dcMem,0,0,SRCCOPY);

dcMem.DeleteDC();
bmMem.DeleteObject();
ReleaseDC(pDC);

free(lpbi);
lpbi = NULL;
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
打點使用CDC::SetPixel()先打到内存DC,然後貼圖其實是一樣的
wuchi 2005-03-08
  • 打赏
  • 举报
回复
movetoex()
lineto()
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
CDC m_dcMem;
CBitmap m_bmMem;
legendhui 2005-03-08
  • 打赏
  • 举报
回复
BitBlt和StretchBlt我也知道的

我现在的问题是在我想要的位置画一个坐标系,然后在上面打点

不是贴位图
xugang_2001 2005-03-08
  • 打赏
  • 举报
回复
摘自msdn:函数原形
BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );

解释:
x//画图初始位置横坐标

Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.

y//画图初始位置纵坐标


Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.

nWidth//显示图片宽

Specifies the width (in logical units) of the destination rectangle and source bitmap.

nHeight//显示图片高度

Specifies the height (in logical units) of the destination rectangle and source bitmap.

pSrcDC//cdc指针

Pointer to a CDC object that identifies the device context from which the bitmap will be copied. It must be NULL if dwRop specifies a raster operation that does not include a source.

xSrc//原始图片取横坐标

Specifies the logical x-coordinate of the upper-left corner of the source bitmap.

ySrc//原始图片取纵坐标

Specifies the logical y-coordinate of the upper-left corner of the source bitmap.

dwRop//类型

Specifies the raster operation to be performed. Raster-operation codes define how the GDI combines colors in output operations that involve a current brush, a possible source bitmap, and a destination bitmap. The following lists raster-operation codes for dwRop and their descriptions:


legendhui 2005-03-08
  • 打赏
  • 举报
回复
你的m_bmMem是什么类型的变量
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
在對話框上放一個Static(IDC_STATIC1),把圖畫在Static裏面的例子
CRect rc;
GetDlgItem(IDC_STATIC1)->GetWindowRect(&rc);
ScreenToClient(&rc);
//dc.BitBlt( rc.left , rc.top , rc.Width() , rc.Height() , &m_dcMem , 0 , 0 , SRCCOPY);
BITMAP bm;
m_bmMem.GetObject(sizeof(BITMAP) , &bm);
dc.SetStretchBltMode(COLORONCOLOR);
dc.StretchBlt( rc.left , rc.top , rc.Width() , rc.Height() , &m_dcMem , 0 , 0 ,
bm.bmWidth , bm.bmHeight , SRCCOPY);
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
CRect rc;
GetClientRect(&rc);
//畫圖位置 x y width height
dc.BitBlt( 0 , 0 , rc.Width() , rc.Height() , &m_dcMem , 0 , 0 , SRCCOPY);
dc.StretchBlt( 0 , 0 , rc.Width() , rc.Height() , &m_dcMem , 0 , 0 , bm.bmWidth , bm.bmHeight , SRCCOPY);
lanstar200 2005-03-08
  • 打赏
  • 举报
回复
BitBlt函數的參數指明了畫圖位置,仔細看看MSDN
fanqing 2005-03-08
  • 打赏
  • 举报
回复
运道,画图函数哪个不要指定坐标啊!不指定位置怎么画啊
fool_leave 2005-03-08
  • 打赏
  • 举报
回复
“我想在对话框的指定区域里画图,不知道该怎么作?”
用dc.SelectClipRgn的方法设置绘图区域,就不会把图画到不想要的地方了。


“我现在画出来的不是在我想要的地方显示的,请问该怎么做?”
好好计算绘图的位置,或者通过dc.SetViewportOrg方法来改变坐标点。
用另一个CDC来绘图,然后再BitBlt到原来的dc上也是个办法!





lanstar200 2005-03-08
  • 打赏
  • 举报
回复
因为我的代码那两个变量是全局的,创建的地方不止一个,因此在重新创建之前释放掉
bobob 2005-03-08
  • 打赏
  • 举报
回复
//得到IDC_STATIC的句柄
CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);

//根据句柄得到dc
CDC* pDC = pWnd->GetDC();

//得到dc的客户区域,并创建画刷填充这个区域
CRect rc;
pWnd->GetClientRect(&rc);
CBrush brush;
brush.CreateSolidBrush(RGB(0,255,0));
pDC->FillRect(&rc,&brush);

//设置dc的裁减区域为客户区域,因为STATIC并不具有真正的dc
//而是其父窗口的dc,所以用裁减来避免画到客户区域外面去
CRgn rgn;
rgn.CreateRectRgn(rc.left,rc.top,rc.right,rc.bottom);
pDC->SelectClipRgn(&rgn);

//创建画笔并选中到pDC
CPen pen,*pOldPen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));//2个像素,红色
pOldPen =pDC->SelectObject(&pen);

pDC->MoveTo(100,100);//移动到第一个点
pDC->LineTo(200,200);//连结第一个和第二个点
pDC->LineTo(300,200);//连结第二个和第三个点

//安全清理
pDC->SelectObject(pOldPen);
pWnd->ReleaseDC(pDC);
legendhui 2005-03-08
  • 打赏
  • 举报
回复
为什么要判断
if(m_dcMem.GetSafeHdc() != NULL)
m_dcMem.DeleteDC();
if(m_bmMem.GetSafeHandle() != NULL)
m_bmMem.DeleteObject();

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧