首先新建makefile
ALL:main
files += -mwindows -lcomctl32 -ldwmapi
main:$(cpp)
g++ $(cpp) $(files) -o $(exe) -static -Wall -std=c++11 $(INCLUDE) $(LIB)
.PHONY: clean all
clean:
del *.exe -rf
形成的编译命令是:g++ demo.cpp -mwindows -lcomctl32 -ldwmapi -o demo -static -Wall -std=c++11
异形窗口
#include <windows.h>
LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC = GetWindowDC(hWnd);
static HRGN hRgn = CreateRectRgn(120, 70, 280, 230);
switch (uMsg)
{
case WM_ERASEBKGND:
{
DefWindowProc(hWnd, uMsg, wParam, lParam);
FillRgn(hDC, hRgn, CreateSolidBrush(RGB(255, 165, 0))); // Orange
SelectObject(hDC, hRgn);
return 0;
}
case WM_CREATE:
{
HRGN hRgn1 = CreateEllipticRgn(0, 0, 400, 300);
HRGN hRgn2 = CreateEllipticRgn(150, 100, 250, 200);
CombineRgn(hRgn1, hRgn1, hRgn2, RGN_XOR);
SetWindowRgn(hWnd, hRgn1, TRUE);
DeleteObject(hRgn1);
DeleteObject(hRgn2);
break;
}
case WM_LBUTTONDOWN:
{
SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
break;
}
case WM_DESTROY:
{
DeleteObject(hRgn);
ReleaseDC(hWnd, hDC);
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, BOOL)
{
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("wndclass");
wc.hbrBackground = CreateSolidBrush(RGB(255, 99, 71));
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpfnWndProc = WinProc;
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST, wc.lpszClassName, TEXT("Window"), WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, 0, 0, hInstance, 0);
if (hWnd == NULL)
return 1;
SetLayeredWindowAttributes(hWnd, NULL, 178, LWA_ALPHA); // Tomato
MSG msg = {0};
while (GetMessage(&msg, 0, 0, 0))
{
DispatchMessage(&msg);
}
return 0;
}
单击更改分辨率
#include <Windows.h>
#include <strsafe.h>
/*****
* CALLBACK代表__stdcall 参数的传递顺序 从右到左 依次入栈 并且在函数返回前 依次出栈
* HWND UINT WPARAM LPARAM
* 消息所属的窗口句柄
* 消息名称 WM_XXX消息名
* 键盘附加消息
* 鼠标附加消息
*/
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
}
break;
case WM_PAINT:
{
}
break;
case WM_SIZE:
{
}
break;
case WM_COMMAND:
{
}
break;
case WM_LBUTTONDOWN:
{
DISPLAY_DEVICE m_Device;
DEVMODE m_DeviceMode;
m_Device.cb = sizeof(DISPLAY_DEVICE);
m_DeviceMode.dmSize = sizeof(DEVMODE);
m_DeviceMode.dmDriverExtra = 0;
EnumDisplayDevices(NULL, 0, &m_Device, 0);
if (EnumDisplaySettingsEx(m_Device.DeviceName, ENUM_CURRENT_SETTINGS, &m_DeviceMode, 0))
{
m_DeviceMode.dmPelsWidth = 1024;
m_DeviceMode.dmPelsHeight = 768;
if (ChangeDisplaySettingsEx(m_Device.DeviceName, &m_DeviceMode, NULL, 0, NULL) == DISP_CHANGE_SUCCESSFUL)
{
}
}
}
break;
case WM_KEYDOWN:
{
}
break;
case WM_CLOSE:
{
DestroyWindow(hWnd);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
{
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
// WINAPI代表__stdcall 参数的传递顺序 从右到左 依次入栈 并且在函数返回前 依次出栈
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
wc.cbClsExtra = 0; // 类的额外内存
wc.cbWndExtra = 0; // 窗口额外内存
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 背景
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // 光标 第一个为NULL代表使用系统提供的光标
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 图标 第一个为NULL代表使用系统提供的图标
wc.hInstance = hInstance; // 应用程序实例句柄
wc.lpfnWndProc = WindowProc; // 回调函数
wc.lpszClassName = TEXT("main"); // 窗口类名称
wc.lpszMenuName = NULL; // 菜单名称
wc.style = CS_HREDRAW | CS_VREDRAW; // 显示风格
RegisterClass(&wc);
/*****
* CreateWindow()
* lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam
* 类名
* 标题名
* WS_OVERLAPPEDWINDOW 混合风格
* x坐标 CW_USEDEFAULT
* y坐标
* 宽
* 高
* 父窗口 NULL
* 菜单 NULL
* 实例句柄 hInstance
* 附加值
*/
HWND hWnd = CreateWindow(wc.lpszClassName, TEXT("Windows"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (hWnd == NULL)
{
return 0;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
/*****
* HWND hwnd; 主窗口句柄
* UINT message; 具体消息名称
* WPARAM wParam; 附加消息 键盘消息
* LPARAM lParam; 附加消息 鼠标消息
* DWORD time; 消息产生时间
* POINT pt; 附加消息 鼠标消息 x y
*/
MSG msg;
/*****
* LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax
* 消息
* 捕获窗口 NULL代表捕获所有窗口
* 最小的过滤消息 0代表捕获所有消息
* 最大的过滤消息 0代表捕获所有消息
*/
while (GetMessage(&msg, NULL, 0, 0))
{
// 如果按的是组合键 需要翻译
TranslateMessage(&msg);
// 单按一个键盘就直接分发消息
DispatchMessage(&msg);
}
return 0;
}
标签:case,wc,windows,hWnd,代码,编程,break,NULL,WM
From: https://www.cnblogs.com/codebai/p/17288287.html