博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[游戏模版4] Win32 显示鼠标位置
阅读量:6231 次
发布时间:2019-06-21

本文共 8751 字,大约阅读时间需要 29 分钟。

 

>_<:use MOUSE_MOVE message refresh the position information.

>_<:use LOWORD(lParam) get position x and use HIWORD(lParam) get position y 

1 //{
{NO_DEPENDENCIES}} 2 // Microsoft Visual C++ generated include file. 3 // Used by FE.RC 4 // 5 #define IDR_MAINFRAME 128 6 #define IDD_FE_DIALOG 102 7 #define IDD_ABOUTBOX 103 8 #define IDS_APP_TITLE 103 9 #define IDM_ABOUT 10410 #define IDM_EXIT 10511 #define IDS_HELLO 10612 #define IDI_FE 10713 #define IDI_SMALL 10814 #define IDC_FE 10915 #define IDC_MYICON 216 #define IDC_STATIC -117 // Next default values for new objects18 //19 #ifdef APSTUDIO_INVOKED20 #ifndef APSTUDIO_READONLY_SYMBOLS21 22 #define _APS_NEXT_RESOURCE_VALUE 12923 #define _APS_NEXT_COMMAND_VALUE 3277124 #define _APS_NEXT_CONTROL_VALUE 100025 #define _APS_NEXT_SYMED_VALUE 11026 #endif27 #endif
resourse.h
1 // stdafx.h : include file for standard system include files, 2 //  or project specific include files that are used frequently, but 3 //      are changed infrequently 4 // 5 #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_) 6 #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ 7  8 #if _MSC_VER > 1000 9 #pragma once10 #endif // _MSC_VER > 100011 12 #define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers13 14 15 // Windows Header Files:16 #include 
17 18 // C RunTime Header Files19 #include
20 #include
21 #include
22 #include
23 24 // Local Header Files25 26 // TODO: reference additional headers your program requires here27 28 //{
{AFX_INSERT_LOCATION}}29 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.30 31 #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
StdAfx.h

main.cpp

1 #include "stdafx.h"  2 #include "stdio.h"  3 #include "resourse.h"  4   5 #define MAX_LOADSTRING 100  6   7 // Global Variables:  8 HINSTANCE hInst;                                // current instance  9 TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text 10 TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text 11  12 // Foward declarations of functions included in this code module: 13 ATOM                MyRegisterClass(HINSTANCE hInstance); 14 BOOL                InitInstance(HINSTANCE, int); 15 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); 16 //======================================================================================== 17  18 int APIENTRY WinMain(HINSTANCE hInstance, 19                      HINSTANCE hPrevInstance, 20                      LPSTR     lpCmdLine, 21                      int       nCmdShow) 22 { 23      // TODO: Place code here. 24     MSG msg; 25  26     MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码; 27  28     // 调用InitInstance函数,进行初始化操作; 29     if (!InitInstance (hInstance, nCmdShow)) 30     { 31         return FALSE; 32     } 33  34     // 消息循环(通过消息循环来获取信息, 35     //进行必要的键盘信息转换而后将控制权交给操作系统, 36     //有操作系统决定哪个程序的消息处理函数处理消息 37     while (GetMessage(&msg, NULL, 0, 0)) //获取程序消息 38     { 39             TranslateMessage(&msg);//转换伪码及字符 40             DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; 41     } 42  43     return msg.wParam; 44 } 45 //===================================================================================== 46  47  48  49 //============================================================================================= 50 //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息, 51 //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。 52 //============================================================================================== 53 ATOM MyRegisterClass(HINSTANCE hInstance) 54 { 55     WNDCLASSEX wcex;            //申请一个窗口类别“WNDCLASSEX”和结构”wcex“ 56                                 //-------------------------------------------------------------- 57                                 //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc) 58                                 //为WNDPROC,类别名称为(lpszClassName)为”fe"; 59                                 //-------------------------------------------------------------- 60     wcex.cbSize = sizeof(WNDCLASSEX); 61  62     wcex.style            = CS_HREDRAW | CS_VREDRAW; 63     wcex.lpfnWndProc    = (WNDPROC)WndProc; 64     wcex.cbClsExtra        = 0; 65     wcex.cbWndExtra        = 0; 66     wcex.hInstance        = hInstance; 67     wcex.hIcon            = NULL; 68     wcex.hCursor        = NULL; 69     wcex.hCursor        = LoadCursor(NULL,IDC_ARROW); 70     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1); 71     wcex.lpszMenuName    = NULL; 72     wcex.lpszClassName    = "fe"; 73     wcex.hIconSm        = NULL; 74  75     return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串 76                                   //此字符串即为类别名称”fe"; 77 } 78 //============================================================================================ 79  80  81 //============================================================================================ 82 //按照前面所定义的窗口类别来建立并显示实际的程序窗口 83 //============================================================================================ 84 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 85 { 86    HWND hWnd; 87  88    hInst = hInstance; // 把instance handle 储存在全局变量中; 89  90    hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW, 91       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 92                       //----------------------------------------------- 93                       //调用CreateWindow函数来建立一个窗口对象 94                       //第一个参数就是窗口建立依据的类别名称 95                       //----------------------------------------------- 96    if (!hWnd) 97    { 98       return FALSE; 99    }100    //------------------------------------------------101    //设定窗口的位置及窗口的大小,然后绘制显示在设备上102    //-------------------------------------------------103    MoveWindow(hWnd,10,10,650,450,true);//位置及大小104    ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态105    UpdateWindow(hWnd);//将窗口绘制在显示设备上106 107    return TRUE;108 }109 //============================================================================================110 111 112 //============================================================================================113 //显示光标所在位置的坐标函数114 //============================================================================================115 void MyPaint(HDC hdc,LPARAM lParam)116 {117     int x,y;118     char str[20]="";//空字符储存要输在屏幕上的文字119 120     x = LOWORD(lParam); //取得鼠标x坐标值(低位字节的信息)121     y = HIWORD(lParam); //取得鼠标y坐标值(高位字节的信息)122 123     SetTextColor(hdc,RGB(255,200,0));//设定DC上文字颜色124 125     TextOut(hdc,10,10,"鼠标坐标",strlen("鼠标坐标"));//格式化所要显示的字符串,并调用126     sprintf(str,"X 坐标: %d   ",x);                 //TextOut()将其输出在窗口上;127     TextOut(hdc,30,30,str,strlen(str));//----||-BOOL TextOut(HDC hdc,int x,int y输出字符串的坐标,128     sprintf(str,"Y 坐标: %d   ",y);   //----||-------------LPCTSTR 字符串指针,int 字符串长度);129     TextOut(hdc,30,50,str,strlen(str));130 }131 //============================================================================================132 133 134 //============================================================================================135 //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型136 //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个137 //程序都会接收信息,选择性接受、处理;138 //============================================================================================139 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)140 {141     PAINTSTRUCT ps;142     HDC hdc;143 144     switch (message)                   //判断消息类型145     {146         case WM_PAINT:                //窗口重绘制147             hdc = BeginPaint(hWnd, &ps);148             EndPaint(hWnd, &ps);149             break;150         case WM_MOUSEMOVE:            //鼠标移动消息151             hdc = GetDC(hWnd);//BeginPaint()仅用在处理重绘消息上,在其他地方要获得DC要用GetDC();152             MyPaint( hdc,  lParam);//MyPaint(hdc,lParam);//lParam鼠标状态相关信息153             ReleaseDC(hWnd,hdc);154             break;155         case WM_DESTROY:              //处理窗口结束消息156             PostQuitMessage(0);157             break;158         default:159             return DefWindowProc(hWnd, message, wParam, lParam);160    }161    return 0;162 }163 //============================================================================================

 

转载地址:http://dctna.baihongyu.com/

你可能感兴趣的文章
10.15 iptables filter表案例, iptables nat表应用
查看>>
java B2B2C Springboot电子商城系统-路由网关(zuul)
查看>>
重磅课程|《CNCF x Alibaba 云原生技术公开课》正式开讲!
查看>>
java反射+注解实现Entity类与Dto类相互转换
查看>>
LVM讲解和磁盘故障小案例
查看>>
年后跳槽怕面试不过关?搞懂并发编程,轻松应对80%的面试场景
查看>>
Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway
查看>>
【更新】Infragistics Ultimate UI for WPF v18.2(二):分类图
查看>>
交易比特币的三种方式和购买数字资产的利弊
查看>>
干货 | 京东云部署Wordpress最佳实践
查看>>
nodejs 请求自动超时
查看>>
Spring Boot开发WEB页面
查看>>
Eclipse快捷键大全
查看>>
px和em和rem的区别
查看>>
OSChina 周六乱弹 —— “我们”快被你们玩坏了
查看>>
OSChina 周四乱弹 ——00后让别人给自己网购女朋友
查看>>
OSChina 周六乱弹 ——程序员的情怀:贫贱不能移
查看>>
螺旋矩阵
查看>>
SQLserver From simple To Full backup model
查看>>
一个不错的图片
查看>>