Windows copy bitmap

127 篇文章 0 订阅
108 篇文章 1 订阅
HBITMAP CopyBitmap( HBITMAP hbm) {
    HDC hdcSrc = CreateCompatibleDC(NULL);
    HDC hdcDst = CreateCompatibleDC(NULL);
    HBITMAP hbmOld, hbmOld2, hbmNew;
    BITMAP bm;
    GetObject(hbm, sizeof(bm), &bm);
    hbmOld = SelectObject(hdcSrc, hbm);
    hbmNew = CreateBitmap( bm.bmWidth, bm.bmHeight, bm.bmPlanes,
bm.bmBitsPixel,
            NULL);
    hbmOld2 = SelectObject(hdcDst, hbmNew);
    BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
    SelectObject(hdcSrc, hbmOld);
    DeleteDC(hdcSrc);
    DeleteDC(hdcDst);
    return hbmNew;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows环境下32位汇编语言程序设计 第2版(罗文斌) 完整光盘内容,包含每章内容的完整代码 本光盘所包含目录的说明 根目录下的 *.pdf ;附录A、B、C的电子版文档 Chapter02\Test ;测试编译环境 Chapter03\HelloWorld ;Hello World Chapter04\FirstWindow ;用Win32汇编写第一个窗口 Chapter04\FirstWindow-1 ;用Win32汇编写第一个窗口 Chapter04\SendMessage ;窗口间的消息互发 Chapter04\SendMessage-1 ;窗口间的消息互发 Chapter05\Menu ;使用资源 - 使用菜单 Chapter05\Icon ;使用资源 - 使用图标 Chapter05\Dialog ;使用资源 - 使用对话框 Chapter05\Listbox ;使用资源 - 使用列表框 Chapter05\Control ;使用资源 - 使用子窗口控件 Chapter05\ShowVersionInfo ;使用资源 - 显示版本信息资源的程序 Chapter05\VersionInfo ;使用资源 - 使用版本信息资源 Chapter06\Timer ;定时器的使用 Chapter07\DcCopy ;在两个窗口的 DC 间互相拷贝屏幕 Chapter07\Clock ;模拟时钟程序 Chapter07\BmpClock ;用 Bitmap 图片做背景的模拟时钟程序 Chapter07\TestObject ;一些常见的绘图操作 Chapter08\CommDlg ;使用通用对话框 Chapter09\Toolbar ;使用工具栏 Chapter09\StatusBar ;使用状态栏 Chapter09\Richedit ;使用丰富编辑控件 Chapter09\Wordpad ;一个完整的文本编辑器例子 Chapter09\SubClass ;窗口的子类化例子 Chapter09\SuperClass ;窗口的超类化例子 Chapter10\MemInfo ;显示当前内存的使用情况 Chapter10\Fragment ;内存碎片化的演示程序 Chapter10\FindFile ;全盘查找文件的例子 Chapter10\FormatText ;文件读写例子 Chapter10\FormatText\FileMap ;使用内存映射文件进行文件读写的例子 Chapter10\MMFShare ;使用内存映射文件进行进程间数据共享 Chapter11\Dll\Dll ;最简单的动态链接库例子 - 编写 DLL Chapter11\Dll\MASM Sample ;最简单的动态链接库例子 - 使用 DLL Chapter11\Dll\VC++ Sample ;最简单的动态链接库例子 - 在VC++中使用汇编编写的DLL Chapter11\KeyHook ;Windows 钩子的例子 - 监听键盘动作 Chapter11\RecHook ;Windows 日志记录钩子的例子 - 监听键盘动作 Chapter12\Counter ;有问题的程序 - 一个计数程序 Chapter12\Thread ;用多线程的方式解决上一个程序的问题 Chapter12\Event ;使用事件对象 Chapter12\ThreadSynErr ;一个存在同步问题的多线程程序 Chapter12\ThreadSyn\UseCriticalSection ;使用临界区对象解决多线程同步问题 Chapter12\ThreadSyn\UseEvent ;使用事件对象解决多线程同步问题 Chapter12\ThreadSyn\UseMutex ;使用互斥对象解决多线程同步问题 Chapter12\ThreadSyn\UseSemaphore ;使用信号灯对象解决多线程同步问题 Chapter13\CmdLine ;使用命令行参数 Chapter13\Process ;创建进程的例子 Chapter13\ProcessList ;显示系统中运行的进程列表 Chapter13\Patch1 ;一个内存补丁程序 Chapter13\Patch2 ;一个内存补丁程序 Chapter13\Patch3 ;一个内存补丁程序 Chapter13\HideProcess9x ;Windows 9x下的进程隐藏 Chapter13\RemoteThreadDll ;用 DLL 注入的方法实现远程进程 Chapter13\RemoteThread ;不依靠任何外部文件实现远程进程 Chapter14\TopHandler ;使用筛选器处理异常 Chapter14\SEH01 ;最基本结构化异常处理例子 Chapter14\SEH02 ;改进后的结构化异常处理例子 Chapter14\Unwind ;异常处理中的展开操作例子 Chapter15\Ini ;使用 INI 文件 Chapter15\Reg ;操作注册表的例子 Chapter15\Associate ;操作注册表实现文件关联 Chapter16\TcpEcho ;实现 TCP 服务器端的简单例子 Chapter16\Chat-TCP ;用 TCP 协议实现的聊天室例子 Chapter17\PeInfo ;查看 PE 文件的基本信息 Chapter17\Import ;查看 PE 文件的导入表 Chapter17\Export ;查看 PE 文件的导出表 Chapter17\Resource ;查看 PE 文件的资源列表 Chapter17\Reloc ;查看 PE 文件的重定位信息 Chapter17\NoImport ;不使用导入表调用 API 函数 Chapter17\AddCode ;在 PE 文件上附加可执行代码的例子 Chapter18\OdbcSample ;用ODBC操作数据库的例子 Appendix A\EchoLine ;控制台输入输出的例子 Appendix B\MsgWindow01 ;消息机制试验 1 Appendix B\MsgWindow02 ;消息机制试验 2 Appendix B\MsgWindow03 ;消息机制试验 3 Appendix B\MsgWindow04 ;消息机制试验 4 Appendix C\BrowseFolder ;浏览目录对话框
简介 WTL 是 Windows Template Library 的缩写,由微软的ATL(Active Template Library) 小组开发,主要是基于 ATL 对Win32API 的封装。从 2.0 后,功能逐步完善,成为了一个完整的支持窗口的框架(windows framework)。 WTL 功能不如MFC完善,但是比 MFC 更小巧,不依赖 MFC 的DLL。 WTL 不是微软的正式产品,没有微软的官方支持。 WTL有什么? 它给各种类型的应用程序提供了一个基本的框架.注意,虽然没有MFC那样的文档/视结构,但是有视(views). 在WTL有大量的代码来管理视,而且加入自己的代码也很容易. WTL有AppWizard,可以生成SDI,MDI和多线程SDI程序多线程SDI跟IE或Windows Explorer很像.它看起来是打开了多个程序实例,实际上这些窗口都是属于一个进程的). 另外,程序可以是基于对话框的,也可以是基于视的.视可以是基于CWindowImpl的,也可以是基于控件,甚至是IE里的一个HTML页.可以选择程序是否需要一个rebar, command bar (CE-like), toolbar 和/或status bar.另外,程序可以支持ActiveX控件,以及成为一个COM服务器. 这里有几个关于视的选项. WTL提供splitter窗口类(这样在一个视里您可以有两个窗口)和scroll窗口类(这样您的窗口可以比它显示的"视"小). WTL也有个类似MFC的UpDateUI的东西,但是它们不是很一样 - 主要的区别是您需要把需要更新的项用宏映射标注出来,然后您在您的类里加入执行UpdateUI的代码. DDX/DDV在WTL也支持,同样类似MFC,但有不同. 必须加一个宏映射来实现DoDataExchange,然后加入调用它的代码. 现在WTL也有GDI类了.然而,HDC的封装类就像CWindow一样,只进行了很简单的封装 - 它几乎没有加入任何新的功能.不过,在WTL,你可以得到播放meta文件和OpenGL支持. 最有价值的我猜应该是打印机DC的那些继承类 - WTL有打印机支持,甚至打印预览. 当然也有GDI对象的封装. 诸如画笔,画刷,区域等. WTL对所有的Win32 (和W2K) 通用对话框进行了封装.同样尽管简单,但是它的确使请求字体或者文件变的非常的简单. 合成了旧的AtlControls.h,新加了一些封装类. 这些封装类封装了W2K控件,以及一些不属于Win32的"控件",像Command Bar, bitmap button, hyperlink 和 wait cursor. WTL 最终把消息分离带入了ATL! 一些新的MSG映射宏将消息分离,调用您类里的消息处理函数.消息处理函数的参数的值是从消息分离得到的.唯一令人头痛的是,您需要查看头文件以确定函数参数的意义. 最后,WTL还有一些实用类.最重要的是CString. 不错,它是从MFC克隆得到的(copy on write),具有(在我知道的范围内)MFC版本的所有方法.还有查找文件的API的封装类,以及CRect, CSize and CPoint. WTL中没有什么? 没有Document支持 WTL提供了frame和view,但是没有document。WTL所关注的是用户界面,而document是不可见的,所以它不是WTL关心的范围。 没有Active Document支持 没有ISAPI支持 没有WinInet支持 没有对线程和同步进行包装 没有数据库支持 总结 如果打算写一个Win32 界面程序,我建议您在考虑MFC之前,先试试WTL.使用WTL来写您的代码, 程序将变得小巧些,也更有效率些.使用WTL, 还将得到ATL支持COM好处.可以在2000年一月份的平台SDK中找到WTL.在MSI选项页的Source Code section下. 无 。
××××××××××××××××××××××××××× × 《Windows环境下32位汇编语言程序设计》 × × 附书代码说明 × ××××××××××××××××××××××××××× 1. 编译器和链接器 本附书代码全部采用 MASM 格式编写,推荐使用 MASM32 软 件包作为编译环境,MASM32 软件包可以在以下地址下载: MASM32官方站点: http://www.masm32.com 安装及开发的操作系统建议采用32位的Windows 7,在64位操作系统 上安装 MASM32 软件包,安装生成的Lib文件可能不兼容。 安装完成以后请将本光盘根目录下的环境设置批处理文件Var.bat 拷贝到 Masm32\bin 目录下,并根据 MASM32 的安装位置编辑修改 Var.bat 文件中的相关目录名称。 2. 代码维护工具 每个例子都包括了描述编译、链接方法的 makefile 文件, 使用 nmake 工具可以自动根据此文件进行编译链接,nmake 工 具可以从 Visual C++ 的 bin 目录中找到,也可以从作者的网 站中下载。 3. 编译环境和编译方法 建议使用命令行方式进行编译,以编译 Chapter02\Test 目录中 Test.asm 为例,步骤是: I. 打开一个“命令提示符”窗口。 II. 进入环境设置批处理文件 Var.bat 所在目录并执行 它,以后就可以使用这个“命令提示符”窗口编译 文件了。 x: cd \masm32\bin var III. 进入源代码目录: cd \chapter02\test IV. 使用 nmake 工具进行编译链接: nmake V. 执行编译好的可执行文件。 如果需要对源代码进行修改,不必关闭“命令提示符”窗口, 只要切换到编辑器窗口,在修改 *.asm 文件后重复进行第 IV 和 第 V 步骤即可。 # 特别注意:将光盘中的代码拷贝到硬盘后,必须将文件的只读属性去除! 4. 本光盘所包含目录的说明 根目录下的 *.pdf ;附录A、B、C的电子版文档 Chapter02\Test ;测试编译环境 Chapter03\HelloWorld ;Hello World Chapter04\FirstWindow ;用Win32汇编写第一个窗口 Chapter04\FirstWindow-1 ;用Win32汇编写第一个窗口 Chapter04\SendMessage ;窗口间的消息互发 Chapter04\SendMessage-1 ;窗口间的消息互发 Chapter05\Menu ;使用资源 - 使用菜单 Chapter05\Icon ;使用资源 - 使用图标 Chapter05\Dialog ;使用资源 - 使用对话框 Chapter05\Listbox ;使用资源 - 使用列表框 Chapter05\Control ;使用资源 - 使用子窗口控件 Chapter05\ShowVersionInfo ;使用资源 - 显示版本信息资源的程序 Chapter05\VersionInfo ;使用资源 - 使用版本信息资源 Chapter06\Timer ;定时器的使用 Chapter07\DcCopy ;在两个窗口的 DC 间互相拷贝屏幕 Chapter07\Clock ;模拟时钟程序 Chapter07\BmpClock ;用 Bitmap 图片做背景的模拟时钟程序 Chapter07\TestObject ;一些常见的绘图操作 Chapter08\CommDlg ;使用通用对话框 Chapter09\Toolbar ;使用工具栏 Chapter09\StatusBar ;使用状态栏 Chapter09\Richedit ;使用丰富编辑控件 Chapter09\Wordpad ;一个完整的文本编辑器例子 Chapter09\SubClass ;窗口的子类化例子 Chapter09\SuperClass ;窗口的超类化例子 Chapter10\MemInfo ;显示当前内存的使用情况 Chapter10\FindFile ;全盘查找文件的例子 Chapter10\FormatText ;文件读写例子 Chapter10\FormatText\FileMap ;使用内存映射文件进行文件读写的例子 Chapter10\MMFShare ;使用内存映射文件进行进程间数据共享 Chapter11\Dll\Dll ;最简单的动态链接库例子 - 编写 DLL Chapter11\Dll\MASM Sample ;最简单的动态链接库例子 - 使用 DLL Chapter11\Dll\VC++ Sample ;最简单的动态链接库例子 - 在VC++中使用汇编编写的DLL Chapter11\KeyHook ;Windows 钩子的例子 - 监听键盘动作 Chapter11\RecHook ;Windows 日志记录钩子的例子 - 监听键盘动作 Chapter12\Counter ;有问题的程序 - 一个计数程序 Chapter12\Thread ;用多线程的方式解决上一个程序的问题 Chapter12\Event ;使用事件对象 Chapter12\ThreadSynErr ;一个存在同步问题的多线程程序 Chapter12\ThreadSyn\UseCriticalSection ;使用临界区对象解决多线程同步问题 Chapter12\ThreadSyn\UseEvent ;使用事件对象解决多线程同步问题 Chapter12\ThreadSyn\UseMutex ;使用互斥对象解决多线程同步问题 Chapter12\ThreadSyn\UseSemaphore ;使用信号灯对象解决多线程同步问题 Chapter13\CmdLine ;使用命令行参数 Chapter13\Process ;创建进程的例子 Chapter13\ProcessList ;显示系统中运行的进程列表 Chapter13\Patch1 ;一个内存补丁程序 Chapter13\Patch2 ;一个内存补丁程序 Chapter13\Patch3 ;一个内存补丁程序 Chapter13\HideProcess9x ;Windows 9x下的进程隐藏 Chapter13\RemoteThreadDll ;用 DLL 注入的方法实现远程进程 Chapter13\RemoteThread ;不依靠任何外部文件实现远程进程 Chapter14\TopHandler ;使用筛选器处理异常 Chapter14\SEH01 ;最基本结构化异常处理例子 Chapter14\SEH02 ;改进后的结构化异常处理例子 Chapter14\Unwind ;异常处理中的展开操作例子 Chapter15\Ini ;使用 INI 文件 Chapter15\Reg ;操作注册表的例子 Chapter15\Associate ;操作注册表实现文件关联 Chapter16\TcpEcho ;实现 TCP 服务器端的简单例子 Chapter16\Chat-TCP ;用 TCP 协议实现的聊天室例子 Chapter17\PeInfo ;查看 PE 文件的基本信息 Chapter17\Import ;查看 PE 文件的导入表 Chapter17\Export ;查看 PE 文件的导出表 Chapter17\Resource ;查看 PE 文件的资源列表 Chapter17\Reloc ;查看 PE 文件的重定位信息 Chapter17\NoImport ;不使用导入表调用 API 函数 Chapter17\AddCode ;在 PE 文件上附加可执行代码的例子 Chapter18\OdbcSample ;用ODBC操作数据库的例子 Appendix A\EchoLine ;控制台输入输出的例子 Appendix B\MsgWindow01 ;消息机制试验 1 Appendix B\MsgWindow02 ;消息机制试验 2 Appendix B\MsgWindow03 ;消息机制试验 3 Appendix B\MsgWindow04 ;消息机制试验 4 Appendix C\BrowseFolder ;浏览目录对话框 6. 联系作者 虽然本书中所有的例子代码都已经在32位的Windows 98、Windows 2000、 Windows XP、Windows Vista和Windows 7下测试通过,但也有存在Bug的可能,如果 发现代码存在错误或者有其它问题,请告知作者,联系方法: E-mail:[email protected] 感谢您的支持! 作者:罗云彬
Introduction Like many of my colleagues in this industry, I learned Windows programming from Charles Petzold's Programming Windows—a classic programming text that is the bible to an entire generation of Windows programmers. When I set out to become an MFC programmer in 1994, I went shopping for an MFC equivalent to Programming Windows. After searching in vain for such a book and spending a year learning MFC the old-fashioned way, I decided to write one myself. It's the book you hold in your hands. And it's the book I would like to have had when I was learning to program Windows the MFC way. MFC, as you probably already know, is Microsoft's C++ class library for Windows programming. Programming Windows with MFC isn't a book about C++; rather, it's a book about writing 32-bit Windows applications in C++ using MFC rather than the Windows API as the chief means of accessing the operating system's essential features and services. It was written with two kinds of people in mind: Windows API programmers who want to learn MFC Programmers who have never before programmed Windows Whichever camp you fall into, I assume that you know the C++ programming language already and are comfortable with basic C++ idioms such as derived classes and virtual functions. If these assumptions are true, you're ready to begin climbing the hill that is MFC programming. Even veteran Windows programmers frequently find MFC code confusing the first time they see it, in part because of the presence of code created by the MFC code-generating wizards in Visual C++ and in part because of the countless lines of code hidden away in MFC classes such as CFrameWnd, CDocument, and CView. That's why this book takes a rather unusual approach to teaching MFC. It begins by having you write MFC code by hand (without the wizards) and by utilizing MFC 1.0-style application architectures—that is, applications that use neither documents nor views. Only after you've mastered the fundamentals and become acquainted with basic MFC classes such as CWnd and CWinApp do I introduce the wizards and teach you how to take advantage of MFC's document/view architecture. Along the way, you build a understanding from the ground up of the message-oriented nature of Windows and of key components of Windows itself, such as the Graphics Device Interface (GDI). I believe that this approach makes learning MFC not only less intimidating, but also more enjoyable. I think that you'll agree once you've worked your way through the book and can look back on the learning experience from the standpoint of a knowledgeable Windows programmer. Programming Windows with MFC is divided into four parts. Part I introduces the core tenets of MFC and Windows programming, beginning with a simple "Hello, MFC" application and introducing, one by one, menus, controls, dialog boxes, and other application building blocks. Part II builds on the foundation laid in Part I with a detailed look at the document/view architecture. In particular, Chapters 9, 10, and 11 reveal much of the "magic" behind documents and views and explain not only how to write basic document/view applications but also how to implement some not so basic features such as split-window views of a document and print previews. Part III covers some of the more advanced features of Windows and MFC—features such as color palettes, bitmap handling, and multiple threads of execution. In Part IV, you'll learn how MFC wraps its arms around COM, OLE, and ActiveX and how to write COM-enabled applications and software components. By the time you're finished with Chapter 21, you'll be well versed in the art of 32-bit Windows programming using MFC. And you'll have prodigious amounts of sample code to draw from when it's time to strike out on your own and write your first great Windows application. What's New in the Second Edition Those of you who read the first edition of this book will notice two rather obvious changes in the second edition. First, this edition contains seven new chapters. One is devoted to the MFC view classes; another covers the MFC collection classes; one introduces MFC file I/O and serialization mechanisms; and four cover the relationship between MFC and COM. MFC is not the general-purpose COM framework that the Active Template Library (ATL) is, but MFC makes certain types of COM programming exceptionally easy. For example, MFC greatly simplifies the task of writing ActiveX controls, and it makes writing Automation servers—programs that use COM to expose their functionality to scripting clients—a breeze. The second major change in this edition has to do with wizards. The first edition didn't cover the MFC wizards at all. The second edition uses hand-generated code in Chapters 1 through 3 but then shifts gears and begins using AppWizard and ClassWizard in Chapter 4. Why the change of heart? I still believe that code-generating wizards are an impediment to learning and should be used only by knowledgeable programmers, but I've also come to realize that in the real world, MFC programmers use the wizards. For certain tasks—writing ActiveX controls, for example—it doesn't make sense not to use the wizards. So after much deliberation, I decided I would be remiss not to cover them. Despite the new material regarding wizards, however, this is not—and never will be—a book about clicking buttons in AppWizard. After introducing a fundamental skill, such as how to write a message handler with ClassWizard, I thereafter let the source code do the talking and assume that you can figure out how the source code was created. Keep in mind that the wizards never do anything you can't do yourself, so it's perfectly feasible to type in every source code listing by hand if you'd like to. The downside to using wizards in a book that teaches MFC programming is that they produce code that isn't fit to publish. The first edition of this book included printed listings for each and every source code file. This one does not. It contains printed copies of relevant source code files and provides the others on CD. Why? Because printing a source code file that's 50 percent meat and 50 percent fat adds bulk to a book without adding content. Some of the code produced by the MFC AppWizard in Visual C++ 6.0 won't even compile. (For details, see Chapter 4.) I'm not very proud of the parts of my book that the wizards created, because those portions are littered with arbitrary blank lines, comments that lack consistent style, and unnecessary functions. For someone who takes pride in writing concise, readable sample code, wizard output is a bitter pill to swallow. Nevertheless, wizards represent the new world order in Windows programming, and they're something that you, I, and everyone else must get used to. It's a shame that the Visual C++ team won't give us real wizards to play with instead of the toys that they pass off as wizards today. Until they do, we must make do with what we have. What's On the CD The CD that accompanies this book contains source code and executables for all the sample programs presented in the book. All samples were written and compiled with Visual C++ 6.0 and MFC 6.0 and tested on various Win32 platforms. Unless otherwise noted, all are compatible with Windows 98, Windows NT 4.0, and Windows 2000. Most are also compatible with Windows 95 and Windows NT 3.51. You can copy the contents of the CD to your hard disk by running the setup program found in the CD's root directory, or you can retrieve the files directly from the CD's \Code directory. The \Code directory contains one subdirectory for each chapter of the book—Chap01, Chap02, and so on. Inside these subdirectories you'll find the sample programs. Each set of source code files is accompanied by a release-build EXE as well as a Visual C++ workspace (DSW) file that you can open with Visual C++'s Open Workspace command. From Me to You (and You to Me) From the day in 1995 when I began writing the first edition of Programming Windows with MFC, my goal has been to provide C++ programmers with the same kind of timeless, irreplaceable resource that Programming Windows is to C programmers. Whether I've achieved that goal, I'll let you be the judge. I want to know what you think about Programming Windows with MFC, and I particularly want to hear from you if you find mistakes. You can reach me by sending mail to [email protected] or by visiting my Web site at www.prosise.com. At that site you'll find up-to-date information regarding the book, a list of errata, and information about other projects that I'm working on. Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I know that you could have chosen any number of MFC books besides this one. I thank you for purchasing Programming Windows with MFC, and I sincerely hope you conclude that your money was well spent. Enjoy! Jeff Prosise
Windows环境下32位汇编语言程序设计 第2版(罗文斌) 完整光盘内容,包含每章内容的完整代码 本光盘所包含目录的说明 根目录下的 *.pdf ;附录A、B、C的电子版文档 Chapter02\Test ;测试编译环境 Chapter03\HelloWorld ;Hello World Chapter04\FirstWindow ;用Win32汇编写第一个窗口 Chapter04\FirstWindow-1 ;用Win32汇编写第一个窗口 Chapter04\SendMessage ;窗口间的消息互发 Chapter04\SendMessage-1 ;窗口间的消息互发 Chapter05\Menu ;使用资源 - 使用菜单 Chapter05\Icon ;使用资源 - 使用图标 Chapter05\Dialog ;使用资源 - 使用对话框 Chapter05\Listbox ;使用资源 - 使用列表框 Chapter05\Control ;使用资源 - 使用子窗口控件 Chapter05\ShowVersionInfo ;使用资源 - 显示版本信息资源的程序 Chapter05\VersionInfo ;使用资源 - 使用版本信息资源 Chapter06\Timer ;定时器的使用 Chapter07\DcCopy ;在两个窗口的 DC 间互相拷贝屏幕 Chapter07\Clock ;模拟时钟程序 Chapter07\BmpClock ;用 Bitmap 图片做背景的模拟时钟程序 Chapter07\TestObject ;一些常见的绘图操作 Chapter08\CommDlg ;使用通用对话框 Chapter09\Toolbar ;使用工具栏 Chapter09\StatusBar ;使用状态栏 Chapter09\Richedit ;使用丰富编辑控件 Chapter09\Wordpad ;一个完整的文本编辑器例子 Chapter09\SubClass ;窗口的子类化例子 Chapter09\SuperClass ;窗口的超类化例子 Chapter10\MemInfo ;显示当前内存的使用情况 Chapter10\Fragment ;内存碎片化的演示程序 Chapter10\FindFile ;全盘查找文件的例子 Chapter10\FormatText ;文件读写例子 Chapter10\FormatText\FileMap ;使用内存映射文件进行文件读写的例子 Chapter10\MMFShare ;使用内存映射文件进行进程间数据共享 Chapter11\Dll\Dll ;最简单的动态链接库例子 - 编写 DLL Chapter11\Dll\MASM Sample ;最简单的动态链接库例子 - 使用 DLL Chapter11\Dll\VC++ Sample ;最简单的动态链接库例子 - 在VC++中使用汇编编写的DLL Chapter11\KeyHook ;Windows 钩子的例子 - 监听键盘动作 Chapter11\RecHook ;Windows 日志记录钩子的例子 - 监听键盘动作 Chapter12\Counter ;有问题的程序 - 一个计数程序 Chapter12\Thread ;用多线程的方
您可以使用 C# 中的 Windows 录制 API 来实现屏幕录制功能。以下是一些步骤: 1. 引入 Windows API: ```csharp using System.Runtime.InteropServices; ``` 2. 声明 Windows 录制 API: ```csharp [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight); [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern int DeleteDC(IntPtr hDC); [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern int DeleteObject(IntPtr hObject); ``` 3. 创建屏幕截图: ```csharp public static Bitmap CaptureScreen() { // 获取屏幕分辨率 int screenX = Screen.PrimaryScreen.Bounds.Width; int screenY = Screen.PrimaryScreen.Bounds.Height; // 创建一个屏幕大小的位图 Bitmap bitmap = new Bitmap(screenX, screenY); // 获取屏幕 DC IntPtr hDC = GetWindowDC(GetDesktopWindow()); IntPtr hMemDC = CreateCompatibleDC(hDC); IntPtr hBitmap = CreateCompatibleBitmap(hDC, screenX, screenY); // 将位图选入内存 DC IntPtr hOldBitmap = SelectObject(hMemDC, hBitmap); // 复制屏幕内容到内存 DC BitBlt(hMemDC, 0, 0, screenX, screenY, hDC, 0, 0, SRCCOPY); // 将位图从内存 DC 保存到 Bitmap 对象 SelectObject(hMemDC, hOldBitmap); DeleteDC(hMemDC); ReleaseDC(IntPtr.Zero, hDC); bitmap.Save("screenshot.bmp"); // 释放资源 DeleteObject(hBitmap); return bitmap; } ``` 4. 使用 Windows Media Encoder 编码视频: ```csharp public static void RecordScreen() { // 创建 Windows Media Encoder 对象 WMEncoder encoder = new WMEncoder(); // 配置编码器 IWMEncSourceGroupCollection sourceGroups = encoder.SourceGroupCollection; IWMEncSourceGroup sourceGroup = (IWMEncSourceGroup2)sourceGroups.Add("SG_1"); IWMEncVideoSource2 videoSource = (IWMEncVideoSource2)sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO); videoSource.SetInput("ScreenCap://ScreenCapture1", "", ""); videoSource.SetInputProps(640, 480, 15, 1000000); // 设置输出文件名和格式 IWMEncFile file = encoder.File; file.LocalFileName = "output.wmv"; file.Type = WMENC_FILE_TYPE.WMENC_FILE_TYPE_AV; // 开始录制 encoder.Start(); // 循环截屏并编码为视频 while (true) { // 截屏 Bitmap bitmap = CaptureScreen(); // 将位图转换为位图数据 BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); byte[] bytes = new byte[data.Stride * data.Height]; Marshal.Copy(data.Scan0, bytes, 0, bytes.Length); // 将位图数据写入编码器 videoSource.SetInputData(ref bytes); videoSource.DoFrame(); // 释放资源 bitmap.UnlockBits(data); bitmap.Dispose(); } // 停止录制 encoder.Stop(); encoder = null; } ``` 这是一个简单的示例,您可以根据您的需求进行修改和扩展。请注意,此代码仅用于演示目的,实际使用中需要进行错误处理和资源管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值