如何解决如何在Visual Studio 2019上使用winapi创建打开文件浏览器的按钮
================================已解决============== ===================
正如肯·怀特(Ken White)在评论中提到的那样。我发现here的代码对我有所帮助。我已经在某些地方编辑了代码a以将其调整为Visual Studio 2019:
#include <iostream>
#include <Windows.h>
// FUNCTION PROTOTYPES =========================================================
LRESULT CALLBACK WndProc(HWND,UINT,WParaM,LParaM);
// entry point for a Windows application =======================================
int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PWSTR szCmdLine,int nWinMode) {
// define some variables
const wchar_t szWinName[] = L"Win32App";
const wchar_t szAppTitle[] = L"Win32 API Skeletal Application";
HWND hwnd;
MSG msg;
WNDCLASS wc;
// define a window class
wc.hInstance = hInstance; // handle to this instance
wc.lpszClassName = szWinName; // window class name
wc.lpfnWndProc = WndProc; // pointer to window proc
wc.style = 0; // default style
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); // predefined icon
wc.hCursor = LoadCursor(NULL,IDC_ARROW); // predefined cursor
wc.lpszMenuName = NULL; // no class menu
wc.cbClsExtra = 0; // no extra info needed
wc.cbWndExtra = 0; // no extra info needed
wc.hbrBackground = (HBrush)(COLOR_WINDOW + 1); // predefined color brush
// register the defined window class
if (RegisterClass(&wc) == 0)
{
// an error occurred,abort the program
MessageBox(NULL,L"Couldn't Register the Window Class!",L"ERROR",MB_OK | MB_ICONERROR);
return 0;
}
// Now that a window class has been registered,create the main window
hwnd = CreateWindow(szWinName,// name of window class to create
szAppTitle,// window title bar caption
WS_OVERLAPPEDWINDOW,// window style - normal
CW_USEDEFAULT,// X coordinate - let Windows decide
CW_USEDEFAULT,// Y coordinate - let Windows decide
CW_USEDEFAULT,// width - let Windows decide
CW_USEDEFAULT,// height - let Windows decide
NULL,// no parent window
NULL,// no menu
hInstance,// handle to this instance
NULL); // no additional arguments
// check to see if window was successfully created
if (hwnd == NULL)
{
// an error occurred,abort
MessageBox(NULL,L"Couldn't Create the Main Window!",MB_OK | MB_ICONERROR);
return 0;
}
// display (and update) the newly created window
ShowWindow(hwnd,nWinMode);
UpdateWindow(hwnd);
// create the message loop
while (GetMessage(&msg,NULL,0))
{
TranslateMessage(&msg); // translate keyboard messages
dispatchMessage(&msg); // return control to Windows
}
return msg.wParam;
}
// processes the messages that Windows sends to the application ================
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WParaM wParam,LParaM lParam)
{
// choose which Windows messages you want to use
switch (message)
{
// the window is being destroyed,so terminate the application
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
// let Windows process any messages not specified in the switch statement
return DefWindowProc(hwnd,message,wParam,lParam);
}
问题描述:使用Visual Studio 2019 in C ++创建新窗口
我正在使用Visual Studio 2019在cpp中项目的控制台应用程序类型中为USB设备编写用户界面。我想添加两个功能-我想创建将出现在屏幕上的按钮,并使用户能够:
- 通过单击“存储数据”按钮,文件浏览器将打开,他们将能够选择文件来存储从端点缓冲区读取的数据。
- 通过单击“读取数据”按钮,文件浏览器将打开,他们将能够选择要从中读取数据的文件并将其写入端点。 我想让用户选择文件的路径,我不想用户使用键盘将路径插入终端,我想创建一个按钮来打开文件浏览器,并允许用户访问所需的文件\位置。
例如,要将数据从端点缓冲区存储到文件中,用户将单击“存储数据”并导航到所需的文件\位置。选择位置后,libusb函数将从端点读取数据并将数据存储在临时结构中,我将从该结构中将其写入文件中。
对我来说,主要问题是创建这些按钮并使它们显示文件浏览器。我从一些简短的代码开始,以创建一个新窗口,如本指南中所述:
- 按钮创建-https://docs.microsoft.com/en-us/windows/win32/controls/create-a-button
- 创建窗口-https://docs.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program
问题是,当我运行程序时,窗口会打开并立即折叠。此外,我找不到什么功能可以帮助我按下按钮以打开文件浏览器。
我正在使用windows.h标头创建窗口。这是我的代码:
''''
#include <Windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,LParaM lParam) { return
DefWindowProc(hwnd,uMsg,lParam); }
int WINAPI wWinMain(HINSTANCE hInstance,PWSTR pCmdLine,int nCmdshow) {
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,// Optional window styles.
CLASS_NAME,// Window class
L"Learn to Program Windows",// Window text
WS_OVERLAPPEDWINDOW,// Window style
// Size and position
CW_USEDEFAULT,CW_USEDEFAULT,// Parent window
NULL,// Menu
hInstance,// Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd,nCmdshow);
return 0;
}
''''
我在链接器的其他依赖项中添加了标志“ user32.lib”,并且我也没有任何链接器错误,所以我想不是。可能是什么问题呢?我尝试查看其他类似问题,但找不到任何有用的方法。 还有,是否有任何库提供单击按钮后打开文件浏览器的功能?
我希望我的问题以前没有问过,并且写得很对。如果有问题,请告诉我,以便我进行编辑。
非常感谢您的时间和精力。
解决方法
运行该程序的窗口打开并立即折叠的原因是,您没有创建消息循环。消息循环不仅仅是您提到的WindowProc
函数。您还需要添加消息处理循环功能。
根据MSDN文档:
wWinMain 是程序入口点。程序启动时,它会注册一些有关应用程序窗口行为的信息。最重要的一项是函数的地址,在此示例中,该函数名为WindowProc。此功能定义了窗口的行为-窗口的外观,它与用户的交互方式等等。
接下来,程序创建窗口并接收一个唯一标识该窗口的句柄。
如果成功创建了窗口,程序将进入 while 循环。程序将一直处于此循环中,直到用户关闭窗口并退出应用程序为止。
请注意,即使我们说这是定义大多数应用程序逻辑的地方,该程序也没有显式调用WindowProc函数。 Windows通过传递一系列消息来与您的程序进行通信。 while 循环中的代码驱动了此过程。每次程序调用{{1}}函数时,它都会间接导致Windows调用WindowProc函数(对于每条消息一次)。
然后您可以通过CreateWindow作为以下代码创建按钮(但是您需要为后续操作设置唯一的标识符):
DispatchMessage
您可以通过两种方式打开资源管理器。
您可以直接通过ShellExecuteA
打开资源管理器:
#define IDB_BTN 1001
HWND btn = CreateWindow("BUTTON","OPEN",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,500,200,100,hwnd,(HMENU)IDB_BTN,(HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE),NULL);
当然,最好的方法是使用SHOpenFolderAndSelectItems()
函数。
ShellExecute(NULL,"open","explorer.exe",NULL,SW_NORMAL);
这是完整的示例:
LPCWSTR pszPathToOpen = L"C:\\Windows";
PIDLIST_ABSOLUTE pidl;
if (SUCCEEDED(SHParseDisplayName(pszPathToOpen,&pidl,0)))
{
ITEMIDLIST idNull = { 0 };
LPCITEMIDLIST pidlNull[1] = { &idNull };
SHOpenFolderAndSelectItems(pidl,1,pidlNull,0);
ILFree(pidl);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。