使用Core WIN32浏览文件夹OnButtonClick [英] To Browse a Folder OnButtonClick using Core WIN32
本文介绍了使用Core WIN32浏览文件夹OnButtonClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好..
我正在使用Core WIN32编程.我想在Button单击上浏览一个文件夹.这是我用来打开文件对话框的代码:
Hello Everyone..
Am using Core WIN32 Programming. And I want to browse a folder on Button click. here is the Code i used to Open a File Dialog:
#include<windows.h>
#include<windowsx.h>
#include<commctrl.h>
#include<shlobj.h>
#include <stdio.h>
// Declare WndProcedure
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND button;
MSG Msg;
HWND hWnd;
HRESULT hRet;
WNDCLASSEX WndClsEx;
// Populate the WNDCLASSEX structure
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = "GlowdotWin32TutorialPartI";
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// Register the class
RegisterClassEx(&WndClsEx);
// Create the window object
hWnd = CreateWindow("GlowdotWin32TutorialPartI",
"Glowdot Win32 Tutorial - Part I",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//To Create a button :
button = CreateWindow(
"BUTTON",
"Open",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
800,
50,
50,
20,
hWnd,
NULL,
hInstance,
NULL
);
//To Create a Text box :
CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 50, 580, 20, hWnd, NULL, NULL, NULL);
//To Browse for a folder :
BROWSEINFO bi;
char pszBuffer[MAX_PATH];
LPITEMIDLIST pidl;
LPMALLOC lpMalloc;
// Initialize COM
if(CoInitializeEx(0,COINIT_APARTMENTTHREADED) != S_OK)
{
MessageBox(NULL,("Error opening browse window"),("ERROR"),MB_OK);
CoUninitialize();
return 0;
}
// Get a pointer to the shell memory allocator
if(SHGetMalloc(&lpMalloc) != S_OK)
{
MessageBox(NULL,("Error opening browse window"),("ERROR"),MB_OK);
CoUninitialize();
return 0;
}
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = ("Select a install Directory");
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
if(pidl = SHBrowseForFolder(&bi))
{
// Copy the path directory to the buffer
if(SHGetPathFromIDList(pidl,pszBuffer))
{
// pszBuffer now holds the directory path
printf(("You selected the directory: %s\n"),pszBuffer);
}
lpMalloc->Free(pidl);
}
lpMalloc->Release();
CoUninitialize();
if ( !button)
return 0;
// Verify window creation
if( !hWnd ) // If the window was not created,
return 0; // stop the application
// Show the window
ShowWindow(hWnd, SW_SHOWNORMAL);
ShowWindow(button, SW_SHOWNORMAL);
UpdateWindow(hWnd);
UpdateWindow(button);
// our message pump
while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
{
if (hRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
}
//////////////////
// WndProcedure //
//////////////////
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
请,有人建议我打开文件对话框打开"按钮.
在此先感谢..
Please, Someone suggest me to Open the File Dialog On button Click..
Thanks in Advance..
推荐答案
您应该:
- 将浏览器文件夹代码移动到新功能.
- 将唯一ID作为
hMenu
参数传递给按钮的CreateWindow
调用.- 将<消息循环(
WM_COMMAND
消息)中的c2>通知(wParam
的高字).当wParam
参数的低位字词是您的按钮ID时,请调用文件夹浏览功能.
You should:
- Move the browse for folder code to a new function.
- Pass a unique ID as
hMenu
parameter to theCreateWindow
call for the button.- Hanlde the
BN_CLICKED
notification (high word ofwParam
) in your message loop (WM_COMMAND
message). When the low word of thewParam
parameter is your button ID, call the browse for folder function.
// Define button ID
#define IDC_BROWSE_BTN 1000
// Pass button ID
button = CreateWindow(
"BUTTON", "Open",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
800, 50, 50, 20,
hWnd,
(HMENU)IDC_BROWSE_BTN,
hInstance,
NULL);
BOOL BrowseForFolder()
{
BOOL bRet = 0;
// Move the browse for folder code from WinMain to here
// Add line 'bRet = 1;' behind the printf statement when
// a directory has been choosen.
return bRet;
}
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
case WM_COMMAND :
if (HIWORD(wParam) == BN_CLICKED)
{
if (LOWORD(wParam) == IDC_BROWSE_BTN)
BrowseForFolder();
}
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
使用 ^ ]和GWL_ID
为按钮提供命令ID,例如1234.
为 WM_COMMAND 添加消息处理程序 [ ^ ]按钮的父窗口或您在任何地方进行消息处理的位置,如果LOWORD(wParam)
是您为按钮提供的ID(例如1234),则显示您的文件对话框.
Use SetWindowLong[^] withGWL_ID
to give your button a command ID, like 1234.
Add a message handler for WM_COMMAND[^] to the button''s parent window or wherever you do message handling, and ifLOWORD(wParam)
is the ID you gave to your button (e.g. 1234), then display that file dialog of yours.
这篇关于使用Core WIN32浏览文件夹OnButtonClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文