windows.h是Windows系统中的一个头文件,它包含了其他Windows头文件,这些头文件的某些也包含了其他头文件。这些头文件定义了Windows的所有资料型态、函数调用、资料结构和常数识别字,它们是Windows文件中的一个重要部分。C/C++ 程序在源文件前面写#include <windows.h>即可。
Windows系统C语言常用系统函数
FindWindow查找窗口句柄
SendMessage向窗口句柄发送指令
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | #include <windows.h>
 int main(){
 HWND window;
 
 
 
 
 window = FindWindow(NULL,"新建文本文档.txt - 记事本");
 SendMessage(window,WM_CLOSE,0,0);
 return 0;
 }
 
 | 
WindowFromPoint通过鼠标点击获得被点击窗口的句柄
GetCursorPos函数获取鼠标指针位置
模拟键盘向窗口发送字符:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | #include <windows.h>
 int main() {
 POINT mouse;
 HWND window;
 while (1) {
 GetCursorPos(&mouse);
 window = WindowFromPoint(mouse);
 
 SendMessage(window,WM_CHAR,'a',0);
 Sleep(100);
 }
 return 0;
 }
 
 | 
SetCursorPos函数设置鼠标指针位置
改变鼠标位置(运行后鼠标会飘,不失为一个小恶作剧程序):
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | #include <windows.h>
 int main(){
 int i;
 while(i < 100000){
 SetCursorPos(100,100);
 i += 1;
 }
 return 0;
 }
 
 | 
ShowWindow函数改变窗口状态,包括隐藏窗口
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | #include <windows.h>#include <stdio.h>
 #include <time.h>
 
 int main(){
 HWND window;
 window = FindWindow(NULL,"新建文本文档.txt - 记事本");
 ShowWindow(window,SW_HIDE);
 Sleep(5000);
 ShowWindow(window,SW_MAXIMIZE);
 Sleep(5000);
 ShowWindow(window,SW_MINIMIZE);
 Sleep(5000);
 ShowWindow(window,SW_RESTORE);
 Sleep(5000);
 return 0;
 }
 
 | 
GetClientRect函数获取窗口尺寸
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | #include <windows.h>#include <stdio.h>
 
 int main(){
 HWND windows;
 while(1){
 RECT rectangle;
 windows=FindWindow(NULL,"新建文本文档.txt - 记事本");
 GetClientRect(windows,&rectangle);
 printf("%d,%d,%d,%d\n",rectangle.left,rectangle.top,rectangle.right,rectangle.bottom);
 Sleep(1000);
 }
 }
 
 
 | 
EnumWindow函数枚举遍历可见窗口
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | #include <tchar.h>#include <stdio.h>
 #include <windows.h>
 
 typedef TCHAR tchar;
 
 
 BOOL CALLBACK EnumWinProc( HWND hWnd, LPARAM lParam ){
 tchar class_name[64] = { 0 };
 tchar window_text[128] = { 0 };
 
 GetClassName(hWnd, class_name, 64);
 GetWindowTextA(hWnd,window_text,128);
 
 
 if( IsWindowVisible(hWnd) && GetParent(hWnd) == NULL ){
 printf("hWnd: %u\tClassName: %s\tWindowText: %s\n", hWnd, class_name, window_text);
 }
 
 return TRUE;
 }
 
 int main(){
 EnumWindows(EnumWinProc, NULL);
 return 0;
 }
 
 
 | 
创建目录、拷贝文件、删除文件、删除目录
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | #include <windows.h>
 void main(){
 
 CreateDirectory("C:\\testdir",NULL);
 Sleep(3000);
 
 
 CopyFile("C:\\Windows\\System32\\cmd.exe","C:\\testdir\\c.exe",FALSE);
 Sleep(3000);
 
 
 DeleteFile("C:\\testdir\\c.exe");
 Sleep(3000);
 
 
 RemoveDirectory("C:\\testdir");
 }
 
 
 | 
C语言实现监听用户鼠标变动及窗口变化
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | #include <tchar.h>#include <stdio.h>
 #include <windows.h>
 
 typedef TCHAR tchar;
 
 int main() {
 POINT mouse;
 HWND window;
 HWND last_window;
 
 tchar class_name[64] = { 0 };
 tchar window_text[128] = { 0 };
 
 while (1) {
 GetCursorPos(&mouse);
 window = WindowFromPoint(mouse);
 
 
 GetClassName(window, class_name, 64);
 GetWindowTextA(window,window_text,128);
 
 if(window != last_window){
 last_window = window;
 printf("窗口句柄: %u\t窗口类: %s\t窗口标题: %s\n", window, class_name, window_text);
 }
 
 
 
 Sleep(100);
 }
 return 0;
 }
 
 | 
运行结果:
 
延伸阅读:Windows API实现截图
https://blog.csdn.net/greenapple_shan/article/details/39828313
参考资料
C语言windows.h库的常用函数(一)
C语言windows.h库的常用函数(二)
C语言windows.h库的常用函数(三)
C语言windows.h库的常用函数(四)
Win32 API Docs
Win32 API Docs / Winuser.h / EnumWindows function