您的位置:首页 > 其它

计算一段程序进行的时间、 控制台下用Win32 API打开文件对话框

2013-09-26 14:16 483 查看
#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <windows.h>
using namespace std;
#include <fstream>

float aaa;

void main()
{
clock_t start,finish;
int i=0;
int ss=0;
float avg;

start=clock();

while(1)
{
i++;
if (i==10000000)
{
finish=clock();

avg=(finish-start)/*/8.0*/;
aaa=avg;
cout<<finish<<"   "<<start<<endl;
printf("%f",avg);
break;

}

}

}


 

二、 


控制台下用Win32 API打开文件对话框


#include "stdafx.h"
#include <Windows.h>
#include <CommDlg.h>
#include <iostream>

using namespace std;

static OPENFILENAME ofn ;

void PopFileInitialize (HWND hwnd)
{
static TCHAR szFilter[] = TEXT ("所有图片文件\0*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.gif;*.tiff;*.png;*.ico\0")  \
TEXT ("JPEG文件 (*.jpg;*.jpeg;*.jpe)\0*.jpg;*.jpeg;*.jpe\0") \
TEXT ("位图文件 (*.bmp;*.dib)\0*.bmp;*.dib\0") \
TEXT ("GIF (*.gif)\0*.gif\0") \
TEXT ("TIFF (*.tiff)\0*.tiff") \
TEXT ("PNG (*.png)\0*.png") \
TEXT ("ICO (*.ico)\0*.ico\0\0");

ofn.lStructSize       = sizeof (OPENFILENAME) ;
ofn.hwndOwner         = hwnd ;
ofn.hInstance         = NULL ;
ofn.lpstrFilter       = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter    = 0 ;
ofn.nFilterIndex      = 0 ;
ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
ofn.nMaxFile          = MAX_PATH ;
ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
ofn.nMaxFileTitle     = MAX_PATH ;
ofn.lpstrInitialDir   = NULL ;
ofn.lpstrTitle        = NULL ;
ofn.Flags             = 0 ;             // Set in Open and Close functions
ofn.nFileOffset       = 0 ;
ofn.nFileExtension    = 0 ;
ofn.lpstrDefExt       = TEXT ("jpg") ;
ofn.lCustData         = 0L ;
ofn.lpfnHook          = NULL ;
ofn.lpTemplateName    = NULL ;
}

BOOL PopFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
ofn.hwndOwner         = hwnd ;
ofn.lpstrFile         = pstrFileName ;
ofn.lpstrFileTitle    = pstrTitleName ;
ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;

return GetOpenFileName (&ofn) ;
}
int main()
{
static TCHAR     szFileName[MAX_PATH], szTitleName[MAX_PATH] ;
HWND      hwnd;
hwnd=GetForegroundWindow(); //获取前台窗口句柄。本程序中的前台窗口就是控制台窗口。
PopFileInitialize (hwnd);  //初始化ofn
PopFileOpenDlg(hwnd, szFileName, szTitleName);//打开文件对话框
cout<<szFileName<<endl;  //在控制台中显示选中文件的路径
return 0;

}

记得把你的工程属性中得字符集改成多字节字符集,因为在Unicode字符集下路径显示出的是一串数字。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: