Windows编程——API小球点击

Posted ViKyanite.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows编程——API小球点击相关的知识,希望对你有一定的参考价值。

 

 

//windows.h文件中包含应用程序中所需的数据类型和数据结构的定义
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define PI acos(-1)
#define LR (rect.left+rect.right)
#define TD (rect.bottom+rect.top)
#define R 50
#define PI acos(-1)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口函数说明
//---------以下初始化窗口类--------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
    HWND hwnd;
    MSG Msg;
    WNDCLASS wndclass;
    char lpszClassName[] = "窗口";     //窗口类名
    char lpszTitle[] = "My_Windows";    //窗口标题名

    //窗口类的定义
    wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;                  //窗口类型为默认类型
    wndclass.lpfnWndProc = WndProc;      //窗口处理函数为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 = lpszClassName; //窗口类名为“窗口示例”
//-----------以下进行窗口类的注册
    if (!RegisterClass(&wndclass))//如果注册失败则发出警吿声音
    {
        MessageBeep (0);
        return FALSE;
    }

//创建窗口
    hwnd = CreateWindow(
                        lpszClassName, //窗口类名
                        lpszTitle,      //窗口实例的标题名
                        WS_OVERLAPPEDWINDOW, //窗口 的风格
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,  //窗口左上角坐标为默认值
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,  //窗口的髙和宽为默认值
                        NULL,           //此窗口无父窗口
                        NULL,           //此窗口无主菜单
                        hInstance,      //创建此窗口应用程序的当前句柄
                        NULL            //不使用该值
                        );



    ShowWindow( hwnd, nCmdShow);        //显示窗口
    UpdateWindow(hwnd);                 //绘制用户区
    while( GetMessage(&Msg, NULL, 0, 0)) //消息循环
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;                  //程序终止时将信息返回系统
}



LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    HDC hDC, hdc_old;
    HBITMAP hBmp, hBkBmp;
    RECT rect;
    HBRUSH hBrush;
    PAINTSTRUCT Ps;
    TEXTMETRIC tm;
    HFONT hf;
    HPEN hPen;
    SIZE Sz;
    COLORREF color[3] = {RGB(255,0,0), RGB(0,255,0), RGB(0,0,255)};

    char str[150];
    static int mx, my;
    static int x = 200, y = 200, r = 50;
    static int vx = 2, vy = 3;
    static int uddis = 1;
    static int lrdis = 1;
    static int index = 0;

    int nextx, nexty;
    static RECT rec;
    switch(message)
    {
    case WM_MOUSEMOVE:
        mx = LOWORD(lParam);
        my = HIWORD(lParam);
        return 0;
    case WM_LBUTTONDOWN:
        if (mx <= rec.right && mx >= rec.left && my <= rec.bottom && my >= rec.top)
                index = (index+1)%3;
        InvalidateRect(hWnd, NULL, 1);
        return 0;
    case WM_LBUTTONUP:

        return 0;
    case WM_ERASEBKGND:
        return 1;
    case WM_PAINT:
//        GetClientRect(hWnd,&rect);
//        hDC = BeginPaint(hWnd, &Ps);
        hdc_old = BeginPaint(hWnd, &Ps);
        hDC = CreateCompatibleDC(hdc_old);
        GetClientRect(hWnd,&rect);
        hBmp = CreateCompatibleBitmap(hdc_old,rect.right,rect.bottom);
        SelectObject(hDC,hBmp);

        Rectangle(hDC, rect.left, rect.top,  rect.right, rect.bottom);
        hPen = CreatePen(PS_SOLID, 3, RGB(255,255,255));
        hBrush = CreateSolidBrush(color[index]);

        SelectObject(hDC, hPen);
        SelectObject(hDC, hBrush);

        Ellipse(hDC, x-r, y-r, x+r, y+r);
        rec.left = x-r;
        rec.top = y-r;
        rec.right = x+r;
        rec.bottom = y+r;

        DeleteObject(hPen);
        DeleteObject(hBrush);
        nextx = x + lrdis * vx;
        nexty = y + uddis *vy;
        if (nextx < rect.left+r)
        {
            nextx = rect.left + r;
            lrdis = -lrdis;
        }
        if (nexty < rect.top + r)
        {
            nexty = rect.top + r;
            uddis = -uddis;
        }
        if (nextx > rect.right - r)
        {
            nextx > rect.right - r;
            lrdis = -lrdis;
        }
        if (nexty > rect.bottom - r)
        {
            nexty = rect.bottom - r;
            uddis = -uddis;
        }
        x = nextx;
        y = nexty;

        Sleep(5);
        InvalidateRect(hWnd, NULL, 1);

        BitBlt(hdc_old,0,0,rect.right,rect.bottom,hDC,0,0,SRCCOPY);
        DeleteObject(hBmp);
        DeleteDC(hDC);
        ReleaseDC(hWnd, hDC); //释放
        EndPaint(hWnd, &Ps); //结束缓制

        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    default:
        return  DefWindowProc(hWnd,message,wParam,lParam);
    }
        return 0;
}

 

以上是关于Windows编程——API小球点击的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

Vue实现购物小球抛物线的方法实例

Windows程序设计之简单的动画效果(小球弹一弹)

C语言Windows API编程中,创建多个edit控件,随机点击其中一个edit按钮,怎么得到该edit按钮的标识符?

scratch接球游戏 电子学会图形化编程scratch等级考试三级真题和答案解析2022年3月

scratch接球游戏 电子学会图形化编程scratch等级考试三级真题和答案解析2022年3月