WIN32一些文件及窗口操作

0,获取指定的系统路径:

    #include <shlobj.h> 
    #pragma comment(lib, "shell32.lib")

    TCHAR szPath[MAX_PATH];
  
::SHGetSpecialFolderPath(NULL, szPath, CSIDL_PERSONAL, FALSE);

 

1,打开文件或选中文件:

BOOL OpenDownFile(const TCHAR* pFilePath, BOOL bOpenCatalogue)

    std::wstring wstrPath
    if (bOpenCatalogue
    { 
        wstrPath.append(L"/n,/select,"); 
        //打开文件所在目录,选中指定文件 
    }

    wstrPath.append(pFilePath);//没有/n,/select,参数的话是直接打开指定文件或目录
  
ShellExecute(NULL, L"open", L"Explorer.exe", wstrPath.c_str(), NULL, SW_SHOWDEFAULT);

    return TRUE;

 

2,文件另存为:
BOOL SaveFileAs(const HWND hWnd, const TCHAR* pDefaultFileName)
{
    std::wstring FileSavePath;
    std::wstring fileNameAll;
    std::wstring wstrPostfix;

    fileNameAll = pDefaultFileName;
    size_t ipoint = fileNameAll.rfind(L".");   
    if(ipoint == fileNameAll.npos)//无后缀名
    {
        wstrPostfix.clear();
    }
    else
    {
        wstrPostfix = fileNameAll.substr(ipoint+1);
    }

    TCHAR filename[MAX_PATH] = {0};
  
TCHAR pstrFilter[MAX_PATH] = {0};
  
wsprintf(pstrFilter,L"%s Files(*.%s)|*.%s|All Files(*.*)|*.*||",wstrPostfix.c_str(),wstrPostfix.c_str(),wstrPostfix.c_str());

    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(ofn));
  
wcscpy_s(filename,_countof(filename),fileNameAll.c_str());  
   ofn.lpstrFile        = filename;
    ofn.nMaxFile        = MAX_PATH;
    ofn.lpstrFilter        = L"*.*";   
    ofn.lpstrDefExt        = wstrPostfix.c_str();
    ofn.lpstrTitle        = L"另存为";   
    ofn.Flags        = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
    ofn.FlagsEx     = OFN_EX_NOPLACESBAR;
    ofn.lStructSize        = sizeof(OPENFILENAME);
    ofn.hwndOwner        = hWnd;
    ofn.hInstance        = NULL;

    if (::GetSaveFileName(&ofn))
    {
        FileSavePath = filename;
    }
    else
    {
        return FALSE;
    }  

    return TRUE;
}

 

3,选择文件
#include <commdlg.h>
BOOL SelectFile(std::wstring& strSelectFile )
{
    OPENFILENAME opfn;
    TCHAR strFilename[MAX_PATH];
    ZeroMemory(&opfn, sizeof(OPENFILENAME));
    opfn.lStructSize = sizeof(OPENFILENAME);
    opfn.lpstrFilter = L"pdf Files(*.pdf)\0 *.pdf\0";//设置过滤
    opfn.nFilterIndex = 1;
    opfn.lpstrFile = strFilename;
    opfn.lpstrFile[0] = '\0';
    opfn.nMaxFile = sizeof(strFilename);
    opfn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
    if (::GetOpenFileName(&opfn))
    {
        strSelectFile = strFilename;
    }
    return TRUE;
}

 

4,文件拖拽

DragAcceptFiles(hWnd, TRUE);

case WM_DROPFILES:
{
  HDROP hDrop = (HDROP)wParam;
  TCHAR szFileName[MAX_PATH];
  int count = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
  if(count)
  {
    
for(int i=0; i<count;i++)
        { 
      int pathLen = DragQueryFile(hDrop, i, szFileName,MAX_PATH);
      
//Do(szFileName);  

    }
  
}
}

 break;

 

5,透明窗口

SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(m_hwnd, GWL_EXSTYLE) ^ 0x80000);
HINSTANCE hInst = LoadLibrary(L"User32.DLL");  
if (hInst)
{
    typedef BOOL(WINAPI *MYFUNC)(HWND, COLORREF, BYTE, DWORD);
    MYFUNC fun = NULL;
    fun = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
    if (fun)
        fun(hWnd, 0, 255 / 2, LWA_ALPHA); //半透明
    FreeLibrary(hInst);
}

你可能感兴趣的:(WIN32一些文件及窗口操作)