MFC如何使用GetDIBits 获取RichEdit控件用户区像素

caihong0011 2019-09-17 01:45:54

我使用RichEdit控件编辑文本, 然后想取出RichEdit控件用户区的像素点, 再把它显示在自画的屏幕上,如上图通过GetPixel实现的

最开始我是使用GetPixel 函数,代码如下 ,其中IDC_RICHEDIT21是RichEdit控件ID

pWnd1 = GetDlgItem(IDC_RICHEDIT21);
CDC* pDC = pWnd1->GetDC();
HDC hDC = pDC->GetSafeHdc();
for(int r=0;r<wy;r++)
{
for(int l=0;l<wx/8;l++)
{
for(int b=0;b<8;b++)
{
nColor = GetPixel(hDC, l*8+b, r);
B=(0x00ff0000 & nColor) >> 16;
G=(0x0000ff00 & nColor) >> 8;
R=0x000000ff & nColor;
}
}
}

通过上面操作,是可以正常获取RichEdit控件用户区像素, 但有个问题是速度超级慢,卡得很

所以想通过GetDIBits 实现

pWnd1 = GetDlgItem(IDC_RICHEDIT21);
CDC* pDC = pWnd1->GetDC();
HDC hDC = pDC->GetSafeHdc();

HBITMAP hbmp= (HBITMAP)GetCurrentObject(hDC,OBJ_BITMAP);
BITMAP bmp;
GetObject(hbmp, sizeof(BITMAP), &bmp);
UINT * pData = new UINT[bmp.bmWidth * bmp.bmHeight];

BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = bmp.bmWidth;
bmpInfo.bmiHeader.biHeight = -bmp.bmHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biBitCount = 32;

GetDIBits(pDC-> m_hDC,hbmp,0,bmp.bmHeight,pData,&bmpInfo,DIB_RGB_COLORS);

但我发现通过这样获取不到, 而且bmp.bmWidth , bmp.bmHeight 的大小并不是RichEdit控件的大小, 像是整个窗口的大小
问题在哪?哪位大神帮我看看
...全文
223 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
caihong0011 2019-09-18
  • 打赏
  • 举报
回复
已经解决 ,没有用GetCurrentObject了,改用CreateCompatibleBitmap ,也是参考别人的,处理如下:

CDC* pDC = GetDlgItem(IDC_RICHEDIT21)->GetDC();
HDC hDC = pDC->GetSafeHdc();

HDC hTmpDC = CreateCompatibleDC(hDC);

HBITMAP hbmp = CreateCompatibleBitmap(hDC,wx,wy);
SelectObject(hTmpDC, hbmp);

BitBlt(hTmpDC, 0, 0, wx, wy, hDC, 0, 0, SRCCOPY);

BITMAP bmp;
if (hbmp != NULL)
GetObject(hbmp, sizeof(BITMAP), (LPSTR)&bmp);

UINT *pData = new UINT[bmp.bmWidth * bmp.bmHeight];

BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = bmp.bmWidth;
bmpInfo.bmiHeader.biHeight = -bmp.bmHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biBitCount = 32;

GetDIBits(hDC,hbmp,0,bmp.bmHeight,pData,&bmpInfo,DIB_RGB_COLORS);
schlafenhamster 2019-09-17
  • 打赏
  • 举报
回复
那就是 RichEdit控件用户区 了!
caihong0011 2019-09-17
  • 打赏
  • 举报
回复
引用 4 楼 schlafenhamster 的回复:
RichEdit控件 是 父窗口 ? 里面 还有 别的 控件(中国深圳) ?


不是,中国深圳只是我输入的几个汉字而已
schlafenhamster 2019-09-17
  • 打赏
  • 举报
回复
RichEdit控件 是 父窗口 ? 里面 还有 别的 控件(中国深圳) ?
caihong0011 2019-09-17
  • 打赏
  • 举报
回复
@schlafenhamster

问题是现在通过这个GetObject(hbmp, sizeof(BITMAP), &bmp); 后
获取的位图好像不是RichEdit 控件, 因为 bmp.bmWidth,bmp.bmHeight的值是跟我的窗口大小一样,感觉像是获取到了整个窗口的位图
我拉伸RichEdit控件bmp.bmWidth,bmp.bmHeight的值不变, 但拉伸窗口的大小会变
schlafenhamster 2019-09-17
  • 打赏
  • 举报
回复
BITMAP bm;
HBITMAP hBitmap = (HBITMAP)GetCurrentObject(hdc, OBJ_BITMAP);

if (hBitmap != NULL)
GetObject(hBitmap, sizeof(BITMAP), &bm);

bm.bmBits里就是位图对象数据区的起始地址
zgl7903 2019-09-17
  • 打赏
  • 举报
回复

15,979

社区成员

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

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