// Spy.cpp : Defines the entry point for the application.
//定义所需要的ID号;功能实现了与Spy++查找窗体的类似功能。目的是学习窗体类API
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//以下是用到的句柄的定义
HWND hHandle;
HWND hWindowName;
HWND hClassName;
HWND hStyle;
HWND hRect;
HWND hBtnSend;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SPY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SPY);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SPY);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_SPY;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
20,20,400,220, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
short int xPos;
short int yPos;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
static setcapture=0;
HWND hNotepad;
HWND hEdit;
switch (message)
{
case WM_CREATE:
//创建控件
CreateWindow("STATIC","句 柄:",WS_VISIBLE|WS_CHILD|WS_BORDER,10,0,80,20,
hWnd,(HMENU)IDC_HANDLE,hInst,NULL);
hHandle=CreateWindow("EDIT",NULL,WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,100,0,200,20,
hWnd,(HMENU)ID_HANDLE,hInst,NULL);
CreateWindow("STATIC","窗口名称:",WS_VISIBLE|WS_CHILD|WS_BORDER,10,30,80,20,
hWnd,(HMENU)IDC_WINDOWNAME,hInst,NULL);
hWindowName= CreateWindow("STATIC",NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,100,30,200,20,
hWnd,(HMENU)ID_WINDOWNAME,hInst,NULL);
CreateWindow("STATIC","类 名 称:",WS_VISIBLE|WS_CHILD|WS_BORDER,10,60,80,20,
hWnd,(HMENU)IDC_CLASSNAME,hInst,NULL);
hClassName=CreateWindow("STATIC",NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,100,60,200,20,
hWnd,(HMENU)ID_CLASSNAME,hInst,NULL);
CreateWindow("STATIC","样式名称:",WS_VISIBLE|WS_CHILD|WS_BORDER,10,90,80,20,
hWnd,(HMENU)IDC_STYLE,hInst,NULL);
hStyle=CreateWindow("STATIC",NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,100,90,200,20,
hWnd,(HMENU)ID_STYLE,hInst,NULL);
CreateWindow("STATIC","外形尺寸:",WS_VISIBLE|WS_CHILD|WS_BORDER,10,120,80,20,
hWnd,(HMENU)IDC_RECT,hInst,NULL);
hRect=CreateWindow("STATIC",NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,100,120,200,20,
hWnd,(HMENU)ID_RECT,hInst,NULL);
hBtnSend=CreateWindow("BUTTON","发送",WS_VISIBLE|WS_CHILD|WS_BORDER|BS_PUSHBUTTON,130,150,70,20,
hWnd,(HMENU)IDC_BTNSEND,hInst,NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDC_BTNSEND:
//发送信息到记事本
char szBuffer[256];
char temp[64];
GetWindowText(GetDlgItem(hWnd,IDC_HANDLE),temp,64);
wsprintf(szBuffer,"%s",temp);
GetWindowText(hHandle,temp,64);
lstrcat(szBuffer,temp);
// wsprintf(szBuffer,"%s/n",szBuffer);//这里有待于提高啊
GetWindowText(GetDlgItem(hWnd,IDC_WINDOWNAME),temp,64);
lstrcat(szBuffer,temp);
GetWindowText(hWindowName,temp,64);
lstrcat(szBuffer,temp);
// wsprintf(szBuffer,"%s/n",szBuffer,13);
GetWindowText(GetDlgItem(hWnd,IDC_CLASSNAME),temp,64);
lstrcat(szBuffer,temp);
GetWindowText(hClassName,temp,64);
lstrcat(szBuffer,temp);
//wsprintf(szBuffer,"%s/n",szBuffer,13);
GetWindowText(GetDlgItem(hWnd,IDC_STYLE),temp,64);
lstrcat(szBuffer,temp);
GetWindowText(hStyle,temp,64);
lstrcat(szBuffer,temp);
wsprintf(szBuffer,"%s/n",szBuffer,13);
GetWindowText(GetDlgItem(hWnd,IDC_RECT),temp,64);
lstrcat(szBuffer,temp);
GetWindowText(hRect,temp,64);
lstrcat(szBuffer,temp);
// wsprintf(szBuffer,"%s/n",szBuffer,13);
//打开记事本进程
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!
CreateProcess("C://WINNT//notepad.exe",NULL,0,0,FALSE
,0,NULL,NULL,&si,&pi))
{
MessageBox(hWnd,szBuffer,NULL,MB_OK);
return 0;
}
Sleep(100);
//找到记事本的句柄
hNotepad=FindWindow("Notepad",NULL);
if(!hNotepad)
{
return 0;
}
//找到记事本的第一个EDIT控件的句柄
hEdit=FindWindowEx(hNotepad,NULL,"EDIT",NULL);
//向其发送消息
SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)szBuffer);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
SetCapture(hWnd); //设置扑获鼠标信息
SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
break;
case WM_LBUTTONUP:
HWND hTemp;
POINT pt;
xPos=LOWORD(lParam);
yPos=HIWORD(lParam);
pt.x=xPos;
pt.y=yPos;
ClientToScreen(hWnd,&pt); //将客户区坐标转化为屏幕坐标
hTemp=WindowFromPoint(pt);//找屏幕区中点处的窗体句柄
if(hTemp)
{
char Buffer[64];
long style;
RECT rect;
wsprintf(Buffer,"0X%x",hTemp);
SetWindowText(hHandle,Buffer);
GetWindowText(hTemp,Buffer,64);
SetWindowText(hWindowName,Buffer);
GetClassName(hTemp,Buffer,64);
SetWindowText(hClassName,Buffer);
style=GetWindowLong(hTemp,GWL_STYLE);
wsprintf(Buffer,"0X%x",style);
SetWindowText(hStyle,Buffer);
GetWindowRect(hTemp,&rect);
wsprintf(Buffer,"左上角(%d,%d),右下角(%d,%d)",rect.left,rect.top,rect.bottom,rect.right);
SetWindowText(hRect,Buffer);
}
ReleaseCapture(); //释放扑获鼠标
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
// RECT rt;
// GetClientRect(hWnd, &rt);
// DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}