【请教高手】VC编软件中常见却不简单的问题!!!!!

【请教高手】VC编软件中常见却不简单的问题!!!!!,第1张

BROWSEINFO bBinfo

memset(&bBinfo,0,sizeof(BROWSEINFO))//定义结构并初始化

bBinfo.hwndOwner=m_hWnd//设置对话框所有者句柄

char strTmp[255]

bBinfo.lpszTitle= "请选择文件所在目录: "

bBinfo.ulFlags = BIF_RETURNONLYFSDIRS //设置标志只允许选择目录

LPITEMIDLIST lpDlist

//用来保存返回信息的IDList,使用陆胡逗SHGetPathFromIDList函数转换为早卖字符串

lpDlist=SHBrowseForFolder(&bBinfo) //显示选择对话框

if(lpDlist!=NULL)

{

//把项目标识列表转化成目录

SHGetPathFromIDList(lpDlist,strTmp)

//这个strTmp就是目录的路径

m_editDir.SetWindowText(strTmp)

}

--------------- 如果找不到方法,做凯

头文件: Declared in shlobj.h.

库 Library: shell32.lib.

用c++打开一个文件夹的方法如下:可以实现单选文件或者多选文件,代码如下:

需引入头文件#include "CommDlg.h"

[cpp] view plaincopy

TCHAR szBuffer[MAX_PATH] = {0}

OPENFILENAME ofn= {0}

ofn.lStructSize = sizeof(ofn)

ofn.hwndOwner = m_hWnd

ofn.lpstrFilter = _T("Exe文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0")//要选择的文件后缀

ofn.lpstrInitialDir = _T("D:\\Program Files"誉租)//默认的文件路径

ofn.lpstrFile = szBuffer//存放文件的缓冲区

ofn.nMaxFile = sizeof(szBuffer)/sizeof(*szBuffer)

ofn.nFilterIndex = 0

ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER //标志如果是多选要加上OFN_ALLOWMULTISELECT

BOOL bSel = GetOpenFileName(&ofn)

这样就可以打开选择文件对话瞎虚袭框了。可以选择需要的文件。szBuffer是存放的选择磨兄的文件路径。

没有文件列表,只能枚举所尘郑有文件,再根据文件的属性判断是文件或者文件夹。

下面是MSDN上的完整例子

#include <派键颂windows.h>

#include <tchar.h>

#include <stdio.h>

#include <strsafe.h>

#pragma comment(lib, "User32.lib")

void DisplayErrorBox(LPTSTR lpszFunction)

int _tmain(int argc, TCHAR *argv[])

{

WIN32_FIND_DATA ffd

LARGE_INTEGER filesize

TCHAR szDir[MAX_PATH]

size_t length_of_arg

HANDLE hFind = INVALID_HANDLE_VALUE

DWORD dwError=0

// If the directory is not specified as a command-line argument,

// print usage.

if(argc != 2)

{

_tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0])

return (-1)

}

// Check that the input path plus 3 is not longer than MAX_PATH.

// Three characters are for the "\*" plus NULL appended below.

StringCchLength(argv[1], MAX_PATH, &length_of_arg)

if (length_of_arg >(MAX_PATH - 3))

{

_tprintf(TEXT("\nDirectory path is too long.\n"))

return (-1)

}

_tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1])

// Prepare string for use with FindFile functions. First, copy the

// string to a buffer, then append '\*' to the directory name.

StringCchCopy(szDir, MAX_PATH, argv[1])

StringCchCat(szDir, MAX_PATH, TEXT("\\亮谈*"))

// Find the first file in the directory.

hFind = FindFirstFile(szDir, &ffd)

if (INVALID_HANDLE_VALUE == hFind)

{

DisplayErrorBox(TEXT("FindFirstFile"))

return dwError

}

// List all the files in the directory with some info about them.

do

{

if (ffd.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)

{

_tprintf(TEXT(" %s <DIR>\n"), ffd.cFileName)

}

else

{

filesize.LowPart = ffd.nFileSizeLow

filesize.HighPart = ffd.nFileSizeHigh

_tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart)

}

}

while (FindNextFile(hFind, &ffd) != 0)

dwError = GetLastError()

if (dwError != ERROR_NO_MORE_FILES)

{

DisplayErrorBox(TEXT("FindFirstFile"))

}

FindClose(hFind)

return dwError

}

void DisplayErrorBox(LPTSTR lpszFunction)

{

// Retrieve the system error message for the last-error code

LPVOID lpMsgBuf

LPVOID lpDisplayBuf

DWORD dw = GetLastError()

FormatMessage(

FORMAT_MESSAGE_ALLOCATE_BUFFER |

FORMAT_MESSAGE_FROM_SYSTEM |

FORMAT_MESSAGE_IGNORE_INSERTS,

NULL,

dw,

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

(LPTSTR) &lpMsgBuf,

0, NULL )

// Display the error message and clean up

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,

(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR))

StringCchPrintf((LPTSTR)lpDisplayBuf,

LocalSize(lpDisplayBuf) / sizeof(TCHAR),

TEXT("%s failed with error %d: %s"),

lpszFunction, dw, lpMsgBuf)

MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK)

LocalFree(lpMsgBuf)

LocalFree(lpDisplayBuf)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12290358.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存