WTL 的图片框
一篇文章,介绍如何使用 Trilobyte-Solutions.nl CPictureBox 控件加载和显示 *.bmp、*.jpg、*.png 和 *.gif 文件。
引言
如果您想显示图像,这段代码非常有用。它可以从磁盘或包含在您程序中的资源中加载 *.bmp、*.jpg、*.png 和 *.gif 文件。该控件具有滚动条支持和内置上下文菜单,方便使用,可以禁用。
使用代码
以下示例假定您希望图片框位于主视图的客户端区域。
// CMainFrm.h #include "PictureBox.h" class CMainFrame: { public: // Other code here WTL::CPictureBox m_PictureBox; }; // CMainFrm.cpp LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { // Some code // Create the Picture Box m_hwndClient = m_PictureBox.Create(m_hWnd, rcDefault, NULL, WS_VISIBLE|WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, WS_EX_CLIENTEDGE); // The control is created, now we can load a picture in it. // To load an image in the Picture Box use any of the functions below // Some more code }
CPictureBox
的创建函数与任何 CWindowImpl
派生类的创建函数一样。默认情况下,控件会将位图定位在视图的中心,并使用内置上下文菜单。CPictureBox
函数
// Load an image from a file // pszFileName is the path to the image // the function returns true if succesfull, otherwise it returns false. bool LoadBitmapFromFile(LPCTSTR pszFileName); // Load an image from a resource // hInstance is the handle to the HINSTANCE you want to load from // uiIDResource is the ID of the resource // the function returns true if succesfull, otherwise it returns false. bool LoadBitmapFromID(HINSTANCE hInstance, UINT uiIDResource) // Set the bitmap directly // hBitmap is the Handle to the bitmap // if bOwner is true, the CPictureBox will // destroy the hBitamp when it is destroyed // otherwise you have to clean up the hBitmap yourself void SetBitmap(HBITMAP hBitmap, bool bOwner = true) // Detaches the bitmap from the CPictureBox and returns the bitmap HBITMAP GetBitmap() // returns a save handle to the bitmap HBITMAP GetSaveBitmap()const // if bCenter is true, the control will position // the bitmap in the center of the view // otherwise the bitmap will be positioned in the // top left corner of the view void CenterPicture(bool bCenter) // if bStretch is true, the control will stretct the // bitmap so that it fits the view // otherwise the bitmap will be drawn normaly void StretchPicture(bool bStretch) // if bUseMenu is true, the control will use the build in context menu // otherwise the context menu will not be used void UseMenu(bool bUseMenu)
关注点
我编写这段代码是因为 WTL 没有可以显示 *.jpg、*.png、*.gif 或具有滚动条支持的类。
附加信息
有关更多信息、问题和错误报告,请访问我的网站:http://www.Trilobyte-Solutions.nl,或通过 bertwillems@trilobyte-solutions.nl 与我联系。