SDL窗口嵌入到MFC中

发布时间 2023-12-08 15:12:30作者: 阿风小子

第一步:新建MFC基于对话框的应用程序(此例工程命名为MFC_SDL),然后直接点击完成即可,如下图。

    第二步:删除“TODO:在此放置对话框控件”。添加Picture Control和Button到对话框中,修改Button的名字为显示图片。

 

  第三步:SDL相关头文件、lib库以及dll动态链接库的设置可以参考这篇文章:SDL2学习笔记1-环境搭建以及HelloSDL

  第四步:打开MFC_SDLDlg.cpp文件,在程序中添加头文件

  1. #include <SDL.h>  

  第五步:双击Button控件,转到鼠标点击响应程序。添加程序。程序如下:

  1. void CMFC_SDLDlg::OnBnClickedButton1()  
  2. {  
  3.     // TODO: 在此添加控件通知处理程序代码  
  4.     //The window we'll be rendering to  
  5.     SDL_Window* gWindow = NULL;  
  6.     //The surface contained by the window  
  7.     SDL_Surface* gScreenSurface = NULL;  
  8.   
  9.     //The image we will load and show on the screen  
  10.     SDL_Surface* gHelloWorld = NULL;  
  11.   
  12.     //首先初始化  
  13.     if(SDL_Init(SDL_INIT_VIDEO)<0)  
  14.     {  
  15.         printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );  
  16.         return ;  
  17.     }  
  18.     //创建窗口  
  19.     gWindow=SDL_CreateWindowFrom( (void *)( GetDlgItem(IDC_STATIC)->GetSafeHwnd() ) );  
  20.     if(gWindow==NULL)  
  21.     {  
  22.         printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );  
  23.         return ;  
  24.     }  
  25.     //Use this function to get the SDL surface associated with the window.  
  26.     //获取窗口对应的surface  
  27.     gScreenSurface=SDL_GetWindowSurface(gWindow);  
  28.   
  29.     //加载图片  
  30.     gHelloWorld = SDL_LoadBMP("Hello_World.bmp");//加载图片  
  31.     if( gHelloWorld == NULL )  
  32.     {  
  33.         printf( "Unable to load image %s! SDL Error: %s\n", "Hello_World.bmp", SDL_GetError() );  
  34.         return ;  
  35.     }  
  36.     //Use this function to perform a fast surface copy to a destination surface.    
  37.     //surface的快速复制  
  38.     //              SDL_Surface* src ,const SDL_Rect* srcrect , SDL_Surface* dst ,  SDL_Rect* dstrect  
  39.     SDL_BlitSurface( gHelloWorld ,     NULL ,                     gScreenSurface ,          NULL);  
  40.     SDL_UpdateWindowSurface(gWindow);//更新显示copy the window surface to the screen  
  41.   
  42. }  

  第六步:运行程序,点击“显示图片”按钮,现象如下:

 

  从程序中可以看出,要将SDL窗口嵌入到MFC中很简单,只要将SDL原来创建窗口的函数:

  1. SDL_Window* gWindow = SDL_CreateWindow("SHOW BMP",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);  

  改成下面的函数即可:

  1. SDL_Window* gWindow=SDL_CreateWindowFrom( (void *)( GetDlgItem(IDC_STATIC)->GetSafeHwnd() ) );  

其中,IDC_STATIC为PictureControl的ID

 

 

 

 

 

要在SDL中创建窗口并将其置于对话框上,可以按照以下的步骤进行操作。

  1. 首先需要引入SDL库文件,确保已经正确安装了SDL开发包。

 
#include <SDL2/SDL.h>
  1. 初始化SDL子系统,如图形、音频等。这里我们只关注图形部分,所以只需要调用SDL_Init(SDL_INIT_VIDEO)来初始化视频子系统。

 
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("无法初始化SDL: %s\n", SDL_GetError());
return -1;
}
  1. 设定窗口属性,比如大小、标题等。使用SDL_CreateWindow()函数来创建窗口。

 
 
const int WINDOW_WIDTH = 800; // 窗口宽度
const int WINDOW_HEIGHT = 600; // 窗口高度
 
// 创建窗口
SDL_Window* window = SDL_CreateWindow("My Window Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (!window){
printf("无法创建窗口: %s\n", SDL_GetError());
return -1;
}
  1. 获取窗口的指针,然后通过该指针将窗口置于对话框上。

HWND hwnd = FindWindowA(NULL, "My Window Title"); // 根据窗口标题查找窗口句柄
SetParent((HWND)SDL_GetWindowID(window), hwnd); // 将窗口置于对话框上

完成以上步骤后,就能够在SDL中创建窗口并将其置于对话框上了。