DX窗口

首先注册窗口

 

声明一个结构体对象: WNDCLASSEX  wndclass;

接着填充各成员变量

 wndclass.cbSize               = sizeof( WNDCLASSEX); //结构大小
 wndclass.style                  = CS_CLASSDC; //窗口格式
 wndclass.cbClsExtra         = 0;
 wndclass.cbWndExtra        = 0;//这两个一般为0
 wndclass.hbrBackground   = NULL;//背景
 wndclass.hCursor              = NULL;//鼠标
 wndclass.hIcon                 = NULL;//窗口小图标,如最左上角小图标"无穷大"
 wndclass.hIconSm             = NULL;//资源管理器小图标,如文件夹图标
 wndclass.hInstance           = hInstance; //应用程序的句柄
 wndclass.lpfnWndProc       = WndProc; //回调函数
 wndclass.lpszClassName   = "new_windows";//项目名称
 wndclass.lpszMenuName   = NULL;//主菜单名称

然后调用注册窗口函数 RegisterClassEx( &wndclass);

 

第二  初始化窗口

先创建窗口 

CreateWindow("new_windows", "标题", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL,
  NULL, hInstance, NULL);
 //第一个:项目名称,标题,扩充属性(有无图标关闭),开始坐标x, y,高度,宽度,

接着显示和刷新窗口

 

第三 完成回调函数

 

第四 消息循环处理

MSG msg;
while( msg.message != WM_QUIT)
 {
  if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE)) //PM_REMOVE将读到的消息移除
  {

 
   TranslateMessage(&msg); //转换字符消息
   DispatchMessage(&msg); //把转换的消息发给系统
  }
  else
  {
   //画图
  }
 }

 

第五  释放资源

 

UnregisterClass( 项目名称, wndclass)

 

 

 

 

MessageBox  用法

 

MessageBox( 窗口句柄 hwnd, "文本内容", "消息框的标题", 消息框类型)

 

消息框类型 记住一个,然后点右键可以查相应其他的 MB_OK

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用SharpDX库来后台截取DX窗口。下面是一个示例代码: ``` using System; using System.Drawing; using System.Drawing.Imaging; using SharpDX; using SharpDX.Direct3D9; using SharpDX.Windows; namespace DXScreenCaptureTest { class Program { static void Main(string[] args) { var processName = "myDXProcess"; // 要截取的DX程序进程名 var adapterIndex = 0; // 显卡索引,如果只有一个显卡则为0 // 获取DX程序窗口句柄 IntPtr hWnd = IntPtr.Zero; while (true) { hWnd = User32.FindWindowEx(IntPtr.Zero, hWnd, null, processName); if (hWnd == IntPtr.Zero) break; if (User32.IsWindowVisible(hWnd)) { int pid; User32.GetWindowThreadProcessId(hWnd, out pid); if (System.Diagnostics.Process.GetProcessById(pid).ProcessName == processName) break; } } if (hWnd == IntPtr.Zero) { Console.WriteLine($"未找到进程名为{processName}的DX程序窗口"); return; } // 获取DX设备对象 var config = new PresentParameters() { BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = hWnd, PresentationInterval = PresentInterval.Immediate, SwapEffect = SwapEffect.Discard }; var device = new Device(new Direct3D(), adapterIndex, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, config); // 获取后台缓存表面 var surface = Surface.CreateOffscreenPlain(device, config.BackBufferWidth, config.BackBufferHeight, Format.A8R8G8B8, Pool.SystemMemory); // 截取屏幕并保存到文件 var bitmap = CaptureSurface(device, surface); bitmap.Save("screenshot.png", ImageFormat.Png); device.Dispose(); surface.Dispose(); bitmap.Dispose(); Console.WriteLine("截图完成"); } private static Bitmap CaptureSurface(Device device, Surface surface) { // 将后台缓存表面复制到系统内存表面 device.GetFrontBufferData(0, surface); // 将表面转换为Bitmap var dataRectangle = surface.LockRectangle(LockFlags.ReadOnly); var bitmap = new Bitmap(dataRectangle.Width, dataRectangle.Height, PixelFormat.Format32bppArgb); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Utilities.CopyMemory(bitmapData.Scan0, dataRectangle.DataPointer, dataRectangle.Pitch * dataRectangle.Height); surface.UnlockRectangle(); bitmap.UnlockBits(bitmapData); return bitmap; } } static class User32 { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); } } ``` 代码中使用了SharpDX库,需要使用NuGet进行安装。另外,要注意确保程序有足够的权限进行后台截图操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值