位图

    技术2022-05-19  19

    1,Windows的位图实际上是一些和显示象素相对应的位阵列。

    2,GDI位图是设备相关位图,用MS基本类库(MFC)中的CBitmap类表示的,依赖具体的设备。

    3,DIB设备无关位图,比GDI位图有许多编程优势。任何运行Windows的机器都可以处理DIB位图。DIB位图通常以.BMP文件形式保留在磁盘中,或作为资源保存在程序的EXE或DLL文件中。WIN32API只直接支持DIB格式文件。

    4,Windows在位图中使用模糊颜色。GDI位图也是GDI对象,使用时先创建(并初始化),再把它选入设备环境,使用完后还要删除掉。 注意: 使用GDI位图的时候,不能直接把位图选进显示设备环境或打印机设备环境中。必须利用CDC::CreateCompatibleDC函数为位图创建一个特殊的内存设备环境,然后使用CDC::StretchBlt或CDC::BitBlt函数把内存设备环境中的各个位复制到真正的设备环境中。 说明: 1)CDC::CreateCompatibleDC  virtual BOOL CreateCompatibleDC( CDC* pDC ); // Creates a memory device context that is compatible with the device specified by pDC. A memory device context is a block of memory that represents a display surface. It can be used to prepare images in memory before copying them to the actual device surface of the compatible device. 2)CDC::BitBlt  BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop ); //Copies a bitmap from the source device context to this current device context. 3)CDC::StretchBlt BOOL StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop ); //Moves a bitmap from a source rectangle and device into a destination rectangle, stretching or compressing the bitmap if necessary to fit the dimensions of the destination rectangle. 4)应用事例: void CGDIView::OnDraw(CDC* pDC) {  CBitmap bitmap;  bitmap.LoadBitmap(IDB_BITMAP1);  CDC *pdcm=new CDC;  pdcm->CreateCompatibleDC(pDC);  pdcm->SelectObject(&bitmap);  pDC->BitBlt(100,100,54,96,pdcm,0,0,SRCCOPY);  delete pdcm;  //pDC->StretchBlt(100,100,500,50,pdcm,0,0,50,50,SRCCOPY); } 注:事例中显示映射模式为MM_TEXT,MM_TEXT下每个位图象素都映射到一个显示器象素。如果在其它的模式下需要进行一些伸缩变换处理。处理缩小的位图时候可以调用CDC::SetStretchBltMode函数(参数可选COLORONCOLOR)使缩小的位图显示更好些。 在非MM_TEXT模式下,使用BitBlt或StretchBlt函数,如果GDI要对其进行伸缩变换,则更新的速度较慢.可以使用以下代码代替BitBlt语句可加快其更新速度: pDC->SetMapMode(MM_LOENGLISH);//设置映射模式为MM_LOENGLISH。 CSize size(54,96); pDC->DPtoLP(&size); pDC->StretchBlt(0,0,size.cx,-size.cy,pdcm,0,0,54,96,SRCCOPY);//MM_LOENGLISH模式下注意-size.cy前的负号不能少。

    5,事例GDI中函数说明: 1)CGDIObject::GetObject int GetObject( int nCount, LPVOID lpObject ) const; 参数说明: nCount:Specifies the number of bytes to copy into the lpObject buffer. lpObject:Points to a user-supplied buffer that is to receive the information. //Return Value:The number of bytes retrieved; otherwise 0 if an error occurs. //Fills a buffer with data that describes the Windows GDI object attached to the CGDIObject object. // Object  Buffer type CPen   LOGPEN CBrush   LOGBRUSH CFont   LOGFONT CBitmap  BITMAP CPalette  WORD CRgn   Not supported //If the object is a CBitmap object, GetObject returns only the width, height, and color format information of the bitmap. The actual bits can be retrieved by using CBitmap::GetBitmapBits.

    2)CGDIObject::GetSafeHandle HGDIOBJ GetSafeHandle( ) const; //Return Value:A HANDLE to the attached Windows GDI object; otherwise NULL if no object is attached.

    6,事例说明:位图有程序自己生成,并且能使位图在屏幕上平滑移动。原理:首先将所选择的位图绘制在内存设备环境中,然后再将它反复快速地在屏幕上进行显示。

    7,DIB编程(出于个人需要,这里做概要笔记,详见P190-204) 1)在MFC库中有GDI位图的类CBitmap,但没有DIB的类。 2)DIB包含一个二维数组,数组的元素为象素。 3)DIB是标准的Windows位图格式,且BMP文件包含一个DIB结构。 4)DIB访问函数: SetDIBitsToDevice//直接在显示器或打印机上显示DIB,不进行缩放,位图每一位对应一显示象素或一打印点。 StretchDIBits//仿照StretchBlt类似的方式将DIB显示在显示器或打印机上。 GetDIBits//函数利用申请到的内存,由GDI位图来构造DIB。 CreateDIBitmap//由DIB来创建GDI位图。 CreateDIBSection//创建一种称为DIB项的DIB,然后返回一个GDI句柄。提供了DIB和GDI位图最好的特性。可以直接访问DIB的内存,还可以在DIB中调用GDI函数画图。 5)LoadImage//The LoadImage function loads an icon, cursor, or bitmap. LoadImage可以直接从一个磁盘文件中读入位图 6)DrawDibDraw//The DrawDibDraw function draws a DIB to the screen. DrawDibDraw需要MM_TEXT坐标和MM_TEXT映射模式。

    8,在按钮上设置位图 1)先设置按钮的Owner Draw属性,然后在对话框类中写一个消息控制函数,在该按钮控制的窗口里画图。 2)使用MFC的CBitmapButton类实现在按钮在设置位图。步骤如下: 第一步:在对话框中添加按钮,并设置Owner Draw属性,大小并不重要。(框架自动调整为位图大小) 第二步:添加位图资源(.bmp),并用相应的按钮Caption+D/U来标识位图。即在位图的属性ID栏中输入"按钮Caption+U"或"按钮Caption+D",其中名字两边的引号不能少,这样就表示了用名字标识资源而不是ID,U表示不翻转显示的位图,D表示翻转显示的位图。如:按钮Caption为[Button1],则在该按钮上贴位图的名字应为["Button1D"]或["Button1U"]。 第三步:为要设置位图的按钮,对应在按钮所在对话框类中添加CBitmapButton类型的数据成员。 第四步:映射按钮所在对话框类WM_INITDIALOG消息,在OnInitDialog函数中用刚添加的CBitmapButton类型的数据成员调用 AutoLoad函数把每个按钮和两个匹配的位图资源连接起来。如:VERIFY(CWnd::EnableWindow.AutoLoad(IDC_BUTTON1,this)); 说明一: CBitmapButton::AutoLoad BOOL AutoLoad( UINT nID, CWnd* pParent ); //nID:The button’s control ID. //pParent:Pointer to the object that owns the button. //AutoLoad Associates a button in a dialog box with an object of the CBitmapButton class, loads the bitmap(s) by name, and sizes the button to fit the bitmap.(注意哦,这里的loads the bitmap by name。) //Use the AutoLoad function to initialize an owner-draw button in a dialog box as a bitmap button. 说明二: VERIFY VERIFY( booleanExpression ) //VERIFY是MFC诊断宏,当参数booleanExpression 为 0 的时候,停止程序并显示一个对话框;当booleanExpression 不为零,则什么也不做。 说明三: 在为按钮设置的位图加名字标识的时候: "按钮Caption+U"  表示按钮凸起状态的位图 "按钮Caption+D"  表示按钮凹起状态的位图 "按钮Caption+F"  表示按钮有焦点状态的位图 "按钮Caption+X"  表示按钮无效状态的位图

    9,CWnd::EnableWindow函数可以是一个窗口有效或无效,可以调用它来使按钮等控件(窗口)无效或恢复有效。 CWnd::EnableWindow  BOOL EnableWindow( BOOL bEnable = TRUE ); //If this parameter is TRUE, the window will be enabled. If this parameter is FALSE, the window will be disabled. //EnableWindow Enables or disables mouse and keyboard input. When input is disabled, input such as mouse clicks and keystrokes is ignored. When input is enabled, the window processes all input. //An application can use this function to enable or disable a control in a dialog box. A disabled control cannot receive the input focus, nor can a user access it.


    最新回复(0)