在嵌入式的路上,仅仅会一种C语言是远远不够的,所以便在学习stm32的同时,开始学习C++,为以后做准备。由于学习之初,为了能激发自己的兴趣,便模仿别人写了一个截屏小软件,当然可能是世上最垃圾的截图软件了,这句话是真的,,,虽然我也不想这么说
程序如下:
// ScreenShot@qigaohua.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "ScreenShot@qigaohua.h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
void ScreenCapture(); //获取整个桌面的图像
void CopyBitmap2Cipboard(); //复制鼠标选中的区域
HDC g_srcMemDc; //圆桌面截图
int screenW; // 屏幕的宽
int screenH; // 屏幕的高
RECT rect; // 矩形区域
bool isSelect = false ; //判断矩形区域是否已经选择
bool isDown = false ; //判断鼠标左键是否已经按下
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SCREENSHOTQIGAOHUA, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SCREENSHOTQIGAOHUA));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SCREENSHOTQIGAOHUA));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0; //MAKEINTRESOURCE(IDC_SCREENSHOTQIGAOHUA);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP , //********************************************************
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, SW_MAXIMIZE); //第二个参数表示窗口大小的样式********************88***********
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)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
//改变透明的画刷
LOGBRUSH brush;
brush.lbStyle = BS_NULL ;
HBRUSH hBrush = CreateBrushIndirect (&brush);
//改变笔的颜色
LOGPEN pen;
POINT pt;
pt.x = 2;
pt.y = 2;
pen.lopnColor = 0x0000ffff;
pen.lopnStyle = PS_SOLID ;
pen.lopnWidth = pt;
HPEN hPen = CreatePenIndirect (&pen);
switch (message)
{
//
case WM_CREATE :
ScreenCapture();
break ;
case WM_LBUTTONDOWN :
{
if(!isSelect)
{
POINT pt;
GetCursorPos (&pt);
rect.left = pt.x;
rect.top = pt.y;
rect.right = pt.x;
rect.bottom = pt.y;
isDown = true;
}
}
break;
case WM_LBUTTONUP :
{
if(isDown == true && !isSelect)
{
POINT pt;
GetCursorPos (&pt);
rect.right = pt.x;
rect.bottom = pt.y;
InvalidateRgn (hWnd ,0, true);
isDown = false ;
isSelect = true;
}
}
break;
case WM_MOUSEMOVE :
break ;
case WM_LBUTTONDBLCLK :
if(isSelect == true)
{
isSelect = false ;
CopyBitmap2Cipboard();
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
//将整个桌面放到全屏的窗口上面
SelectObject (hdc,hBrush);
SelectObject (hdc,hPen );
BitBlt (hdc,0,0,screenW,screenH,g_srcMemDc ,0,0,SRCCOPY);
Rectangle (hdc,rect.left ,rect.top ,rect.right ,rect.bottom);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
//获取整个桌面的图像
void ScreenCapture()
{
HDC disDC = ::CreateDC(L"DISPLAY", 0, 0, 0);
//得到整个桌面的大小(分辨率)
screenW = GetDeviceCaps (disDC ,HORZRES); //水平分辨率
screenH = GetDeviceCaps (disDC ,VERTRES); //垂直分辨率
//函数功能:该函数创建一个与指定设备兼容的内存设备上下文环境(DC)。
//函数原型:HDC CreateCompatibleDC(HDC hdc);
//参数:
//hdc:现有设备上下文环境的句柄,如果该句柄为NULL,该函数创建一个与应用程序的当前显示器兼容的内存设备上下文环境。
//返回值:如果成功,则返回内存设备上下文环境的句柄;如果失败,则返回值为NULL
g_srcMemDc = CreateCompatibleDC (disDC);
//然后用CreateCompatibleBitmap建立一个与屏幕显示兼容的位图,再用SelectObject将位图选入到内存显示设备中
HBITMAP hbitmap = CreateCompatibleBitmap (disDC,screenW,screenH);
SelectObject (g_srcMemDc ,hbitmap);
BitBlt (g_srcMemDc ,0,0,screenW,screenH,disDC ,0,0,SRCCOPY); //SRCAND
}
//复制鼠标选中的区域
void CopyBitmap2Cipboard()
{
int width = rect.right - rect.left ;
int height = rect.bottom - rect.top ;
HDC hSrcDc = ::CreateDC(L"DISPLAY",0,0,0);
HDC memDc = CreateCompatibleDC (hSrcDc);
HBITMAP map = CreateCompatibleBitmap (hSrcDc ,width,height);
HBITMAP oldmap =(HBITMAP) SelectObject (memDc ,map);
BitBlt (memDc ,0,0,width,height,hSrcDc ,rect.left ,rect.top,SRCCOPY );
HBITMAP newmap = (HBITMAP)SelectObject (memDc ,oldmap);
if(OpenClipboard(0))
{
EmptyClipboard ();
SetClipboardData (CF_BITMAP ,newmap);
CloseClipboard ();
}
}