在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例Windows系统编程 → 自制“浏览文件夹”对话框

自制“浏览文件夹”对话框

Windows系统编程

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.05M
  • 下载次数:8
  • 浏览次数:109
  • 发布时间:2021-08-22
  • 实例类别:Windows系统编程
  • 发 布 人:消费的健康
  • 文件格式:.rar
  • 所需积分: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;
}



实例下载地址

自制“浏览文件夹”对话框

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警