无法在 C++ Win32 中将 URL 或 ID 设置为 SysLink
Can not Set URL or ID to SysLink in C++ Win32
这是我的代码,但它无法编译并给出两个错误:
error C2440: '=' : cannot convert from 'WCHAR' to 'WCHAR [2084]'
IntelliSense: expression must be a modifiable lvalue
我已经阅读了下面的 link 但它对我没有帮助 :
error C2106: '=' : left operand must be l-value
[代码]:
#define ID_SysLink 500
HWND hWnd;
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_CREATE:
{
HWND syslink_handle=0;
syslink_handle=CreateWindowEx(0, WC_LINK, L"test syslink", WS_VISIBLE | WS_CHILD , 10, 10, 100, 30, hWnd, HMENU(ID_SysLink), 0, 0);
LITEM *pitem = 0;
pitem->iLink = 0;
pitem->mask = LIF_ITEMINDEX | LIF_ITEMID | LIF_URL | LIF_STATE;
pitem->state = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
pitem->stateMask = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
WCHAR url_wchar = WCHAR(L"http://www.google.com");
pitem->szUrl = url_wchar;
SendMessage(syslink_handle, LM_SETITEM, 0, LPARAM(&pitem));
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPreviewInstance,LPSTR lpcmdline,int ncmdshow)
{
WNDCLASSEX wndexcls;
wndexcls.lpszClassName = wndclssname;
wndexcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndexcls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndexcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wndexcls.hbrBackground = (HBRUSH)(COLOR_3DDKSHADOW + 1);
wndexcls.lpszMenuName = NULL;
wndexcls.style = NULL;
wndexcls.hInstance = hInstance;
wndexcls.cbSize = sizeof(WNDCLASSEX);
wndexcls.cbClsExtra = 0;
wndexcls.cbWndExtra = 0;
wndexcls.lpfnWndProc = WndProc;
RegisterClassEx(&wndexcls);
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, L"winclass", L"TestApp", WS_OVERLAPPEDWINDOW, 100, 100, 640, 380, 0, 0,hInstance, 0);
ShowWindow(hWnd, ncmdshow);
UpdateWindow(hWnd);
MSG wnd_msg;
while (GetMessage(&wnd_msg,NULL,0,0)>0)
{
TranslateMessage(&wnd_msg);
DispatchMessage(&wnd_msg);
}
return (int)wnd_msg.wParam;
}
感谢您的帮助。
WCHAR url_wchar = WCHAR(L"http://www.google.com");
pitem->szUrl = url_wchar;
您可能打算将 url_wchar
设为指针:
WCHAR* url_wchar = L"http://www.google.com";
不幸的是,szUrl
不是一个指针,它是一个字符数组。因此,要分配它,您需要使用字符串复制函数 copy 将字符串复制到其中(而不是仅仅将其分配为指针)。例如,
wcscpy_s(pitem->szUrl, L_MAX_URL_LENGTH, L"http://www.google.com");
如果你确实编译过它,你会发现它或多或少会立即崩溃,因为:
LITEM *pitem = 0;
pitem->iLink = 0;
你正在写一个空指针。您需要为 LITEM
结构分配 space,或者(最好)只在堆栈上声明它:
LITEM item{};
item.mask = LIF_ITEMINDEX | LIF_ITEMID | LIF_URL | LIF_STATE;
item.state = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
item.stateMask = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
wcscpy_s(item.szUrl, L_MAX_URL_LENGTH, L"http://www.google.com");
SendMessage(syslink_handle, LM_SETITEM, 0, (LPARAM)&item);
这是我的代码,但它无法编译并给出两个错误:
error C2440: '=' : cannot convert from 'WCHAR' to 'WCHAR [2084]'
IntelliSense: expression must be a modifiable lvalue
我已经阅读了下面的 link 但它对我没有帮助 :
error C2106: '=' : left operand must be l-value
[代码]:
#define ID_SysLink 500
HWND hWnd;
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_CREATE:
{
HWND syslink_handle=0;
syslink_handle=CreateWindowEx(0, WC_LINK, L"test syslink", WS_VISIBLE | WS_CHILD , 10, 10, 100, 30, hWnd, HMENU(ID_SysLink), 0, 0);
LITEM *pitem = 0;
pitem->iLink = 0;
pitem->mask = LIF_ITEMINDEX | LIF_ITEMID | LIF_URL | LIF_STATE;
pitem->state = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
pitem->stateMask = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
WCHAR url_wchar = WCHAR(L"http://www.google.com");
pitem->szUrl = url_wchar;
SendMessage(syslink_handle, LM_SETITEM, 0, LPARAM(&pitem));
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPreviewInstance,LPSTR lpcmdline,int ncmdshow)
{
WNDCLASSEX wndexcls;
wndexcls.lpszClassName = wndclssname;
wndexcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndexcls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndexcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wndexcls.hbrBackground = (HBRUSH)(COLOR_3DDKSHADOW + 1);
wndexcls.lpszMenuName = NULL;
wndexcls.style = NULL;
wndexcls.hInstance = hInstance;
wndexcls.cbSize = sizeof(WNDCLASSEX);
wndexcls.cbClsExtra = 0;
wndexcls.cbWndExtra = 0;
wndexcls.lpfnWndProc = WndProc;
RegisterClassEx(&wndexcls);
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, L"winclass", L"TestApp", WS_OVERLAPPEDWINDOW, 100, 100, 640, 380, 0, 0,hInstance, 0);
ShowWindow(hWnd, ncmdshow);
UpdateWindow(hWnd);
MSG wnd_msg;
while (GetMessage(&wnd_msg,NULL,0,0)>0)
{
TranslateMessage(&wnd_msg);
DispatchMessage(&wnd_msg);
}
return (int)wnd_msg.wParam;
}
感谢您的帮助。
WCHAR url_wchar = WCHAR(L"http://www.google.com"); pitem->szUrl = url_wchar;
您可能打算将 url_wchar
设为指针:
WCHAR* url_wchar = L"http://www.google.com";
不幸的是,szUrl
不是一个指针,它是一个字符数组。因此,要分配它,您需要使用字符串复制函数 copy 将字符串复制到其中(而不是仅仅将其分配为指针)。例如,
wcscpy_s(pitem->szUrl, L_MAX_URL_LENGTH, L"http://www.google.com");
如果你确实编译过它,你会发现它或多或少会立即崩溃,因为:
LITEM *pitem = 0;
pitem->iLink = 0;
你正在写一个空指针。您需要为 LITEM
结构分配 space,或者(最好)只在堆栈上声明它:
LITEM item{};
item.mask = LIF_ITEMINDEX | LIF_ITEMID | LIF_URL | LIF_STATE;
item.state = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
item.stateMask = LIS_ENABLED | LIS_FOCUSED | LIS_HOTTRACK;
wcscpy_s(item.szUrl, L_MAX_URL_LENGTH, L"http://www.google.com");
SendMessage(syslink_handle, LM_SETITEM, 0, (LPARAM)&item);