Welcome to ErranLi's Blog!

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

常用链接

留言簿(12)

搜索

  •  

积分与排名

  • 积分 - 169282
  • 排名 - 151

最新评论

阅读排行榜



在WinCE中, 由于WinCE的精简,有很多win32下的标准API不支持,所以有很多在vc上能够轻易实现的功能, 在evc中有点难度,必须变通一下,就拿截取屏幕来说,一个常用的函数GetDIBits在wince就Unsupported.当时为了实现这个功能,颇费了了一番心血,当然其主要还是因为对api不是很熟悉。

最初使用的截屏方法说来还真有些丢人,一个一个点的取得颜色值,再保存到bmp文件中去,这样程序运行的速度超慢,保存一个800X600的bmp差不多要一两分钟(研华8251板),觉得有些不对头,决定想办法改进一下,就使劲地看了一下Charles Petzold先生的《Programming Windows》(这本书很经典,值得好好研究一下),终于把时间缩短到几秒钟了,下面这个函数在evc和vc下都test pass(不用更改),有不足之处欢迎指正,函数:

// this function create a bmp file to save the current screen;
// supported eVC++ 4.0 (wince 4.0) and vc++ 6.0 , test pass;

void CSnapDlg::OnScreenSave(const char *filename)

    HDC  hScrDC, hMemDC;        
    int  width, height; 
 
 //the pointer will save all pixel point's color value
 BYTE  *lpBitmapBits = NULL;
       
 //creates a device context for the screen device
    hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);

  //get the screen point size
    width = GetDeviceCaps(hScrDC, HORZRES);
    height = GetDeviceCaps(hScrDC, VERTRES);

    //creates a memory device context (DC) compatible with the screen device(hScrDC) 
    hMemDC = CreateCompatibleDC(hScrDC);

 //initialise the struct BITMAPINFO for the bimap infomation,
 //in order to use the function CreateDIBSection
 //
on wince os, each pixel stored by 24 bits(biBitCount=24) 
 //and no compressing(biCompression=0)

    BITMAPINFO RGB24BitsBITMAPINFO;
    ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
    RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
    RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
    RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
    RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
  
 
//use the function CreateDIBSection and SelectObject 
 //in order to get the bimap pointer : lpBitmapBits

    HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
       DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
 HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);

 // copy the screen dc to the memory dc
 BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
 
 //if you only want to get the every pixel color value,
 //you can begin here and the following part of this function will be unuseful;
 //the following part is in order to write file;

 //bimap file header in order to write bmp file
 BITMAPFILEHEADER bmBITMAPFILEHEADER;
 ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
 bmBITMAPFILEHEADER.bfType = 0x4d42;  //bmp 
    bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)
 
 //write into file
 FILE *mStream = NULL;
 if((mStream = fopenfilename, "wb")))
 {  
  //write bitmap file header
  fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
  //write bitmap info
  fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
  //write bitmap pixels data
  fwrite(lpBitmapBits, 3*width*height, 1, mStream);
 
 //close file
  fclose(mStream);
 }
 
 //delete
 DeleteObject(hMemDC);
 DeleteObject(hScrDC);
 DeleteObject(directBmp);
 DeleteObject(previousObject);
}

还是用英语作了注释,虽然英语学的很糟糕,还是觉得这个习惯比较好~~ ..


posted on 2006-05-14 01:34 erran 阅读(3900) 评论(4)  编辑 收藏 引用 所属分类: WinCE

Feedback

# re: EVC实现WIN CE下截屏并且保存到文件[By Erran] 2007-06-13 00:12 S.S.Pang
万分感谢!!正需要这个.呵呵.  回复  更多评论
  

# re: EVC实现WIN CE下截屏并且保存到文件[By Erran] [未登录] 2007-12-26 13:08 Terry
if((mStream = fopenfilename, "wb")))

應該是...

if((mStream = fopen(filename, "wb")))
  回复  更多评论
  

# re: EVC实现WIN CE下截屏并且保存到文件[By Erran] [未登录] 2008-11-07 17:06 Feng
十分感谢  回复  更多评论
  

# re: EVC实现WIN CE下截屏并且保存到文件[By Erran] 2010-03-06 09:34 xiaoI000
如果只是需要一个截屏的工具,就不需要自己写代码了,网上有这种工具
我用了下下面这个,还很好用,可以直接在PC上远程截取Windows CE上的屏幕.
http://www.wceui.cn/blog/article/wceui_capturescreen.html
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理