#include <windows.h>
#include <strsafe.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void DrawRectangle(HWND hwnd); int cxClient, cyClient; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//声明全局数据:类名
static TCHAR szClassName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg; //注册窗口类
WNDCLASS wndclass; wndclass.hInstance = hInstance;
wndclass.lpszClassName = szClassName;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszMenuName = NULL;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.style = CS_HREDRAW; if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("this program must run in Windows NT!"), szClassName, MB_ICONERROR);
return ;
} hwnd = CreateWindow(
szClassName,
TEXT("MyFirstPractice"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
); ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd); // while (GetMessage(&msg, NULL, 0, 0)) 阻塞函数 这个NULL是指接收当前窗口,包括非客户区的信息,如果设为hwnd则(指的是当前客户区)处理非客户区时,不会响应去退出程序,所以会在后台存在 ???
// {
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// } // #define PM_NOREMOVE 0x0000
// #define PM_REMOVE 0x0001
// #define PM_NOYIELD 0x0002
// #if(WINVER >= 0x0500)
// #define PM_QS_INPUT (QS_INPUT << 16)
// #define PM_QS_POSTMESSAGE ((QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER) << 16)
// #define PM_QS_PAINT (QS_PAINT << 16)
// #define PM_QS_SENDMESSAGE (QS_SENDMESSAGE << 16) while (TRUE)
{
if (PeekMessage(&msg,NULL,,,PM_REMOVE))//PM_REMOVE接收后删除 PM_NOREMOVE获取后不删除(偷窥)
{
if (msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//rand() //0~MAX_RAND 0-32767
//绘制矩形,用到消息处理中的数据(全局)
DrawRectangle(hwnd);
}
} return msg.wParam;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
// static int cxClient, cyClient; //static是指数据在这个函数中为全局,不是在程序中 switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect); EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
} void DrawRectangle(HWND hwnd)
{
// TCHAR szBuffer[100];
// StringCchPrintf(szBuffer, 100, L"%d %d", cxClient, cyClient);
// MessageBox(NULL, szBuffer, L"Info", NULL);
HBRUSH hBrush;
HDC hdc;
RECT rect; if (cxClient == || cyClient == )
return;
hdc = GetDC(hwnd);
SetRect(&rect, rand() % cxClient, rand() % cyClient, rand() % cxClient, rand() % cyClient);
hBrush = CreateSolidBrush(RGB(rand() % , rand() % , rand() % ));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
ReleaseDC(hwnd, hdc);
}

学习windows编程 day4 之 绘制随机矩形和peekMessage的更多相关文章

  1. 学习windows编程 day4 之 矩形的操作

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  2. 学习windows编程 day4 之 多边矩形填充

    #include <windows.h> #include <math.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, ...

  3. 学习windows编程 day4 之 盯裆猫

    写着写着就困了.... 看这些测量数据就算了,是对各种函数的练习 #include <windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT ...

  4. 学习windows编程 day4 之视口和窗口

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  5. 学习windows编程 day4 之 设置画刷

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  6. 学习windows编程 day4 之 自定义映射

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  7. 学习windows编程 day4 之 映射模式

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  8. 有一定基础的 C++ 学习者该怎样学习 Windows 编程?

    人的心理有个奇异的特性:一项知识一旦学会之后,学习过程中面临的困惑和不解非常快就会忘得干干净净,似乎一切都是自然而然,本来就该这种.因此,关于「怎样入门」这类问题,找顶尖高手来回答,未必能比一个刚入门 ...

  9. 我为什么学习Windows编程

    前一段时间在看TCP/IP,在图书馆里面找了不少的书,其中有几本书还是不错的.比如: <Windows网络与通信程序设计(第二版)> 王艳平著 <WinSock网络编程经络> ...

随机推荐

  1. id生成工具类

    import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: ...

  2. 百度AI--自然语言处理之Java开发

    参数: public class APIConstants { //设置APPID/AK/SK public static final String APP_ID = "108***&quo ...

  3. curl用法一例 传递代理用户名密码

    curl -u bb0e1736d66744495b814b942fd04a80:0e11dda88048ed52cc8758caf06dc6b4 https://jinshuju.net/api/v ...

  4. Java singleton 一例

    org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of ...

  5. Windows 下类似于 grep 查找字符串的命令 [转帖]

    https://www.cnblogs.com/zxy1992/p/4372717.html findstr的命令参数及其意义如下所示 在文件中寻找字符串. FINDSTR [/B] [/E] [/L ...

  6. SQLPLUS SQLCMD连接管理oracle sqlserver的简单用法

    1. SQLPLUS 与plsql一样,其实不需要安装oracle客户端,只要是有sqlplus的即时客户端 以及将目录放置到path或者是相应的oralce_home变量中即可. 打开运行cmd s ...

  7. python中Switch/Case实现

    学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...

  8. javascript 中的后退和前进到上下一页

    jsp页面中要返回到上一页可以使用的方法有: 一不刷新: window.history.back();  //返回上一页,这里是利用的浏览器的缓存,返回后数据不会刷新;下一页就用:window.his ...

  9. Nginx referer防盗链模块

    L75 referer模块 ngx_http_referer_module 默认编译进nginx valid_referers 指令 Syntax: valid_referers none | blo ...

  10. GO系列教程

    1.介绍与安装 2.Hello World 3.变量 4. 类型 5.常量 6.函数(Function) 7.包 8.if-else 语句 9.循环 10.switch语句 11.数组和切片 12.可 ...