创建子窗口类,使得子窗口有自己的处理过程。

子窗口类型WS_CHILD不能和WS_POPUP一起使用!
为什么子窗口要有自己的处理过程?
如果使用主窗口类来创建子窗口,那么子窗口和主窗口将公用窗口处理过程,此时,窗口处理过程在进行消息处理的时候,必须判断是哪个窗口的消息,非常不方便。
子窗口必须有自己的窗口类型WSCHILD,并且子窗口的父窗口句柄一定不能使NULL,子窗口的坐标主窗口的映射方式和客户区有关。否则CreateWindow函数就会失败。

客户定制消息:WL_USER+N,其中N可以是0x7FFF-WM_USER之间的任何值。发送窗口消息的函数:

SendMessage
LRESULT WINAPI SendMessage(
_In_ HWND hWnd,
_In_ UINT Msg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。和函数PostMessage不同,PostMessage是将一个消息寄送到一个线程的消息队列后就立即返回。
hWnd:  发送消息的目标窗口;
Msg:   WMXXX消息;
wParam: 用户自定义的一个32位值;
1param: 用户自定义的一个32为值;
设置当前的背景色
COLORREF SetBkColor(
HDC hdc,
COLORREF color
);

用指定的颜色值来设置当前的背景色,如果指定的颜色值超出了当前设备的表示范围,则设置为最近似的、设备可以表示的颜色。

hdc:设置上下文句柄
color:标识新的背景颜色值。如果想要获得COLORREF的值,请使用RGB宏。
改变窗口位置
BOOL WINAPI SetWindowPos(
_In_ HWND hWnd,
_In_opt_ HWND hWndInsertAfter,
_In_ int X,
_In_ int Y,
_In_ int cx,
_In_ int cy,
_In_ UINT uFlags
);
改变一个子窗口,弹出式窗口或顶层窗口的尺寸,位置和Z序。
hwnd:在z序中的位于被置位的窗口前的窗口句柄。该参数必须为一个窗口句柄
hWndlnsertAfter:用于标识在z-顺序的此 CWnd 对象之前的 CWnd 对象。如果uFlags参数中设置了SWP_NOZORDER标记则本参数将被忽略。可为下列值之一:
    • HWND_BOTTOM:值为1,将窗口置于Z序的底部。如果参数hWnd标识了一个顶层窗口,则窗口失去顶级位置,并且被置在其他窗口的底部。
    • HWND_NOTOPMOST:值为-2,将窗口置于所有非顶层窗口之上(即在所有顶层窗口之后)。如果窗口已经是非顶层窗口则该标志不起作用。
    • HWND_TOP:值为0,将窗口置于Z序的顶部。
    • HWND_TOPMOST:值为-1,将窗口置于所有非顶层窗口之上。即使窗口未被激活窗口也将保持顶级位置。

X:以客户坐标指定窗口新位置的左边界。

Y:以客户坐标指定窗口新位置的顶边界。

cx:以像素指定窗口的新的宽度。

cy:以像素指定窗口的新的高度。

uFlags:窗口尺寸和定位的标志。

改变指定窗口的属性

LONG WINAPI SetWindowLong(
_In_ HWND hWnd,
_In_ int nIndex,
_In_ LONG dwNewLong
);
hwnd:    窗口句柄;
nIndex:   要改变窗口的那个参数;
dwNewLong: 窗口参数的新值;

 获取窗口类信息

BOOL WINAPI GetClassInfo(
_In_opt_ HINSTANCE hInstance,
_In_ LPCTSTR lpClassName,
_Out_ LPWNDCLASS lpWndClass
);

hInstance: 当前程序的基地址,也就是程序的句柄;
1pclassName: 类名;
1pwndclass:    指向 WNDCLASS类型的指针;

画矩形

BOOL Rectangle(
HDC hdc,
int left,
int top,
int right,
int bottom
);

画一个矩形,可以用当前的画笔画矩形轮廓,用当前画刷进行填充。

hdc: 设备环境句柄。
left: 指定矩形左上角的逻辑X坐标。
top: 指定矩形左上角的逻辑Y坐标。
right:   指定矩形右下角的逻辑X坐标。
bottom: 指定矩形右下角的逻辑Y坐标。
返回值:如果函数调用成功,返回值非零,否则返回值为0

获得当前程序基地址,或者HINSTANCE的函数:GetModuleHandle(NULL);

系统内置的窗口类型“BUTTON”
WL_COMMAND消息:
wParam:
lparam:

不使用子窗口类创建一个按钮,点击按钮按钮会变色,源码

 //#define _AFXDLL
//#include<afx.h>
#include<Windows.h>
#include<tchar.h> #ifndef _AFXDELL
#define TRACE(a,b)
#endif LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush; hBrush = CreateSolidBrush(RGB(,,));
WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; RegisterClass(&WndClass);
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return ;
}
LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT pt;
HBRUSH hBrush;
static int status;
switch (message)
{
case WM_CREATE:
return ;
case WM_SIZE:
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pt);
if(status == )
{
hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , , );
SetBkColor(hdc, RGB(, , ));
TextOut(hdc, , , TEXT("OK"), );
TextOut(hdc, , , TEXT("Hit me!"), );
}
else
{
hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , , );
SetBkColor(hdc, RGB(, , ));
TextOut(hdc, , , TEXT("OK"), );
TextOut(hdc, , , TEXT("Hit me!"), );
}
EndPaint(hwnd, &pt);
return ;
case WM_LBUTTONDOWN:
if (LOWORD(lParam)> && LOWORD(lParam)< && HIWORD(lParam)< && HIWORD(lParam)>)
{
status = ;
InvalidateRect(hwnd, NULL,TRUE);
}
return ;
case WM_LBUTTONUP:
InvalidateRect(hwnd, NULL,TRUE);
status = ;
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
}

使用子窗口创建按钮

 .h
//#pragma once
//#define _AFXDLL
//#include<afx.h>
#include<Windows.h>
#include<tchar.h> #ifndef _AFXDELL
#define TRACE(a,b)
#endif HWND CreateChild(HWND ParentHwnd, int x, int y, int cx, int cy);
extern HINSTANCE G_hInst;
 .c
#include"1.h"
HINSTANCE G_hInst;
LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush; hBrush = CreateSolidBrush(RGB(,,));
G_hInst = hInst;
WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; RegisterClass(&WndClass);
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return ;
}
LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT pt;
HBRUSH hBrush;
static int status;
static HWND child1,child2,child3;
TCHAR buf[];
switch (message)
{
case WM_CREATE:
child1 = CreateChild(hwnd, , , , );
child2 = CreateChild(hwnd, , , , );
child3 = CreateChild(hwnd, , , , );
return ;
case WM_SIZE:
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pt);
EndPaint(hwnd, &pt);
return ;
case WM_USER+:
hdc = GetDC(hwnd);
if((LPARAM)child1==lParam)
{
TextOut(hdc,,,TEXT("Hit First button"),);
}
if((LPARAM)child2==lParam)
{
TextOut(hdc,,,TEXT("Hit Second button"),);
}
if((LPARAM)child3==lParam)
{
TextOut(hdc,,,TEXT("Hit Third button"),);
}
ReleaseDC(hwnd, hdc);
ValidateRect(hwnd, NULL);
return ;
case WM_USER + :
InvalidateRect(hwnd, NULL, TRUE);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
}
 .c
#include"1.h"
extern HINSTANCE G_hInst;
static LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); HWND CreateChild(HWND ParentHwnd, int x, int y, int cx, int cy)
{
WNDCLASS WndClass,MyWnd;
TCHAR* ClassName = TEXT("MyChild");
HWND hwnd;
MSG msg;
HBRUSH hBrush; hBrush = CreateSolidBrush(RGB(, , ));
WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = G_hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!GetClassInfo(G_hInst,ClassName,&MyWnd))
{
RegisterClass(&WndClass);
} hwnd = CreateWindow(ClassName, TEXT("OK"), WS_CHILD, x, y, cx, cy, ParentHwnd, NULL, G_hInst, NULL);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd); //GetMessage第二个参数为NULL时,会获取所有消息。因为父窗口已经获取消息了,所以没必要再获取了
/*while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}*/
return hwnd;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT pt;
HBRUSH hBrush;
static int status;
static int cx,cy; switch (message)
{
case WM_CREATE:
return ;
case WM_SIZE:
cx=LOWORD(lParam);
cy=HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pt);
//TRACE(TEXT("status=%d\n"),status);
if (status == )
{
hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx,cy);
SetBkColor(hdc, RGB(, , ));
TextOut(hdc, ,, TEXT("OK"), );
}
else
{
hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx,cy);
SetBkColor(hdc, RGB(, , ));
TextOut(hdc, ,, TEXT("OK"), );
}
EndPaint(hwnd, &pt);
return ;
case WM_LBUTTONDOWN:
{
status = ;
InvalidateRect(hwnd, NULL, TRUE);
SendMessage(hwnd,WM_PAINT,NULL,NULL);
SendMessage(GetParent(hwnd), WM_USER + , NULL,(LPARAM)hwnd); }
return ;
case WM_LBUTTONUP:
status = ;
InvalidateRect(hwnd, NULL, TRUE);
SendMessage(GetParent(hwnd), WM_USER + , NULL,NULL);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
}

还可以使用WIndows自身的button控件

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg; WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + );
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static HWND button;
switch (message)
{
case WM_CREATE:
button = CreateWindow(TEXT("button"), TEXT("OK"), WS_CHILD | WS_VISIBLE, , , , , hwnd, NULL, GetModuleHandle(NULL), NULL);
//禁用按钮,按钮变灰色
//Button_Enable(button, FALSE);
Button_SetText(button,TEXT("测试"));
return ;
case WM_COMMAND:
hdc = GetDC(hwnd);
if (button == (HWND)lParam)
{
TextOut(hdc, , , TEXT("Hit Button"), );
}
if (BN_CLICKED == HIWORD(wParam)) //测试控件通知码
{
TextOut(hdc, , , TEXT("BN_CLICKED Hit Button"), );
}
ReleaseDC(hwnd, hdc);
ValidateRect(hwnd, NULL);
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
}

HIWORD(wParam)控件通知码

LOWORD(wParam)控件标识

lParam控件的窗口句柄

12 Windows编程——子窗口和系统内置窗口类“BUTTON”的更多相关文章

  1. windows 服务管理器使用系统内置帐户时密码的输入

    windows 服务管理器使用系统内置帐户时在选择帐户如network services后不需要输入密码,直接确认即可,系统会自动附加密码.

  2. windows 编程 —— 子窗口 与 子窗口控件

    目录: 子窗口与主窗口的交互 子窗口控件 按钮类别 button 滚动条类别 scrollbar 静态类别  static 编辑框类别 edit 清单方块 listbox 子窗口与主窗口的交互 创建窗 ...

  3. windows 编程 —— 子窗口类别化(Window Subclassing)

    对于子窗口控件,有时我们可能会想要获取子窗口的某些消息,比如在一个主窗口下有三个按钮,如果想要实现使用键盘Tab或者Shift-Tab键来使焦点切换于不同按钮之间,这时就可以使用子窗口类别化(Wind ...

  4. 【Windows编程】入门篇——win 32窗口的hello word!

    ✍  Windows编程基础 1.Win 32应用程序基本类型 1)  控制台程序 不需要完善的windows窗口,可以使用DOS窗口方式显示 2)  Win 32窗口程序 包含窗口的程序,可以通过窗 ...

  5. 16 Windows编程——系统内置窗口子类型之edit、ComboBox、ownerbutton、listbox

    edit类型的子窗口 ES_MULTILINE:多行输入文本框 窗口的消息: WL_COMMAND: EN_CHANGE:当edit窗口内的文本内容改变的时候,edit子窗口给父窗口发送一个WL_CO ...

  6. 13 Windows编程——系统内置窗口子类型之静态子窗口

    静态子窗口类型 wndclass:static 源码 #include<Windows.h> #include<Windowsx.h> HINSTANCE G_h; LRESU ...

  7. 15 Windows编程——系统内置窗口子类型之button

    button子类型BS_3STATE.BS_AUTO3STATE.BS_AUTOCHECKBOX 源码 #include<Windows.h> #include<Windowsx.h ...

  8. [Windows编程] 使用AttachThreadInput 来捕捉其它窗口的键盘输入

    在一些情况下(比如屏幕软键盘或者输入法程序),自己的窗口没有输入焦点但是想要当前焦点窗口的键盘输入消息,可以使用Win32 API函数AttachThreadInput()来解决这个问题.Attach ...

  9. 走进windows编程的世界-----对话框、文本框、button

    1 对话框的分类  2 对话框的基本使用方式  3 对话框资源  4 有模式对话框的使用 int DialogBox( HINSTANCE hInstance, LPCTSTR lpTemplate, ...

随机推荐

  1. 【Leetcode_easy】744. Find Smallest Letter Greater Than Target

    problem 744. Find Smallest Letter Greater Than Target 题意:一堆有序的字母,然后又给了一个target字母,让求字母数组中第一个大于target的 ...

  2. Maven下载依赖包所使用的方法或者说三方包

    wagon-http-3.2.0-shaded.jar 下载主要用的是这个包,mac位于路径/usr/local/Cellar/maven/3.6.0/libexec/lib下 如图,即使修改jar包 ...

  3. Efcore迁移

    Efcore迁移 Add-Migration XX:1.根据模型的实际结构对比当前快照,从而生成新迁移文件的Up和Down方法2.根据模型的实际结构修改快照和新迁移文件---------------- ...

  4. Egret入门学习日记 --- 第十六篇(书中 6.10~7.3节 内容)

    第十六篇(书中 6.10~7.3节 内容) 昨天搞定了6.9节,今天就从6.10节开始. 其实这个蛮简单的. 这是程序员模式. 这是设计师模式. 至此,6.10节 完毕. 开始 6.11节. 有点没营 ...

  5. Introduction - What is machine learning

    摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第一章<绪论:初识机器学习>中第2课时<什么是机器学习?>的视频原文字幕.为本人在视频学习过程中逐 ...

  6. Netty学习笔记(一)——nio基础

    Netty简单认识: 1) Netty 是由JBOSS 提供的一个Java 开源框架. 2) Netty 是一个异步的.基于事件驱动的网络应用框架,用以快速开发高性能.高可靠性的网络I0 程序. 3) ...

  7. mac 已损坏 移至废纸篓

    1.问题描述: 从网页下载的安装包,总是提示“已损坏,移至废纸篓”这类的信息 2.原因: 系统版本过高,对安全性进行了校验. 3.解决方案:命令行输入以下命令,然后输入密码 sudo spctl -- ...

  8. Redis 常用命令学习三:哈希类型命令

    1.赋值与取值命令 127.0.0.1:6379> hset stu name qiao (integer) 1 127.0.0.1:6379> hset stu sex man (int ...

  9. 20191011-构建我们公司自己的自动化接口测试框架-Util的读取excel常用方法模块

    包括获取excel的sheet名字,设定excel的sheet,读excel,写excel等常规操作. from openpyxl import Workbook from openpyxl impo ...

  10. PHP的 parse_ini_file 解析配置文件

    解析配置文件: parse_ini_file 类似解析php.ini文件样 配置文件内容如下: Example #1 sample.ini 的内容 ; This is a sample configu ...