C++获取屏幕分辨率(屏幕窗口大小),屏幕显示比例(DPI)几种方法

  1. 获取屏幕的分辨率的几种方法
    #include 
    
    #include 
    int main(void) {
    	HWND hd = GetDesktopWindow();
    	// 方法一
    	RECT rect;
    	// 只获得窗口客户区的大小
    	GetClientRect(hd, &rect);
    	int client_width = (rect.right - rect.left);
    	int client_height = (rect.bottom - rect.top);
    	std::cout << "client width:" << client_width << std::endl;
    	std::cout << "client height:" << client_height << std::endl;
    	// 获取到的是窗口在屏幕上的位置
    	GetWindowRect(hd, &rect);
    	int window_width = (rect.right - rect.left);
    	int window_height = (rect.bottom - rect.top);
    	std::cout << "window width:" << window_width << std::endl;
    	std::cout << "window height:" << window_height << std::endl;
    
    	// 方法二
    	// 不带菜单栏的大小
    	int no_menu_bar_width = GetSystemMetrics(SM_CXFULLSCREEN);
    	int no_menu_bar__height = GetSystemMetrics(SM_CYFULLSCREEN);
    	std::cout << "no menu bar width:" << no_menu_bar_width
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在C++中使用FFmpeg进行屏幕录制和推流获取屏幕分辨率,可以使用FFmpeg的C API。以下是一个简单的示例代码: ```c++ extern "C" { #include <libavutil/avutil.h> #include <libavutil/opt.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/error.h> } int main() { AVFormatContext *pFormatCtx; AVOutputFormat *pOutputFmt; AVStream *pStream; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame; AVPacket pkt; int ret; int width, height; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输出文件 avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, "output.mp4"); if (!pFormatCtx) { printf("Error allocating output context\n"); return -1; } // 添加视频流 pOutputFmt = pFormatCtx->oformat; pStream = avformat_new_stream(pFormatCtx, NULL); if (!pStream) { printf("Error creating new stream\n"); avformat_free_context(pFormatCtx); return -1; } // 设置编码器参数 pCodecCtx = pStream->codec; pCodecCtx->codec_id = pOutputFmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->width = 1920; pCodecCtx->height = 1080; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->time_base = (AVRational){1, 30}; pCodecCtx->bit_rate = 400000; // 查找编码器 pCodec = avcodec_find_encoder(pCodecCtx->codec_id); if (!pCodec) { printf("Codec not found\n"); avformat_free_context(pFormatCtx); return -1; } // 打开编码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Error opening codec\n"); avformat_free_context(pFormatCtx); return -1; } // 创建帧 pFrame = av_frame_alloc(); if (!pFrame) { printf("Error allocating frame\n"); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } pFrame->format = pCodecCtx->pix_fmt; pFrame->width = pCodecCtx->width; pFrame->height = pCodecCtx->height; ret = av_frame_get_buffer(pFrame, 32); if (ret < 0) { printf("Error allocating frame buffer\n"); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } // 打开屏幕录制设备 AVInputFormat *pInputFmt = av_find_input_format("gdigrab"); AVDictionary *pOptions = NULL; av_dict_set(&pOptions, "framerate", "30", 0); av_dict_set(&pOptions, "draw_mouse", "0", 0); av_dict_set(&pOptions, "offset_x", "0", 0); av_dict_set(&pOptions, "offset_y", "0", 0); av_dict_set(&pOptions, "video_size", "1920x1080", 0); ret = avformat_open_input(&pFormatCtx, "desktop", pInputFmt, &pOptions); if (ret < 0) { char errbuf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE); printf("Error opening input: %s\n", errbuf); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } // 读取屏幕分辨率 width = pCodecCtx->width; height = pCodecCtx->height; // 开始录制 avformat_write_header(pFormatCtx, NULL); while (1) { AVFrame *pFrameRaw = av_frame_alloc(); ret = av_read_frame(pFormatCtx, &pkt); if (ret < 0) { if (ret == AVERROR_EOF) { printf("End of file\n"); } else { printf("Error reading frame\n"); } break; } ret = avcodec_send_packet(pCodecCtx, &pkt); if (ret < 0) { printf("Error sending packet\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(pCodecCtx, pFrameRaw); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } if (ret < 0) { printf("Error receiving frame\n"); break; } sws_scale(sws_getContext(width, height, AV_PIX_FMT_BGRA, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, SWS_BILINEAR, NULL, NULL, NULL), (const uint8_t *const *)pFrameRaw->data, pFrameRaw->linesize, 0, height, pFrame->data, pFrame->linesize); pFrame->pts = av_gettime(); ret = avcodec_send_frame(pCodecCtx, pFrame); if (ret < 0) { printf("Error sending frame\n"); break; } } av_packet_unref(&pkt); } av_write_trailer(pFormatCtx); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return 0; } ``` 这个示例代码可以录制整个桌面,并将录制的视频输出为MP4格式的文件。其中,`avformat_open_input()`函数打开gdigrab设备,`avcodec_send_frame()`函数将录制的帧发送给编码器进行压缩,`av_write_frame()`函数将压缩后的帧写入输出文件。在代码中,我们也获取屏幕的分辨率信息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值