基本信息
源码名称:自制“浏览文件夹”对话框
源码大小:0.05M
文件格式:.rar
开发语言:C/C++
更新时间:2021-08-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

模仿Windows3.1的浏览文件夹对话框

在插入或弹出U盘或光盘时能自动处理

lParam指向初始目录字符串,若lParam为0则显示C盘根目录

//由MSDN的代码“FoodDlgProc”改编而来,在属性表中实现

INT_PTR CALLBACK FoodDlgProc(HWND hDlg, UINT message, WPARAM wParam, 
    LPARAM lParam)
{
LPNMHDR     lpnmhdr;
static LinkQueue<LPTSTR> L;
TCHAR achBuffer[MAX_PATH];
DWORD nID, Count, i;

static DWORD dbcv_unitmask;

LPTSTR pB;
tagDNode<LPTSTR>* p;

LPPROPSHEETPAGE psp;

HWND hwndCB = GetDlgItem(hDlg, IDCOMBO); 
// Retrieve the list box handle. 
    HWND    hListBox = GetDlgItem(hDlg, IDC_LIST1); 

static HICON hIcon1;   // icon handle 
static HICON hIcon2;   // icon handle ;

static TCHAR ch = 0;

PMEASUREITEMSTRUCT pmis; 
    PDRAWITEMSTRUCT pdis; 
size_t cch;
    int yPos; 
    TEXTMETRIC tm; 
    RECT rcBitmap;

switch (message)
    {
    case WM_INITDIALOG:

// Load the bitmaps. g_hInst is the global HINSTANCE handle.
// Create a standard question icon. 

hIcon1 = LoadIcon(hInst, MAKEINTRESOURCE(IDB_PENCIL)); 

// Create a custom icon based on a resource. 

hIcon2 = LoadIcon(hInst, MAKEINTRESOURCE(IDB_CRAYON)); 

// Create a custom icon at run time.
InitQueue(&L);

        // Call an application-defined function to load bitmap resources.
dbcv_unitmask = AddItems(GetDlgItem(hDlg, IDCOMBO));

psp = (LPPROPSHEETPAGE)lParam;

if (psp->lParam != 0)
{
TCHAR *p;

strcpy(achBuffer, (LPTSTR)psp->lParam);
strlwr(achBuffer);
ch = achBuffer[0];
SetWindowText(GetDlgItem(hDlg, IDC_EDIT1), achBuffer);
p = strtok(achBuffer, "\\");
while (p != NULL)
{
pB = (LPTSTR)Malloc(sizeof(TCHAR) * MAX_PATH);
if (pB == NULL)
{
ErrorHandler();
return 0;
}
strcpy(pB, p);
if (p - achBuffer < 3)
strcat(pB, "\\");
EnterQueue(&L, pB);
AddItem(hListBox, pB, 0);
p = strtok(NULL, "\\");
}

Count = ComboBox_GetCount(hwndCB);
for (i = 0; i<Count; i )
{
SendMessage(hwndCB, CB_GETLBTEXT, 
i, (LPARAM)achBuffer); 
if (achBuffer[0] == ch)
break;
}
ComboBox_SetCurSel(hwndCB, i);

ListDir(hListBox, &L);
ListBox_SetTopIndex(hListBox , ListLength(L.front)-1);
}
else
{
SendDlgItemMessage(hDlg, IDCOMBO, CB_SETCURSEL, 0, 0); 

SendMessage(hwndCB, CB_GETLBTEXT, 
0, (LPARAM)achBuffer); 
achBuffer[2] = '\\';
achBuffer[3] = '\0';

ch = achBuffer[0];

SetWindowText(GetDlgItem(hDlg, IDC_EDIT1), achBuffer);

pB = (LPTSTR)Malloc(sizeof(TCHAR) * MAX_PATH);
if (pB == NULL)
{
ErrorHandler();
return 0;
}
strcpy(pB, achBuffer);
EnterQueue(&L, pB);

AddItem(hListBox, pB, 0);
ListDir(hListBox, &L);
}

return (INT_PTR)TRUE;

case WM_MEASUREITEM: 
 
pmis = (PMEASUREITEMSTRUCT) lParam; 

// Set the height of the list box items. 
pmis->itemHeight = YBITMAP;

return TRUE; 

case WM_DRAWITEM: 
 
pdis = (PDRAWITEMSTRUCT) lParam; 

// If there are no list box items, skip this message. 
if (pdis->itemID == -1) 

break; 


// Draw the bitmap and text for the list box item. Draw a 
// rectangle around the bitmap if it is selected. 
switch (pdis->itemAction) 

case ODA_SELECT: 
case ODA_DRAWENTIRE: 

nID =ListLength(L.front);

// Draw the bitmap associated with the item. 
//
// Get the item bitmap.
if (nID <= pdis->itemID)
hbmpPicture = (HICON)hIcon1;
else
hbmpPicture = (HICON)hIcon2;

// Create a compatible device context. 

// Select the item bitmap into the compatible device
// context and save the old bitmap.


DrawIconEx (pdis->hDC, pdis->rcItem.left min(pdis->itemID, nID) * 6, pdis->rcItem.top, hbmpPicture, XBITMAP, YBITMAP, 0, NULL, DI_NORMAL | DI_COMPAT ); 
// Copy the bitmap into the compatible device context.

// Draw the string associated with the item. 
//
// Get the item string from the list box.
SendMessage(pdis->hwndItem, LB_GETTEXT, 
pdis->itemID, (LPARAM)achBuffer); 

// Get the metrics for the current font.
GetTextMetrics(pdis->hDC, &tm); 

// Calculate the vertical position for the item string 
// so that the string will be vertically centered in the 
// item rectangle.
yPos = (pdis->rcItem.bottom pdis->rcItem.top - 
tm.tmHeight) / 2;

// Get the character length of the item string.
cch = strlen(achBuffer);

// Draw the string in the item rectangle, leaving a six
// pixel gap between the item bitmap and the string.
TextOut(pdis->hDC, XBITMAP 6 min(nID, pdis->itemID) * 6, yPos, achBuffer, cch);                         

// Clean up.

// Is the item selected? 
if (pdis->itemState & ODS_SELECTED) 

// Set RECT coordinates to surround only the 
// bitmap. 
rcBitmap.left = pdis->rcItem.left min(nID, pdis->itemID) * 6; 
rcBitmap.top = pdis->rcItem.top; 
rcBitmap.right = pdis->rcItem.left XBITMAP min(nID, pdis->itemID) * 6; 
rcBitmap.bottom = pdis->rcItem.top YBITMAP; 

// Draw a rectangle around bitmap to indicate 
// the selection. 
DrawFocusRect(pdis->hDC, &rcBitmap); 

break; 

case ODA_FOCUS: 

// Do not process focus changes. The focus caret 
// (outline rectangle) indicates the selection. 
// The IDOK button indicates the final 
// selection. 
break; 

return TRUE; 

case WM_COMMAND:
PropSheet_Changed(GetParent(hDlg), hDlg);
            switch (LOWORD(wParam)) 
            { 
case IDCANCEL: 
                    EndDialog(hDlg, 0); 
break;

case IDC_LIST1:
if ( HIWORD(wParam) & LBN_DBLCLK) 
{
DWORD   nItem = ListBox_GetCaretIndex(hListBox);
Count = ListBox_GetCount(hListBox);
nID = ListLength(L.front);

ListBox_GetText(hListBox, nItem, achBuffer);

for (i = 0; i < Count - min(nID, nItem 1); i )
ListBox_DeleteString(hListBox,min(nID, nItem 1));

i = nItem;
p = L.front;
if ( i < nID)
{
while(p!=NULL && i>0) { //顺指针向后查找直到p指向第i个元素或链尾结束
p=p->next; //指向直接后继结点
i--;
}
while (ListDelete(&p, 1, &pB))
{
Free(pB);
}
L.rear = p;
}

pB = (LPTSTR)Malloc(sizeof(TCHAR) *MAX_PATH);
if (pB==NULL)
{
ErrorHandler();
return 0;
}
strcpy(pB, achBuffer);
EnterQueue(&L, pB);

if ( nItem >= nID) AddItem(hListBox, pB, 0);
ListDir(hListBox, &L);

p=L.front->next; //p指向开始结点
ZeroMemory(achBuffer, sizeof(achBuffer));
nItem = 0;
while(p!=NULL) { //若不是链尾继续
strcpy(&achBuffer[nItem], p->data);
nItem = strlen(&achBuffer[nItem]);
if (achBuffer[nItem-1] != '\\')
{
achBuffer[nItem] = '\\';
nItem ;
}
p=p->next; //p指向直接后继结点
}
SetWindowText(GetDlgItem(hDlg, IDC_EDIT1), achBuffer);
ListBox_SetTopIndex(hListBox , ListLength(L.front)-1);
//EndDialog(hDlg, 0); 
}
break;

case IDCOMBO:
if ( HIWORD(wParam) & CBEN_ENDEDIT && L.front != NULL)
{
Count = ComboBox_GetCurSel(hwndCB); 
SendMessage(hwndCB, CB_GETLBTEXT, 
Count, (LPARAM)achBuffer); 
achBuffer[2] = '\\';
achBuffer[3] = '\0';
if (ch == achBuffer[0])
break;
ch = achBuffer[0];

while (!ListEmpty(L.front))
{
DeleteQueue(&L, &pB);
Free(pB);
}
L.rear = L.front;
Count = ListBox_GetCount(hListBox);
for (i = 0; i < Count; i )
ListBox_DeleteString(hListBox,0);

SetWindowText(GetDlgItem(hDlg, IDC_EDIT1), achBuffer);

pB = (LPTSTR)Malloc(sizeof(TCHAR) * MAX_PATH);
if (pB == NULL)
{
ErrorHandler();
return 0;
}
strcpy(pB, achBuffer);
EnterQueue(&L, pB);

AddItem(hListBox, pB, 0);
ListDir(hListBox, &L);

//SetFocus(hListBox);
break;
}
break;
            } 
            break; 

    case WM_DESTROY:

        // Call the application-defined function to free the bitmap resources.
while (!ListEmpty(L.front))
{
DeleteQueue(&L, &pB);
Free(pB);
}
Free(L.front);

DestroyIcon(hIcon1);
DestroyIcon(hIcon2);

        break;

case WM_NOTIFY:
      lpnmhdr = (NMHDR FAR *)lParam;

      switch (lpnmhdr->code)
         {
         case PSN_APPLY:   //sent when OK or Apply button pressed
            break;

         case PSN_RESET:   //sent when Cancel button pressed
            break;
         
         case PSN_SETACTIVE:
            //this will be ignored if the property sheet is not a wizard
            PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK | PSWIZB_FINISH);
            return FALSE;

         default:
            break;
         }

  case WM_DEVICECHANGE:
  dbcv_unitmask = Main_OnDeviceChange(hDlg, wParam, lParam, dbcv_unitmask);
  break;

  case 0x911:
  ch = '\0';
  SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(IDCOMBO,CBEN_ENDEDIT), 0);
  break;
    }
    return (INT_PTR)FALSE;
}