建立的仍然是Win32 命令行的工程,所以入口点为main(),需要手动获取HINSTANCE结构。程序在WINXP+VS2005测试通过 /********************************************************************
created: 2008/05/02
created: 2:5:2008 3:40
main.cpp
author: Gohan
purpose: 演示SHBrowseForFolder用法
演示SHGetFileInfo获取路径图标
演示Picture Box在SDK里的使用方法
*********************************************************************/
#include <Windows.h>
#include <ShlObj.h>
#include "resource.h"
HICON g_icon;
RECT g_rect;
TCHAR g_path[MAX_PATH];
void OnInitDlg(HWND hwnd)
{
GetWindowRect(GetDlgItem(hwnd, IDC_ICON1), &g_rect);
MapWindowPoints(NULL, hwnd, (LPPOINT) &g_rect, 2);
DestroyWindow(GetDlgItem(hwnd, IDC_ICON1));
SetDlgItemText(hwnd,IDC_PATH,g_path);
}
void OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
DrawIcon(hdc,g_rect.left,g_rect.left,g_icon);
EndPaint(hwnd,&ps);
}
BOOL CALLBACK DlgProc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG :
OnInitDlg(hDlg);
return TRUE ;
case WM_PAINT:
OnPaint(hDlg);
return TRUE;
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDOK :
case IDCANCEL :
EndDialog (hDlg, 0) ;
return TRUE ;
}
break ;
}
return FALSE ;
}
int main()
{
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
BROWSEINFO bi;
ZeroMemory(&bi,sizeof(BROWSEINFO));
LPMALLOC pMalloc;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if(pidl != NULL)
{
SHGetPathFromIDList(pidl,g_path);
SHFILEINFO sfi;
ZeroMemory(&sfi,sizeof(SHFILEINFO));
SHGetFileInfo(g_path,0,&sfi,sizeof(SHFILEINFO),SHGFI_ICON);
g_icon = sfi.hIcon;
DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, DlgProc) ;
//MessageBox(NULL,path,TEXT("Choose"),MB_OK);
if(SUCCEEDED(SHGetMalloc(&pMalloc)))
{
pMalloc->Free(pidl);
pMalloc->Release();
}
}
else
{
MessageBox(NULL,TEXT("),TEXT("Choose"),MB_OK);
}
}
/************************************************************************/
/* resource.h */
/************************************************************************/
#define IDD_DIALOG1 101
#define IDC_PATH 1001
#define IDC_ICON1 1002
/************************************************************************/
/* demo.rc Microsoft Visual C++ generated resource script. */
/************************************************************************/
#include "resource.h"
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_DIALOG1 DIALOGEX 0, 0, 126, 83
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "确定",IDOK,69,7,50,14
PUSHBUTTON "取消",IDCANCEL,69,24,50,14
CONTROL "",IDC_ICON1,"Static",SS_WHITEFRAME | SS_REALSIZEIMAGE,13,10,20,20,WS_EX_TRANSPARENT
LTEXT "静态",IDC_PATH,14,44,89,21,0,WS_EX_CLIENTEDGE
END
/////////////////////////////////////////////////////////////////////////////