[游戏模版4] Win32 显示鼠标位置

 

>_<: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                        104
10 #define IDM_EXIT                        105
11 #define IDS_HELLO                        106
12 #define IDI_FE                107
13 #define IDI_SMALL                        108
14 #define IDC_FE                109
15 #define IDC_MYICON                        2
16 #define IDC_STATIC                        -1
17 // Next default values for new objects
18 //
19 #ifdef APSTUDIO_INVOKED
20 #ifndef APSTUDIO_READONLY_SYMBOLS
21 
22 #define _APS_NEXT_RESOURCE_VALUE        129
23 #define _APS_NEXT_COMMAND_VALUE         32771
24 #define _APS_NEXT_CONTROL_VALUE         1000
25 #define _APS_NEXT_SYMED_VALUE           110
26 #endif
27 #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 once
10 #endif // _MSC_VER > 1000
11 
12 #define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers
13 
14 
15 // Windows Header Files:
16 #include <windows.h>
17 
18 // C RunTime Header Files
19 #include <stdlib.h>
20 #include <malloc.h>
21 #include <memory.h>
22 #include <tchar.h>
23 
24 // Local Header Files
25 
26 // TODO: reference additional headers your program requires here
27 
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 //============================================================================================

 

posted @ 2014-05-16 17:07  beautifulzzzz  阅读(1342)  评论(0编辑  收藏  举报