サスペンド直前にクリップボードを消去するプログラム
なんかすごくニッチだけどちょっと必要になったので作ってみた。
どうしても動かしてみたいという人は、
$ gcc -mwindows -mno-cygwin -o ccb ccb.c
もしくは
C>cl ccb.c user32.lib shell32.lib
とかするとコンパイルできます。
iconData は ICO ファイル の中身そのままです。ICO ファイルは GIMP で作りました。ICO ファイルのヘッダをスキップするために +22 offset してます。CreateIconFromResource() の引数についての情報は wine のソースをあさった。
追記: PostQuitMessage()をあらぬところで呼んでいたので修正。いやはや、Platform SDKなんて15年くらいまともに使ってないので忘れまくりですな。あとアプリ終了時に一応アイコンをタスクトレイから消すようにしました。
/* * CCB - a program that clears the clipboard right before the computer is * suspended (hibernated). * * Copyright (c) 2008 Moriyoshi Koizumi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #include <windows.h> #include <string.h> #include <shellapi.h> static const char progname[] = "ccb"; static BYTE iconData[] = { 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x10, 0x10, 0x00, 0x01, 0x00, 0x04, 0x00, 0x28, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x53, 0x66, 0x00, 0x5f, 0x67, 0x7f, 0x00, 0x7f, 0x90, 0xb0, 0x00, 0x66, 0xaf, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x15, 0x51, 0x00, 0x15, 0x51, 0x00, 0x00, 0x00, 0x14, 0x44, 0x11, 0x44, 0x41, 0x11, 0x21, 0x00, 0x14, 0x44, 0x44, 0x44, 0x44, 0x45, 0x14, 0x10, 0x01, 0x35, 0x55, 0x53, 0x34, 0x44, 0x45, 0x10, 0x01, 0x51, 0x11, 0x15, 0x43, 0x44, 0x44, 0x10, 0x14, 0x15, 0x55, 0x51, 0x43, 0x44, 0x40, 0x00, 0x14, 0x15, 0x00, 0x51, 0x41, 0x00, 0x00, 0x00, 0x14, 0x41, 0x00, 0x14, 0x41, 0x00, 0x00, 0x00, 0x14, 0x44, 0x55, 0x44, 0x41, 0x00, 0x00, 0x00, 0x01, 0x40, 0x55, 0x04, 0x10, 0x00, 0x00, 0x00, 0x01, 0x44, 0x55, 0x44, 0x11, 0x00, 0x00, 0x00, 0x14, 0x51, 0x11, 0x15, 0x41, 0x00, 0x00, 0x00, 0x14, 0x10, 0x00, 0x01, 0x41, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x9e, 0x7f, 0x00, 0x00, 0x0c, 0x3f, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x80, 0x7f, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x1e, 0x3f, 0x00, 0x00, 0xbf, 0x7f, 0x00, 0x00 }; static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT retval = 0; switch (uMsg) { case WM_POWERBROADCAST: if (wParam == PBT_APMSUSPEND) { if (OpenClipboard(hWnd)) { EmptyClipboard(); CloseClipboard(); } else { MessageBox(hWnd, "Cannot clear the clipboard", "Error", MB_OK | MB_ICONEXCLAMATION); } } break; case WM_CREATE: SetWindowLong(hWnd, 0, 0); break; case WM_USER + 0x2000: if (lParam == WM_LBUTTONDOWN) { POINT pos; HMENU hCtxMenu; hCtxMenu = (HMENU)GetWindowLong(hWnd, 0); if (hCtxMenu == (HMENU)NULL) { if (!GetCursorPos(&pos)) break; hCtxMenu = CreatePopupMenu(); if (hCtxMenu == (HMENU)NULL) break; { MENUITEMINFO mii; mii.cbSize = sizeof(mii); mii.fMask = MIIM_ID | MIIM_STRING; mii.wID = 1; mii.dwTypeData = "Quit"; mii.cch = sizeof("Quit") - 1; InsertMenuItem(hCtxMenu, 0, FALSE, &mii); } SetWindowLong(hWnd, 0, (LONG)hCtxMenu); SetForegroundWindow(hWnd); TrackPopupMenu(hCtxMenu, TPM_RIGHTALIGN | TPM_BOTTOMALIGN, pos.x, pos.y, 0, hWnd, NULL); DestroyMenu(hCtxMenu); } else { EndMenu(); SetWindowLong(hWnd, 0, 0); } } break; case WM_COMMAND: if (HIWORD(wParam) == 0 && LOWORD(wParam) == 1) DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: retval = DefWindowProc(hWnd, uMsg, wParam, lParam); } return retval; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hInstPrev, LPSTR lpszCmdLine, int nCmdShow) { int err = 0; ATOM aWndClass = (ATOM)NULL; HWND hWnd = (HWND)NULL; HICON hIcon = (HICON)NULL; if (hInstPrev != (HINSTANCE)NULL) return 2; hIcon = CreateIconFromResource(iconData + 22, sizeof(iconData) - 22, TRUE, 0x00030000); if (hIcon == (HICON)NULL) return -1; { WNDCLASSEX wndClassDesc; wndClassDesc.cbSize = sizeof(wndClassDesc); wndClassDesc.style = 0; wndClassDesc.lpfnWndProc = MyWndProc; wndClassDesc.cbClsExtra = 0; wndClassDesc.cbWndExtra = sizeof(HMENU); wndClassDesc.hInstance = hInstance; wndClassDesc.hIcon = hIcon; wndClassDesc.hIconSm = hIcon; wndClassDesc.hCursor = NULL; wndClassDesc.hbrBackground = NULL; wndClassDesc.lpszMenuName = NULL; wndClassDesc.lpszClassName = progname; aWndClass = RegisterClassEx(&wndClassDesc); if (aWndClass == (ATOM)NULL) { err = 1; goto out; } } hWnd = CreateWindowEx(WS_EX_TRANSPARENT, (LPCTSTR)(DWORD)aWndClass, progname, WS_OVERLAPPED, 0, 0, 1, 1, (HWND)NULL, (HMENU)NULL, hInstance, NULL); if (hWnd == NULL) { err = 1; goto out; } { NOTIFYICONDATA nid; nid.cbSize = sizeof(nid); nid.hWnd = hWnd; nid.uID = 1; nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; nid.uCallbackMessage = WM_USER + 0x2000; nid.hIcon = hIcon; strcpy(nid.szTip, progname); /* XXX: safe */ Shell_NotifyIcon(NIM_ADD, &nid); } { MSG msg; int i; while ((i = GetMessage(&msg, NULL, 0, 0)) != 0) { if (i == -1) { err = 1; goto out; } TranslateMessage(&msg); DispatchMessage(&msg); } err = msg.wParam; } out: if (hWnd != (HWND)NULL) { NOTIFYICONDATA nid; DestroyWindow(hWnd); nid.cbSize = sizeof(nid); nid.hWnd = hWnd; nid.uID = 1; nid.uFlags = 0; Shell_NotifyIcon(NIM_DELETE, &nid); } if (aWndClass != (ATOM)NULL) UnregisterClass((LPCTSTR)(DWORD)aWndClass, hInstance); if (hIcon != (HICON)NULL) DestroyIcon(hIcon); return err; }