如何在 C 上使用 winapi 移动桌面图标?

问题描述 投票:0回答:2

我参考了以下两个主题:

因为他们提到桌面本身是一个 ListView,所以我尝试将

LVM_SETITEMPOSITION
消息直接发送到 Desktop HWND:

#include <windows.h>
#include <stdio.h>
#include <CommCtrl.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
      HWND MYHWND=NULL;
      MYHWND=GetDesktopWindow();
      SendMessage(MYHWND, LVM_SETITEMPOSITION, 2, MAKELPARAM(100, 100));
      return 1;
    }

但是,它不起作用,我不确定为什么,所以我尝试通过执行以下操作将他们的 C++ 代码转换为 C:

#include <windows.h>
#include <stdio.h>
#include <CommCtrl.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
      HWND desktopHandle=NULL;
      desktopHandle= FindWindowEx(NULL, NULL, "Progman", "Program Manager");
      desktopHandle = FindWindowEx(desktopHandle, NULL, "SHELLDLL_DefView", NULL);
      desktopHandle = FindWindowEx(desktopHandle, NULL, "SysListView32", "FolderView");
      SendMessage(desktopHandle, LVM_SETITEMPOSITION, 2, MAKELPARAM(500, 500));
      return 1;
    }

还是不行。有人可以帮我弄清楚出了什么问题吗?谢谢!

c winapi
2个回答
0
投票

做了更多研究,显然这种移动桌面图标的方法不再有效,根据 Raymond Chen:https://devblogs.microsoft.com/oldnewthing/20211122-00/?p=105948

我们可以尝试使用这种适当的方法:https://devblogs.microsoft.com/oldnewthing/20130318-00/?p=4933


0
投票

这是一个对我有用的解决方案,使用我在评论中写的内容。

你对我的解决方案是找不到

SHELLDLL_DefView
,我用 Spy++ 找到了
SHELLDLL_DefView
,它是
WorkerW
的孩子,你必须遍历各种
WorkerW
才能找到正确的一个包含
SHELLDLL_DefView
.

#define UNICODE

#include <windows.h>

#include <CommCtrl.h>
#include <stdio.h>

int main(int argc, char **argv) {
    HWND hWorkerW         = NULL;
    HWND hShellDLLDefView = NULL;
    HWND hListView        = NULL;

    do {
        hWorkerW = FindWindowEx(NULL, hWorkerW, L"WorkerW", NULL);
        hShellDLLDefView =
            FindWindowEx(hWorkerW, NULL, L"SHELLDLL_DefView", NULL);
    } while (hShellDLLDefView == NULL && hWorkerW != NULL);

    hListView =
        FindWindowEx(hShellDLLDefView, NULL, L"SysListView32", L"FolderView");

    if (!hListView) {
        puts("Failed to find SysListView32 window.");
        return EXIT_FAILURE;
    }

    int nIcons = ListView_GetItemCount(hListView);
    printf("Found %d items.", nIcons);

    SendMessage(hListView, LVM_SETITEMPOSITION, 2, MAKELPARAM(500, 500));
    return EXIT_SUCCESS;
}
© www.soinside.com 2019 - 2024. All rights reserved.