工控编程吧
标题:
110上位机VC MFC自动浏览目录全部位图
[打印本页]
作者:
qq263946146
时间:
2015-12-21 21:54
标题:
110上位机VC MFC自动浏览目录全部位图
(, 下载次数: 6)
上传
点击文件名下载附件
110上位机VC MFC自动浏览目录全部位图
功能展示
查看图片时,自查看图片相同目录文件夹中的其他图片,会省很多操作步骤,我们当前例程实现自动浏览同目录下的其他图片,效果如图
要点提示
在用户打开第一幅位图后,启动一定义器,定时进行相同文件夹下位图的打开操作。凡是窗口都可以设置定时器,启动定时器用函数SetTimer(), 原型为UINT SetTimer( HWND hWnd, // handle of window for timermessages UINT nIDEvent, // timer identifier UINT uElapse, // time-out value TIMERPROC lpTimerFunc // address of timer procedure ); hWnd为所在窗口的句柄, nIDEvent为非0的定时器ID, uElapse为定时时间,单位为毫秒, lpTimerFunc 为定时器定时到位后调用的函数,一般不用设置为NULL,在WM_TIMER消息中处理定时到位功能;
实现功能
1.新建基于单文档的应用程序
2.在文档类中添加变量CBitmap m_Bitmap;及它的获取函数CBitmap*CGkbc8Doc::GetBitmap() {return &m_Bitmap;}; 然后在文档类的OnOpenDocument()函数中实图片的打开操作;
if (m_Bitmap.m_hObject != NULL)//清除位图资源
m_Bitmap.DeleteObject();
BeginWaitCursor();//设置等待鼠标
//打开位图文件
HBITMAP hImage = (HBITMAP)LoadImage(NULL, lpszPathName, IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
EndWaitCursor();
if (!hImage) {
AfxMessageBox("LoadImage failed");
return FALSE;
}
if (!m_Bitmap.Attach(hImage)) //构造位图对象
{
AfxMessageBox("Bitmap could not be attached");
return FALSE;
}
SetModifiedFlag(FALSE);
UpdateAllViews(NULL); //更新所有视图
POSITION p = GetFirstViewPosition();
CView *pView = GetNextView(p);
pView->SetTimer(1,2000,NULL);
复制代码
3.在视窗类中添加一函数SearchBmp()实现相同文件夹中其他位图的查找
CString CGkbc8View::SearchBmp(CString curstr)
{
char buffer[_MAX_PATH];
long handle;
if(curstr.IsEmpty()) return "";
if(_getcwd( buffer, 1000)==NULL)
{
AfxMessageBox("请打开一个位图文件!");
return "";
}
CString m_sPartname;
int len = curstr.GetLength();
int i;
for(i = len-1;curstr[i] != '\\';i--)
m_sPartname.Insert(0,curstr[i]);
i++;
while(i--<0)
buffer[i]=curstr[i];
if (_chdir(buffer) != 0) return "";
bool b_notfinde=false;
struct _finddata_t filestruct;
// 开始查找工作, 找到当前目录下的第一个实体(文件或子目录),
// ″*″表示查找任何的文件或子目录, filestruct为查找结果
handle = _findfirst("*", &filestruct);
do{
if((handle ==-1)) // 当handle为-1, 表示当前目录为空, 则结束查找而返回
break;
// 检查找到的第一个实体是否是一个目录
if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
continue ;
}
复制代码
CString Filename=filestruct.name;
{
CString tailstr;
//获取文件扩展名
tailstr = Filename.Mid(Filename.GetLength()-3);
tailstr.MakeUpper();
Filename.MakeUpper();
m_sPartname.MakeUpper();
if(tailstr=="BMP")
{
if(b_notfinde==false)
{
if(m_sPartname==Filename)
b_notfinde=true;
}
else
{
_findclose(handle);
return Filename;
}
}
}
} while(_findnext(handle, &filestruct)==0);
_findclose(handle);
this->KillTimer(1);
return "";
}
<div style="text-align: center;"><span style="font-size: large; line-height: 1.5;">4,添加定时器的处理消息,实现间隔打开相同目录下其他位图</span></div>void CGkbc8View::OnTimer(UINT nIDEvent)
{
CString s=GetDocument()->GetPathName();
CString str=SearchBmp(s);
if(str=="")
return;
AfxGetApp()->OpenDocumentFile(str);
CView::OnTimer(nIDEvent);
}
<div style="text-align: center;"><span style="font-size: large; line-height: 1.5;">5.然后是打开位图的显示,在视窗类OnDraw()函数中实现</span></div>
复制代码
void CGkbc8View::OnDraw(CDC* pDC)
{
CGkbc8Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
BITMAP BitMap;
CBitmap *pBitmap,*pOldBitmap;
CDC DCMem;//创建内存设备场景
if (!DCMem.CreateCompatibleDC(pDC))
TRACE0("DCMem.CreateCompatibleDC failed\n");
pBitmap= pDoc->GetBitmap();
if(!pBitmap->GetSafeHandle())
return;
pOldBitmap = DCMem.SelectObject(pBitmap);
pBitmap->GetBitmap(&BitMap);
//将位图拷贝到显示设备场景中,进行显示
if (!pDC->BitBlt(0, 0, BitMap.bmWidth, BitMap.bmHeight, &DCMem, 0, 0, SRCCOPY))
TRACE0("BitBlt failed\n");
DCMem.SelectObject(pOldBitmap);
DCMem.DeleteDC();
}
<div style="text-align: center;"><span style="line-height: 1.5;"><font size="4">我们来演示下整个功能实现过程</font></span></div>
复制代码
[iqiyi]http://player.video.qiyi.com/7543ebeefadbd2b30b0c1e15572dee15/0/0/w_19rt77tj29.swf-albumId=5065940909-tvId=5065940909-isPurchase=0-cnId=12[/iqiyi]
(, 下载次数: 0)
上传
点击文件名下载附件
[note]1[/note]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4