码迷,mamicode.com
首页 > Windows程序 > 详细

windows应用入门开发

时间:2020-07-29 21:45:17      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:位置   字符串长度   ram   menu   parent   lse   tcl   set   gis   

视频参考:鱼C工作室 小甲鱼

书籍参考:windows程序设计第五版,没错,作者就是写编码:隐匿在计算机软硬件背后的语言这本书的

一个简单的小工具,鼠标连点

源码:

#include <windows.h>
#include <strsafe.h>  //安全字符串

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {  //窗口过程
    HDC hdc;  //设备环境句柄
    PAINTSTRUCT ps;
    RECT rect;

    TCHAR str[200];  //字符串缓冲区
    size_t len;  //字符串长度

    HPEN hpen, hpen1;  //画笔
    HBRUSH hbrush, hbrush1;  //画刷

    static ATOM atom = GlobalAddAtom(TEXT("lvy"));  //热键唯一ID

    RECT rects;  //保存窗口坐标信息

    TEXTMETRIC tm;  //存放字体高度等信息
    static int fontWeight;  //字体宽度
    static int fontHeight;  //字体高度

    static int type = 1;  //类型:左键还是右键 左键用1代表 右键用2代表
    static float interval[] = {0.01, 0.03, 0.05, 0.1, 0.3, 0.5, 1, 3, 5};  //间隔时间
    static int item = 0;  //当前选择的是第几项
    static int isStart = 0;  //当前状态,0代表没有开始点击,1代表开始啦

    int mouseX, mouseY;  //保存鼠标的坐标信息

    switch (message) {  //监听各种消息

    case WM_CREATE:  //监听窗口被创建
        hdc = GetDC(hwnd);
        GetTextMetrics(hdc, &tm);  //获取字体高度等信息
        fontWeight = tm.tmAveCharWidth;
        fontHeight = tm.tmHeight + tm.tmExternalLeading;
        RegisterHotKey(hwnd, atom, 0, VK_F8);  //注册热键F8
        ReleaseDC(hwnd, hdc);
        return 0;

    case WM_TIMER:  //计时器
        if (type == 1) {  //左键
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        else {  //右键
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
        }
        return 0;

    case WM_HOTKEY:  //监听热键
        //SetCursorPos(0, 0);
        if (isStart == 0) {
            SetTimer(hwnd, 1, interval[item] * 1000, NULL);
            isStart = 1;
        }
        else if (isStart == 1) {
            KillTimer(hwnd, 1);  //销毁计时器
            isStart = 0;
        }
        else {}
        return 0;

    case WM_PAINT:  //监听窗口重绘
        hdc = BeginPaint(hwnd, &ps);  //获取设备句柄
        GetClientRect(hwnd, &rect);
        //绘制背景
        hpen = CreatePen(PS_SOLID, 1, RGB(122, 207, 152));  //创建画笔
        SelectObject(hdc, hpen);  //把画笔选入设备环境
        hbrush = CreateSolidBrush(RGB(122, 207, 152));  //创建画刷
        SelectObject(hdc, hbrush);  //把画刷选入设备环境
        Rectangle(hdc, 0, 0, 400, 3);  //上边框
        Rectangle(hdc, 0, 297, 400, 300);  //下边框
        Rectangle(hdc, 0, 3, 3, 297);  //左边框
        Rectangle(hdc, 397, 3, 400, 297);  //右边框
        hpen1 = CreatePen(PS_SOLID, 1, RGB(79, 185, 111));
        SelectObject(hdc, hpen1);
        DeleteObject(hpen);
        hbrush1 = CreateSolidBrush(RGB(79, 185, 111));
        SelectObject(hdc, hbrush1);
        DeleteObject(hbrush);
        Rectangle(hdc, 3, 3, 397, 297);  //中间的背景
        //绘制标题栏
        SetTextColor(hdc, RGB(255, 255, 255));  //设置字体颜色
        SetBkMode(hdc, TRANSPARENT);  //设置字体背景色为透明
        TextOut(hdc, 10, 5, TEXT("鼠标连点器2020正式版(吕洋)_v 1.0.0"), 24);
        SetTextColor(hdc, RGB(21, 93, 43));
        TextOut(hdc, 370, 5, TEXT("×"), 1);
        //绘制分割线
        hpen = CreatePen(PS_SOLID, 1, RGB(245, 233, 135));
        SelectObject(hdc, hpen);
        hbrush = CreateSolidBrush(RGB(245, 233, 135));
        SelectObject(hdc, hbrush);
        DeleteObject(hpen1);
        DeleteObject(hbrush1);
        Rectangle(hdc, 0, fontHeight + 10, 400, fontHeight + 11);
        //绘制主体
        SetTextColor(hdc, RGB(255, 255, 255));
        TextOut(hdc, 30, fontHeight + 20, TEXT("点击类型:鼠标 "), 8);
        if (type == 1) {
            SetTextColor(hdc, RGB(255, 255, 0));
            TextOut(hdc, 30 + fontWeight * 16, fontHeight + 20, TEXT("左键"), 2);
            SetTextColor(hdc, RGB(255, 255, 255));
            TextOut(hdc, 30 + fontWeight * 21, fontHeight + 20, TEXT("右键"), 2);
        }
        else {
            SetTextColor(hdc, RGB(255, 255, 255));
            TextOut(hdc, 30 + fontWeight * 16, fontHeight + 20, TEXT("左键"), 2);
            SetTextColor(hdc, RGB(255, 255, 0));
            TextOut(hdc, 30 + fontWeight * 21, fontHeight + 20, TEXT("右键"), 2);
        }
        SetTextColor(hdc, RGB(255, 255, 255));
        TextOut(hdc, 30, fontHeight * 2 + 30, TEXT("间隔时间:"), 5);
        for (int i = 0, j = 0, x = 0; i < 9; ++i) {
            SetTextColor(hdc, RGB(255, 255, 255));
            if (i == item) {
                SetTextColor(hdc, RGB(255, 255, 0));
            }
            StringCchPrintf(str, 200, TEXT("%1.2f"), interval[i]);
            StringCchLength(str, 200, &len);
            TextOut(hdc, 30 + fontWeight * (11 + j * 5), fontHeight * (2 + x) + 30, str, len);
            ++j;
            if (j % 3 == 0) {
                j = 0;
                ++x;
            }
        }
        Rectangle(hdc, 20, (fontHeight + 10) * 5, 380, (fontHeight + 10) * 9);
        SetTextColor(hdc, RGB(190, 119, 255));
        TextOut(hdc, 30, (fontHeight + 12) * 5, TEXT("使用说明:"), 5);
        TextOut(hdc, 30, (fontHeight + 12) * 6, TEXT("1:鼠标移动到需要点击的位置"), 14);
        TextOut(hdc, 30, (fontHeight + 12) * 7, TEXT("2:F8启动/关闭"), 9);
        EndPaint(hwnd, &ps);  //释放设备句柄
        DeleteObject(hpen);  //释放画笔
        DeleteObject(hbrush);  //释放画刷
        return 0;
    
    case WM_LBUTTONUP:  //客户区鼠标抬起
        mouseX = LOWORD(lParam);
        mouseY = HIWORD(lParam);
        if (mouseX > 360 && mouseY < 25) {  //关闭按钮
            DestroyWindow(hwnd);
        }
        else if (mouseX > (30 + fontWeight * 16) && mouseX < (30 + fontWeight * 20) && mouseY > (fontHeight + 20) && mouseY < (fontHeight * 2 + 20)) {  //鼠标左键
            type = 1;
        }
        else if (mouseX > (30 + fontWeight * 21) && mouseX < (30 + fontWeight * 25) && mouseY > (fontHeight + 20) && mouseY < (fontHeight * 2 + 20)) {  //鼠标右键
            type = 2;
        }
        else if (mouseY > (fontHeight * 2 + 30) && mouseY < (fontHeight * 3 + 30)) {
            if (mouseX > (30 + fontWeight * 11) && mouseX < (30 + fontWeight * 16)) {
                item = 0;
            }
            else if (mouseX > (30 + fontWeight * 16) && mouseX < (30 + fontWeight * 21)) {
                item = 1;
            }
            else if (mouseX > (30 + fontWeight * 21) && mouseX < (30 + fontWeight * 26)) {
                item = 2;
            }
            else {}
        }
        else if (mouseY > (fontHeight * 3 + 30) && mouseY < (fontHeight * 4 + 30)) {
            if (mouseX > (30 + fontWeight * 11) && mouseX < (30 + fontWeight * 16)) {
                item = 3;
            }
            else if (mouseX > (30 + fontWeight * 16) && mouseX < (30 + fontWeight * 21)) {
                item = 4;
            }
            else if (mouseX > (30 + fontWeight * 21) && mouseX < (30 + fontWeight * 26)) {
                item = 5;
            }
            else {}
        }
        else if (mouseY > (fontHeight * 4 + 30) && mouseY < (fontHeight * 5 + 30)) {
            if (mouseX > (30 + fontWeight * 11) && mouseX < (30 + fontWeight * 16)) {
                item = 6;
            }
            else if (mouseX > (30 + fontWeight * 16) && mouseX < (30 + fontWeight * 21)) {
                item = 7;
            }
            else if (mouseX > (30 + fontWeight * 21) && mouseX < (30 + fontWeight * 26)) {
                item = 8;
            }
            else {}
        }
        else {

        }
        InvalidateRect(hwnd, NULL, TRUE);
        UpdateWindow(hwnd);
        return 0;

    case WM_NCHITTEST:
        GetWindowRect(hwnd, &rects);  //获取窗口相对于屏幕的位置
        if ((LOWORD(lParam) - rects.left) < 360 && (HIWORD(lParam) - rects.top) < 25) {  //非客户区,可以拖动
            return HTCAPTION;
        }
        return HTCLIENT;  //客户区

    case WM_DESTROY:  //窗口关闭时
        UnregisterHotKey(hwnd, atom);  //释放热键
        if (isStart == 1) {
            KillTimer(hwnd, 1);
        }
        PostQuitMessage(0);
        return 0;

    }
    return DefWindowProc(hwnd, message, wParam, lParam);  //交给windows处理
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {  //主函数
    static TCHAR szAppName[] = TEXT("Mouse");  //定义窗口类名称
    HWND hwnd;  //窗口句柄
    MSG msg;  //消息
    WNDCLASS wndclass;  //窗口类
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);  //屏幕宽度
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);  //屏幕高度

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;  //设置窗口过程(回调函数)
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass)) {  //注册窗口类
        MessageBox(NULL, TEXT("this program requires windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,  //创建窗口
                        TEXT("鼠标连点"),
                        WS_POPUP,
                        screenWidth / 2 - 200,
                        screenHeight / 2 - 150,
                        400,
                        300,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
    ShowWindow(hwnd, iCmdShow);  //显示窗口
    UpdateWindow(hwnd);  //更新窗口
    while (GetMessage(&msg, NULL, 0, 0)) {  //消息循环
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

界面挺好看的, 有兴趣的可以用VS建个项目运行一下,有空我会把编译后的可执行文件放到我的博客

windows应用入门开发

标签:位置   字符串长度   ram   menu   parent   lse   tcl   set   gis   

原文地址:https://www.cnblogs.com/zhenfeishi/p/13399273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!