// OriginWnd.cpp : 定义应用程序的入口点。
//
#include "framework.h"
#include "OriginWnd.h"
#include <comdef.h>
#include <gdiplus.h>
#include <iostream>
#include <vector>
#pragma comment(lib, "gdiplus.lib")
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_ORIGINWND, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow)) {
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ORIGINWND));
MSG msg;
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Gdiplus::GdiplusShutdown(gdiplusToken); // 关闭 GDI+
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ORIGINWND));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;// MAKEINTRESOURCEW(IDC_ORIGINWND);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目标: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,// 无边框 + 置顶WS_POPUP | WS_EX_TOPMOST,
0, 0, 100, 100, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
//置顶
/*
// 获取当前窗口的扩展样式
LONG exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
// 设置WS_EX_TOPMOST来使窗口置顶
// 如果需要取消置顶,可以使用WS_EX_NOTOPMOST或移除WS_EX_TOPMOST
LONG newExStyle = exStyle | WS_EX_TOPMOST;
// 设置新的窗口扩展样式
SetWindowLong(hWnd, GWL_EXSTYLE, newExStyle);
//或者:
SetWindowPos(hWnd, HWND_TOPMOST, x, 0, info.width, info.height, SWP_NOMOVE | SWP_NOSIZE);
*/
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目标: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
using namespace Gdiplus;
static HFONT hFont = NULL;
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
Image* backgroundImage = new Image(L"bkg.png");
// 绘制背景图像
if (backgroundImage) {
Graphics graphics(hdc);
graphics.DrawImage(backgroundImage, 0, 0, 100, 100);
}
if (true) {
// 设置字体
SelectObject(hdc, hFont);
// 设置文本颜色和背景颜色
SetTextColor(hdc, RGB(0, 0, 0)); // 黑色文字
SetBkMode(hdc, TRANSPARENT); // 透明背景
// 绘制文本
const wchar_t* text = L"Hello, Windows!";
size_t textlen = wcslen(text);
// 计算文字的宽度和高度
SIZE textSize;
GetTextExtentPoint32(hdc, text, textlen, &textSize);
// 获取窗口客户区的大小
RECT rect;
GetClientRect(hwnd, &rect);
// 计算居中位置
int x = (rect.right - textSize.cx) / 2;
int y = (rect.bottom - textSize.cy) / 2;
// 绘制文本
TextOut(hdc, x, y, text, textlen );
}
EndPaint(hWnd, &ps);
}
break;
case WM_CREATE: {
// 创建一个字体
hFont = CreateFont(
30, // 字体高度
0, // 字体宽度(0 表示由系统自动决定)
0, // 旋转角度
0, // 字符倾斜角度
FW_NORMAL, // 字体粗细
FALSE, // 斜体
FALSE, // 下划线
FALSE, // 删除线
ANSI_CHARSET, // 字符集
OUT_DEFAULT_PRECIS, // 输出精度
CLIP_DEFAULT_PRECIS, // 剪裁精度
DEFAULT_QUALITY, // 输出质量
0, // 字体家庭
L"Arial" // 字体名称
);
return 0;
}
case WM_DESTROY: {
if (hFont) {
DeleteObject(hFont);
hFont = NULL; // 避免悬空指针
}
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}