WinApp: カラーピッカー

概要:
カラーピッカー
  • [ESC]か[右クリック]で終了
  • 十字キーで、カーソル移動
  • 右CTRLキーでクリップボードにコピー
ファイル:

ColorPicker20151004.zip

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#define UNICODE
#define _UNICODE
#define STRICT
#define WIN32_LEAN_AND_MEAN
#pragma warning(push, 4)
#pragma warning(disable:4100)
#include <windows.h>
// without msvcr
#pragma comment(linker, "/subsystem:windows")
#pragma comment(linker, "/entry:\"WinMainCRTStartup\"")
#pragma function(memset)
void* memset(void* buf, int ch, size_t len)
{
    unsigned char* ptr = (unsigned char*)buf;
    const unsigned char c = (const unsigned char)(ch & 0xFF);
    while (len--) {
        *ptr++ = c;
    }
    return buf;
}

const TCHAR gszAppName[] = TEXT("ColorPicker");
const TCHAR gszMSGDefault[] = TEXT("Copy to clipboard in RIGHT CTRL Key.");
const int GW_BORDER = 1; //< 線の幅
const int GW_CLIENT = 376; //< ウィンドウクライアント領域(枠内)の幅
const int GW_UP = GW_CLIENT / 2; //< 上の描画領域(マウスカーソル付近のプレビュー)の幅
const int GH_UP = 100; //< 上の描画領域の高さ
const int GW_DN = GW_CLIENT; //< 下の描画領域(ステータス)の幅(メモリーデバイスコンテキストのサイズ)
const int GH_DN = 44; //< 下の描画領域の高さ
const int GL_CD = 32; //< Color Display(色見本のサイズ)
const int GL_CH = 4; //< Cross-Hair(照準の長さ)
const UINT_PTR GTIMER_INTERVAL = 32U; //< タイムアウト値
const RECT grcDN = { 0, GH_UP, GW_DN, GH_UP + GH_DN }; //< ステータスバーの背景
const RECT grcCD = { //< ステータスバーの色見本の枠を含む表示領域
    (GH_DN - GL_CD) / 2 - GW_BORDER, (GH_DN - GL_CD) / 2 - GW_BORDER + GH_UP,
    (GH_DN + GL_CD) / 2 + GW_BORDER, (GH_DN + GL_CD) / 2 + GW_BORDER + GH_UP
};

BOOL WINAPI SetClipboardText(HWND hWndNewOwner, LPCTSTR lpString)
{
    BOOL retval = FALSE;
    if (::OpenClipboard(hWndNewOwner)) {
        const int len = ::lstrlen(lpString) + 1;
        const HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, len * sizeof(TCHAR));
        if (hglbCopy != NULL) {
            LPTSTR lptstrCopy = (LPTSTR)::GlobalLock(hglbCopy);
            if (lptstrCopy != NULL) {
                retval = ((::lstrcpyn(lptstrCopy, lpString, len) != NULL) ? TRUE : FALSE);
                ::GlobalUnlock(hglbCopy);
                retval = ((retval && ::EmptyClipboard()
#ifdef UNICODE
                    && (::SetClipboardData(CF_UNICODETEXT, hglbCopy) != NULL)
#else
                    && (::SetClipboardData(CF_TEXT, hglbCopy) != NULL)
#endif
                ) ? TRUE : FALSE);
            }
            if (!retval) {
                ::GlobalFree(hglbCopy);
            }
        }
        ::CloseClipboard();
    }
    return retval;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HDC hdcDesktop = NULL, hdcWnd = NULL, hdcMemory = NULL;
    static HBITMAP hbitmapMemory = NULL;
    static UINT_PTR nTimerID = 0U;
    static TCHAR szRGB[8] = { 0 };
    static TCHAR szMSG[64] = { 0 };
    static DWORD dwMSGCount = 0U;
    static LOGFONT logfont = { 0 };
    static POINT pt = { 0, 0 };
    switch (uMsg)
    {
    case WM_CREATE: //< デバイスコンテキストの準備
        hdcDesktop = ::GetDC(::GetDesktopWindow());
        hdcWnd = ::GetDC(hWnd);
        hdcMemory = ::CreateCompatibleDC(hdcWnd);
        hbitmapMemory = (HBITMAP)::SelectObject(hdcMemory, ::CreateCompatibleBitmap(hdcWnd, GW_CLIENT, GH_UP + GH_DN));
        {
            const HFONT hfont = (HFONT)::GetStockObject(ANSI_FIXED_FONT);
            ::GetObject(hfont, sizeof(logfont), &logfont);
            ::SelectObject(hdcMemory, hfont);
        }
        ::SetBkMode(hdcMemory, TRANSPARENT);
        nTimerID = ::SetTimer(hWnd, NULL, GTIMER_INTERVAL, NULL);
        break;
    case WM_LBUTTONDOWN: //< 左クリックのドラッグでウィンドウを動かす
        if (::ReleaseCapture()) {
            ::SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        }
        break;
    case WM_RBUTTONUP: //< 右クリックで終了
        ::PostMessage(hWnd, WM_CLOSE, 0, 0);
        break;
    case WM_KEYUP: //< ESCで終了。十字キーでカーソル移動
        if (wParam == VK_ESCAPE) {
            ::PostMessage(hWnd, WM_CLOSE, 0, 0);
        } if ((wParam == VK_LEFT) || (wParam == VK_UP) || (wParam == VK_RIGHT) || (wParam == VK_DOWN)) {
            if (::GetCursorPos(&pt) != 0) {
                if (wParam & 0x01) {
                    // 0x25 100101 VK_LEFT
                    // 0x27 100111 VK_RIGHT
                    pt.x += ((wParam & 0x02) ? +1 : -1);
                } else {
                    // 0x26 100110 VK_UP
                    // 0x28 101000 VK_DOWN
                    pt.y += ((wParam & 0x02) ? -1 : +1);
                }
                ::SetCursorPos(pt.x , pt.y);
            }
        }
        break;
    case WM_TIMER:
    {
        static const int TEXT_X = grcCD.right + grcCD.left + GW_BORDER; //< 横位置
        const int TEXT_Y_MARGIN = ((GH_DN - (logfont.lfHeight * 2)) / 3); //< 行間
        const int TEXT_Y1 = GH_UP + TEXT_Y_MARGIN; //< 1行目
        const int TEXT_Y2 = TEXT_Y1 + logfont.lfHeight + TEXT_Y_MARGIN; //< 2行目
        RECT rcText2 = { TEXT_X, TEXT_Y2, GW_CLIENT - grcCD.left, TEXT_Y2 + logfont.lfHeight + TEXT_Y_MARGIN }; //< 2行目
        TCHAR szBuf[32] = {};
        // クリアしてからマウスカーソル付近のDCをhdcMemoryにコピー
        ::GetCursorPos(&pt);
        ::BitBlt(hdcMemory, 0, 0, GW_UP, GH_UP, NULL, 0, 0, BLACKNESS);
        ::BitBlt(hdcMemory, 0, 0, GW_UP, GH_UP, hdcDesktop, pt.x - (GW_UP / 2), pt.y - (GH_UP / 2), SRCCOPY);
        // 中央の照準を反転で表現(右上左下)
        ::BitBlt(hdcMemory, (GW_UP / 2) - GL_CH - 1, (GH_UP / 2), GL_CH, 1, NULL, 0, 0, DSTINVERT);
        ::BitBlt(hdcMemory, (GW_UP / 2), (GH_UP / 2) - GL_CH - 1, 1, GL_CH, NULL, 0, 0, DSTINVERT);
        ::BitBlt(hdcMemory, (GW_UP / 2) + 2, (GH_UP / 2), GL_CH, 1, NULL, 0, 0, DSTINVERT);
        ::BitBlt(hdcMemory, (GW_UP / 2), (GH_UP / 2) + 2, 1, GL_CH, NULL, 0, 0, DSTINVERT);
        ::FillRect(hdcMemory, &grcDN, (HBRUSH)(COLOR_3DFACE + 1)); //< ステータスバーの背景
        ::FrameRect(hdcMemory, &grcCD, ::GetSysColorBrush(COLOR_3DSHADOW)); //< ステータスバーの色見本の枠
        // ステータスバーの色見本
        ::StretchBlt(
            hdcMemory, (GH_DN - GL_CD) / 2, GH_UP + ((GH_DN - GL_CD) / 2),
            GL_CD, GL_CD, hdcMemory, GW_UP / 2, GH_UP / 2, 1, 1, SRCCOPY
        );
        ::wsprintf(szBuf, TEXT("#%06X"), ::GetPixel(hdcMemory, GW_UP / 2, GH_UP / 2)); //< 中央の色を取得
        const DWORD dwTickCount = ::GetTickCount();
        if (dwTickCount < dwMSGCount) {
            ::DrawText(hdcMemory, szMSG, ::lstrlen(szMSG), &rcText2, DT_RIGHT); //< 2行目
        } else {
            ::DrawText(hdcMemory, gszMSGDefault, ::lstrlen(gszMSGDefault), &rcText2, DT_RIGHT); //< 2行目
            // 右CTRLを押してたら、クリップボードにコピー
            if ((::GetKeyState(VK_RCONTROL) < 0) && (::lstrcmp(szRGB, szBuf) != 0)) {
                SetClipboardText(hWnd, ::lstrcpy(szRGB, szBuf));
                ::lstrcpy(szMSG, TEXT("Copy to clipboard !!!"));
                dwMSGCount = dwTickCount + 1000;
            }
        }
        ::wsprintf(szBuf + ::lstrlen(szBuf), TEXT(" (%4d, %4d)"), pt.x, pt.y);
        ::TextOut(hdcMemory, TEXT_X, TEXT_Y1, szBuf, ::lstrlen(szBuf)); //< 1行目
        // 描画
        ::StretchBlt(hdcWnd, GW_BORDER, GW_BORDER, GW_CLIENT, GH_UP * 2, hdcMemory, 0, 0, GW_UP, GH_UP, SRCCOPY);
        ::BitBlt(hdcWnd, GW_BORDER, GW_BORDER + (GH_UP * 2) + GW_BORDER, GW_DN, GH_DN, hdcMemory, 0, GH_UP, SRCCOPY);
        break;
    }
    case WM_DESTROY:
        ::KillTimer(hWnd, nTimerID);
        ::DeleteObject(::SelectObject(hdcMemory, hbitmapMemory));
        ::DeleteDC(hdcMemory);
        ::ReleaseDC(hWnd, hdcWnd);
        ::ReleaseDC(::GetDesktopWindow(), hdcDesktop);
        ::PostQuitMessage(0);
        break;
    default:
        return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}

//int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
EXTERN_C void __cdecl WinMainCRTStartup()
{
    WNDCLASSEX wcx = {};
    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.lpfnWndProc = WndProc;
    wcx.hInstance = ::GetModuleHandle(NULL);
    wcx.hCursor = ::LoadCursor(NULL, IDC_ARROW);
    wcx.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME + 1); //< WS_BORDER COLOR
    wcx.lpszClassName = gszAppName;
    if ((::RegisterClassEx(&wcx) == 0) || (::CreateWindowEx(
        WS_EX_TOPMOST, wcx.lpszClassName, gszAppName, WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
        GW_BORDER * 2 + GW_CLIENT, GW_BORDER * 3 + (GH_UP * 2) + GH_DN, NULL, NULL, wcx.hInstance, NULL
    ) == NULL)) {
        LPVOID lpAllocBuffer = NULL;
        ::FormatMessage(
            FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, ::GetLastError(),
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpAllocBuffer, 0, NULL
        );
        ::MessageBox(::GetActiveWindow(), (LPTSTR)lpAllocBuffer, gszAppName, MB_OK);
        ::HeapFree(::GetProcessHeap(), 0L, lpAllocBuffer);
        ::ExitProcess(static_cast<UINT>(0)); //return 0;
    } else {
        MSG msg;
        BOOL bRet;
        while ((bRet = ::GetMessage(&msg, NULL, 0, 0)) != 0) {
            if (bRet != -1) {
                (VOID)::DispatchMessage(&msg);
            }
        }
        ::ExitProcess(static_cast<UINT>(msg.wParam)); //return (int)msg.wParam;
    }
}