Visual studio 如何在visual studio 2019上使用winapi创建打开文件资源管理器的按钮
====================================================已解决================================= 正如Ken White在评论中提到的那样。我找到了一个帮助我的代码。我在某些地方编辑了代码a,以将其调整为Visual Studio 2019:Visual studio 如何在visual studio 2019上使用winapi创建打开文件资源管理器的按钮,visual-studio,user-interface,winapi,button,Visual Studio,User Interface,Winapi,Button,====================================================已解决================================= 正如Ken White在评论中提到的那样。我找到了一个帮助我的代码。我在某些地方编辑了代码a,以将其调整为Visual Studio 2019: #include <iostream> #include <Windows.h> // FUNCTION PROTOTYPES ===========
#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!", L"ERROR", 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, 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);
}
#包括
#包括
//功能原型=========================================================
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
//Windows应用程序的入口点=======================================
int WINAPI wWinMain(HINSTANCE HINSTANCE、HINSTANCE hPrevInstance、PWSTR szCmdLine、int nWinMode){
//定义一些变量
const wchar_t szWinName[]=L“Win32App”;
const wchar_t szAppTitle[]=L“Win32 API框架应用程序”;
HWND-HWND;
味精;
WNDCLASS wc;
//定义一个窗口类
wc.hInstance=hInstance;//此实例的句柄
wc.lpszClassName=szWinName;//窗口类名称
wc.lpfnWndProc=WndProc;//指向窗口进程的指针
wc.style=0;//默认样式
wc.hIcon=LoadIcon(NULL,IDI_应用程序);//预定义图标
wc.hCursor=LoadCursor(NULL,IDC_箭头);//预定义的光标
wc.lpszMenuName=NULL;//无类菜单
wc.cbClsExtra=0;//不需要额外的信息
wc.cbWndExtra=0;//不需要额外的信息
wc.hbrBackground=(HBRUSH)(颜色窗口+1);//预定义的颜色笔刷
//注册已定义的窗口类
如果(寄存器类(&wc)==0)
{
//出现错误,请中止程序
MessageBox(NULL,L“无法注册窗口类!”,L“错误”,MB_OK | MB_ICONERROR);
返回0;
}
//现在窗口类已经注册,创建主窗口
hwnd=CreateWindow(szWinName,//要创建的窗口类的名称
szAppTitle,//窗口标题栏标题
WS_OVERLAPPEDWINDOW,//窗口样式-正常
CW_usefault,//X坐标-由Windows决定
CW_USEDEFAULT,//Y坐标-让Windows决定
CW\u usefault,//宽度-由窗口决定
CW_usefault,//高度-由Windows决定
NULL,//没有父窗口
NULL,//没有菜单
hInstance,//此实例的句柄
NULL);//没有其他参数
//检查窗口是否已成功创建
if(hwnd==NULL)
{
//发生错误,中止
MessageBox(NULL,L“无法创建主窗口!”,L“错误”,MB_OK | MB_ICONERROR);
返回0;
}
//显示(并更新)新创建的窗口
显示窗口(hwnd、nWinMode);
更新窗口(hwnd);
//创建消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);//翻译键盘消息
DispatchMessage(&msg);//将控件返回到Windows
}
返回msg.wParam;
}
//处理Windows发送给应用程序的消息================
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
//选择要使用的Windows邮件
开关(信息)
{
//窗口正在被破坏,请终止应用程序
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
//让Windows处理switch语句中未指定的任何消息
返回DefWindowProc(hwnd、message、wParam、lParam);
}
问题描述:使用C++中的Visual Studio 2019创建新窗口
我正在使用VisualStudio2019在cpp中的一个控制台应用程序类型的项目中为USB设备编写用户界面。我想添加两个功能-我想创建将出现在屏幕上的按钮,使用户能够:
#包括
LRESULT回调WindowProc(HWND HWND,UINT uMsg,WPARAM WPARAM,LPARAM LPARAM){return
DefWindowProc(hwnd、uMsg、wParam、lParam);}
int WINAPI wWinMain(HINSTANCE HINSTANCE、HINSTANCE hPrevInstance、PWSTR pCmdLine、int nCmdShow){
//注册窗口类。
常量wchar_t CLASS_NAME[]=L“示例窗口类”;
WNDCLASS wc={};
wc.lpfnWndProc=WindowProc;
wc.hInstance=hInstance;
wc.lpszClassName=类名称;
注册类(&wc);
HWND HWND=CreateWi
#include <Windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return
DefWindowProc(hwnd, uMsg, wParam, lParam); }
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 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, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
return 0;
}
#define IDB_BTN 1001
HWND btn = CreateWindow("BUTTON", "OPEN", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 500, 200, 100, 100, hwnd, (HMENU)IDB_BTN, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
ShellExecute(NULL, "open", "explorer.exe", NULL, NULL, SW_NORMAL);
LPCWSTR pszPathToOpen = L"C:\\Windows";
PIDLIST_ABSOLUTE pidl;
if (SUCCEEDED(SHParseDisplayName(pszPathToOpen, 0, &pidl, 0, 0)))
{
ITEMIDLIST idNull = { 0 };
LPCITEMIDLIST pidlNull[1] = { &idNull };
SHOpenFolderAndSelectItems(pidl, 1, pidlNull, 0);
ILFree(pidl);
}
#include <Windows.h>
#include <shlobj_core.h>
#define IDB_BTN 1001
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
// Register the window class.
TCHAR CLASS_NAME[] = "Sample Window Class";
MSG msg;
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
"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
HWND btn = CreateWindow(
"BUTTON",
"OPEN",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
500,
200,
100,
100,
hwnd,
(HMENU)IDB_BTN,
(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
NULL);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDB_BTN:
{
//ShellExecute(NULL, "open", "explorer.exe", NULL, NULL, SW_NORMAL);
LPCWSTR pszPathToOpen = L"C:\\Windows";
PIDLIST_ABSOLUTE pidl;
if (SUCCEEDED(SHParseDisplayName(pszPathToOpen, 0, &pidl, 0, 0)))
{
ITEMIDLIST idNull = { 0 };
LPCITEMIDLIST pidlNull[1] = { &idNull };
SHOpenFolderAndSelectItems(pidl, 1, pidlNull, 0);
ILFree(pidl);
}
return 0;
}
default:
break;
};
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}