コード:
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h> // sprintf
#include <stdlib.h> // getenv
#define CHILD_ID 100
#define MENU_ITEM 200
HWND hEdit;
HMENU hMenu;
int nItem;
char *dir = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wp, LPARAM lp)
{
switch (message) {
case WM_CREATE: {
WIN32_FIND_DATA wfd;
HANDLE h;
char path[MAX_PATH];
dir = getenv("APPDATA");
if (!dir) return 1;
sprintf(path, "%s/Microsoft/Windows/SendTo/*", dir);
h = FindFirstFile(path, &wfd);
if (h == INVALID_HANDLE_VALUE) return 2;
hMenu = CreatePopupMenu();
nItem = 0;
do {
if (wfd.cFileName[0] != '.')
AppendMenu(hMenu, MF_STRING, MENU_ITEM + nItem++, wfd.cFileName);
} while (FindNextFile(h, &wfd));
break;
}
case WM_RBUTTONDOWN: {
POINT pt;
pt.x = LOWORD(lp);
pt.y = HIWORD(lp);
ClientToScreen(hWnd, &pt);
TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
break;
}
case WM_COMMAND: {
char path[MAX_PATH], app[MAX_PATH], name[MAX_PATH];
WORD id = LOWORD(wp);
if (id < MENU_ITEM || id >= MENU_ITEM + nItem)
return DefWindowProc(hWnd, message, wp, lp);
GetWindowText(hEdit, name, sizeof name);
GetMenuString(hMenu, id, app, sizeof app, 0);
sprintf(path, "%s/Microsoft/Windows/SendTo/%s", dir, app);
ShellExecute(NULL, "open", path, name, NULL, SW_SHOWNORMAL);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wp, lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE h, char *s, int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "test";
RegisterClass(&wc);
hWnd = CreateWindow("test", "SendTo", WS_OVERLAPPEDWINDOW,
160, 120, 480, 240, NULL, NULL, hInst, NULL);
hEdit = CreateWindow("edit", NULL,
WS_VISIBLE | WS_CHILD |ES_LEFT | ES_AUTOHSCROLL,
40, 40, 400, 20, hWnd, (HMENU)CHILD_ID, hInst, NULL);
CreateWindow("static",
"Enter a file path and click here with the right button",
SS_SIMPLE | WS_CHILD | WS_VISIBLE,
50, 80, 360, 60, hWnd, NULL, hInst, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass("test", hInst);
return msg.wParam;
}