Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525460
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-11 14:14:23

这里面有两个个比较有意思的SetCursorPos与GetCurSorPos, 它们的坐标可全是相对于屏幕的。
而SendMessage这个函数也是比较有意思的, 里面还有一个MAKELONG的宏。
代码:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    static TCHAR szAppName[] = TEXT("HIT TEST");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.lpfnWndProc = WndProc;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Register failure..."),
            szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,
        szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static int cxClient, cyClient, cxBlock, cyBlock, state[6][6];
    HDC hdc;
    int i, j, x, y;
    PAINTSTRUCT ps;
    static POINT pt;

    switch(message) {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        cxBlock = cxClient / 6;
        cyBlock = cyClient / 6;
        return 0;

    case WM_KEYDOWN:
        GetCursorPos(&pt);
        ScreenToClient(hwnd, &pt);
        x = max(0, min(5, pt.x / cxBlock));
        y = max(0, min(5, pt.y / cyBlock));
        switch(wParam) {
        case VK_UP:
            y--;
            break;
        case VK_DOWN:
            y++;
            break;
        case VK_LEFT:
            x--;
            break;
        case VK_RIGHT:
            x++;
            break;
        case VK_HOME:
            x = 0;
            break;
        case VK_END:
            x = 5;
            break;
        case VK_RETURN:
            SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
                MAKELONG(cxBlock * x, cyBlock * y));
            break;
        }
        x = (x + 6) % 6;
        y = (y + 6) % 6;
        pt.x = cxBlock * (x + 0.5);
        pt.y = cyBlock * (y + 0.5);
        
        ClientToScreen(hwnd, &pt);
        SetCursorPos(pt.x, pt.y);
        return 0;

    case WM_LBUTTONDOWN:
        x = LOWORD(lParam) / cxBlock;
        y = HIWORD(lParam) / cyBlock;



        if(x < 6 && y < 6) {
            state[x][y] ^= 1;
            InvalidateRect(hwnd, NULL, FALSE);
        }
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        for(i = 0; i < 6; i++)
            for(j = 0; j < 6; j++) {
                Rectangle(hdc, i * cxBlock, j * cyBlock,
                    (i + 1) * cxBlock, (j + 1) * cyBlock);
                if(state[i][j]) {
                    MoveToEx(hdc, i * cxBlock, j * cyBlock, NULL);
                    LineTo(hdc, (i + 1) * cxBlock, (j + 1) * cyBlock);
                    MoveToEx(hdc, (i + 1) * cxBlock, j * cyBlock, NULL);
                    LineTo(hdc, i * cxBlock, (j + 1) * cyBlock);
                }
            }
            EndPaint(hwnd, &ps);
            return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;    
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(787) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~