realizepalette

本文详细介绍了RealizePalette函数的功能及使用方法。该函数用于从逻辑调色板映射调色板入口点到系统调色板中,适用于颜色密集型应用。文章还探讨了其在不同操作系统版本和设备环境下的行为。
  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数从当前逻辑调色板中映射调色板入口点到系统调色板中。

    函数原型:UINT RealizePalette(HDC hdc);

    参数:

    hdc:设备环境句柄。一个逻辑调色板被选择在该设备环境中。

    返回值:如果函数成功,函数返回值定义口点的数目,这些逻辑调色板中的入口点映射到系统调色板中;如果失败,返回值是GDI_ERROR。

    Windows NT:若想获得更多错误信息,请调用GetLastError函数。

    注释:通过调用GetDeviceCaps函数和定义RASTERCAPS常量。一个应用可以确定一个设备是否支持调色板操作。

    RealizePalette函数修改与指定设备环境有关的设备的调色板,如果该设备环境是一个内存设备环境,那么选进该设备环境的位图颜色表将被修改,如果该设备环境是珍上显示设备环境,那么该设备的物理调色板将被修改。

    逻辑颜色调色板是颜色密集型应用和系统之间的一个缓存,允许这些应用在不干扰其他窗口颜色的情况下使用任意多的颜色。

    当焦点在一个应用窗口,并且它调用RealizePalette函数时,则系统试图映射尽可能多的颜色,这对于活动窗口的应用也同样正确。

    Windows CE:Windows CE在前景和背景调色板应用之间并不做出仲裁,前景应用完全控制了系统调色板,因此,对于前景应用Windows CE并不执行任何颜色匹配操作。它仅仅用hdc参数的调色板入口点来覆盖系统调色板的入口点。

    Windows CE背景应用中不支持RealizePalette函数。

    如果与hdc相关的设备设置一个可设置的调色板,那么RealizePalette函数将执行失败,在使用RealizePaletle函数之前,先调用GetDevicecaps函数来断定一个设备是否有一个可设置的调色板。

    速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:2.0及以上版本;头文件:wingdi.h;库文件:gdi32.lib。

// ToolBoxBar.cpp: implementation of the CToolBoxBar class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "ToolBoxBar.h" #include "resource.h" #include "grouptybase.h" #include "Sp_drawView.h" #include "sp_draw.h" #include "group_doc.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif const COLORREF clrList = RGB (0, 204, 153); ///////////////////////////////////////////////////////////////////////////// // CToolBoxBar BEGIN_MESSAGE_MAP(CToolBoxBar, CBCGPDockingControlBar) //{{AFX_MSG_MAP(CToolBoxBar) ON_WM_CREATE() ON_WM_SIZE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CToolBoxBar construction/destruction CToolBoxBar::CToolBoxBar() { // TODO: add one-time construction code here m_grouptype.Empty(); } CToolBoxBar::~CToolBoxBar() { } ///////////////////////////////////////////////////////////////////////////// // CWorkspaceBar message handlers int CToolBoxBar::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CBCGPDockingControlBar::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy; rectDummy.SetRectEmpty (); // m_images.Create (IDB_SHORTCUTS, 32, 0, RGB (255, 0, 255)); // CBitmap bmpIcons; // bmpIcons.LoadBitmap (IDB_SHORTCUTS); m_images.Create (48, 64, ILC_COLOR32 | ILC_MASK, 0, 10); m_images.SetImageCount(10); // m_images.Add (&bmpIcons, RGB (255, 0, 255)); // Create CListCtrl windows. // TODO: create your own tab windows here: const DWORD dwStyle = LVS_ICON | LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL; m_wndList.Create (dwStyle, CRect (0, 0, 0, 0), this, (UINT)-1); m_wndList.SetBkColor (clrList); m_wndList.SetTextBkColor (clrList); m_wndList.m_pBar=this; // Setup list content: m_wndList.SetImageList (&m_images, LVSIL_NORMAL); //m_wndList.InsertItem (0,_T("One"),0); //m_wndList.InsertItem (1,_T("Two"),1); //m_wndList.InsertItem (2,_T("Three"),2); //m_wndList.InsertItem (3,_T("Four"),3); return 0; } void CToolBoxBar::OnSize(UINT nType, int cx, int cy) { CBCGPDockingControlBar::OnSize(nType, cx, cy); // List control should cover a whole client area: m_wndList.SetWindowPos (NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER); } bool SaveBitmapToFile(HBITMAP hBitmap , CString lpFileName) { HDC hDC; //设备描述表 int iBits; //当前显示分辨率下每个像素所占字节数 WORD wBitCount=16; //位图中每个像素所占字节数 DWORD dwPaletteSize=0, //定义调色板大小, 位图中像素字节大小 ,位图文件大小 , 写入文件字节数 dwBmBitsSize, dwDIBSize, dwWritten; BITMAP Bitmap; BITMAPFILEHEADER bmfHdr; //位图属性结构 BITMAPINFOHEADER bi; //位图文件头结构 LPBITMAPINFOHEADER lpbi; //位图信息头结构 HANDLE fh, hDib, hPal,hOldPal=NULL; //指向位图信息头结构,定义文件,分配内存句柄,调色板句柄 //计算位图文件每个像素所占字节数 hDC = CreateDC("DISPLAY",NULL,NULL,NULL); iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES); DeleteDC(hDC); if (iBits <= 1) wBitCount = 1; else if (iBits <= 4) wBitCount = 4; else if (iBits <= 8) wBitCount = 8; else if (iBits <= 24) wBitCount = 24; //计算调色板大小 if (wBitCount <= 8) dwPaletteSize = (1 << wBitCount) *sizeof(RGBQUAD); //设置位图信息头结构 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap); bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = Bitmap.bmWidth; bi.biHeight = Bitmap.bmHeight; bi.biPlanes = 1; bi.biBitCount = wBitCount; bi.biCompression = BI_RGB; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; dwBmBitsSize = ((Bitmap.bmWidth * wBitCount+31)/32)* 4 *Bitmap.bmHeight ; //为位图内容分配内存 hDib = GlobalAlloc(GHND,dwBmBitsSize+ dwPaletteSize+sizeof(BITMAPINFOHEADER)); lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib); *lpbi = bi; // 处理调色板 hPal = GetStockObject(DEFAULT_PALETTE); if (hPal) { hDC = ::GetDC(NULL); hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE); RealizePalette(hDC); } // 获取该调色板下新的像素值 GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)+dwPaletteSize, (LPBITMAPINFO)lpbi, DIB_RGB_COLORS); //恢复调色板 if (hOldPal) { SelectPalette(hDC, (HPALETTE)hOldPal, TRUE); RealizePalette(hDC); DeleteDC(hDC); } //创建位图文件 fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (fh == INVALID_HANDLE_VALUE) return FALSE; // 设置位图文件头 bmfHdr.bfType = 0x4D42; // "BM" dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize; bmfHdr.bfSize = dwDIBSize; bmfHdr.bfReserved1 = 0; bmfHdr.bfReserved2 = 0; bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize; // 写入位图文件头 WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); // 写入位图文件其余内容 WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL); //消除内存分配 GlobalUnlock(hDib); GlobalFree(hDib); CloseHandle(fh); return TRUE; } void CToolBoxBar::UpdateData() { m_images.SetImageCount(0); m_wndList.DeleteAllItems();//清空 CString filepath; filepath=m_dir+"*.grp"; CFileFind ff; BOOL res = ff.FindFile(filepath); float vX0,vX1,vY0,vY1; int w=48,h=64; int num=0; while(res) { res = ff.FindNextFile(); if((!ff.IsDirectory()) && (!ff.IsDots())) { CString strFile = ff.GetFilePath(); CString strTitle=ff.GetFileTitle(); CGroupTyBase grp(strFile); CDC dc; HDC hdc = ::GetWindowDC(this->GetSafeHwnd()); dc.Attach(hdc); CDC memDC;//创建内存DC memDC.CreateCompatibleDC(&dc); CBitmap bm;//位图 CSize sz(w, h); bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy); CBitmap * oldbm = memDC.SelectObject(&bm); CBrush brush;//绘制背景 brush.CreateSolidBrush(clrList); CRect rt; rt.SetRect(0,0,sz.cx,sz.cy); memDC.FillRect(&rt,&brush); grp.GetGroupRect(&vX0,&vY0,&vX1,&vY1); CDrawInfoData drawinfo(vX0,vY0,vX1,vY1,h,w); grp.DrawGroup(&memDC,&drawinfo);//文件内容绘制到缩略图。 //SaveBitmapToFile((HBITMAP)(bm->m_hObject),m_dir+strTitle+".bmp"); //m_images.Add(bm,RGB (255, 0, 255)); //m_images.Replace(num,bm,NULL); memDC.SelectObject(oldbm); //bm->DeleteObject(); memDC.DeleteDC(); dc.DeleteDC(); //m_images.Replace(num,bm,NULL); m_images.Add(&bm,RGB (255, 0, 255)); //SaveBitmapToFile((HBITMAP)(bm->m_hObject),m_dir+strTitle+".bmp"); //m_wndList.InsertItem (num,strTitle,num); num++; } } //m_wndList.SetImageList (&m_images, LVSIL_NORMAL); //CString outstr; //outstr.Format("count=%d",m_images.GetImageCount()); //TRACE(outstr); res = ff.FindFile(filepath); num=0; while(res) { res = ff.FindNextFile(); if((!ff.IsDirectory()) && (!ff.IsDots())) { CString strTitle=ff.GetFileTitle(); m_wndList.InsertItem (num,strTitle,num); num++; } } } ///////////////////////////////////////////////////////////////////////////// // CToolBoxList CToolBoxList::CToolBoxList() { m_nDragIndex=-1; m_bDragging=false; m_pBar=NULL; } CToolBoxList::~CToolBoxList() { } BEGIN_MESSAGE_MAP(CToolBoxList, CListCtrl) //{{AFX_MSG_MAP(CToolBoxList) ON_WM_ERASEBKGND() //}}AFX_MSG_MAP ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnLvnBegindrag) ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() ON_NOTIFY_REFLECT(NM_DBLCLK, OnNMDblclk) ON_COMMAND(ID_GROUP_NEW, OnGroupNew) ON_UPDATE_COMMAND_UI(ID_GROUP_NEW, OnUpdateGroupNew) ON_COMMAND(ID_GROUP_DEL, OnGroupDel) ON_UPDATE_COMMAND_UI(ID_GROUP_DEL, OnUpdateGroupDel) ON_WM_CONTEXTMENU() ON_COMMAND(ID_GROUP_MODIFY, OnGroupModify) ON_UPDATE_COMMAND_UI(ID_GROUP_MODIFY, OnUpdateGroupModify) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CToolBoxList message handlers BOOL CToolBoxList::OnEraseBkgnd(CDC* pDC) { CRect rectClient; GetClientRect (rectClient); pDC->FillSolidRect (rectClient, clrList); return TRUE; } void CToolBoxList::OnLvnBegindrag(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 if(GetSelectedCount() > 1) return; m_nDragIndex = pNMLV->iItem; CImageList* pImageList=GetImageList(LVSIL_NORMAL); POINT pt; GetItemPosition(m_nDragIndex, &pt); pImageList->BeginDrag(m_nDragIndex,CPoint(-8,-10)); pImageList->DragEnter(GetDesktopWindow(),pNMLV->ptAction); m_bDragging=true; SetCapture (); *pResult = 0; } void CToolBoxList::OnMouseMove(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (m_bDragging) { //// Move the drag image CPoint pt(point); //get our current mouse coordinates ClientToScreen(&pt); //convert to screen coordinates CImageList* pImageList=GetImageList(LVSIL_NORMAL); pImageList->DragMove(pt); //move the drag image to those coordinates } CListCtrl::OnMouseMove(nFlags, point); } void CToolBoxList::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (m_bDragging) { // Release mouse capture, so that other controls can get control/messages ReleaseCapture (); // Note that we are NOT in a drag operation m_bDragging = FALSE; CImageList* pImageList=GetImageList(LVSIL_NORMAL); // End dragging image pImageList->DragLeave (GetDesktopWindow ()); pImageList->EndDrag (); CPoint pt (point); //Get current mouse coordinates ClientToScreen (&pt); //Convert to screen coordinates // Get the CWnd pointer of the window that is under the mouse cursor CWnd* pDropWnd = WindowFromPoint (pt); ASSERT (pDropWnd); //make sure we have a window pointer // If window is CListCtrl, we perform the drop if (pDropWnd->IsKindOf (RUNTIME_CLASS (CTysView))) { tagDragDropInfo* pRec= new tagDragDropInfo; memset(pRec,0,sizeof(tagDragDropInfo)); CString filepath; filepath.Format("%s%s.grp",m_pBar->m_dir,GetItemText(m_nDragIndex,0)); strcpy(pRec->filepath,filepath); pRec->posX=pt.x; pRec->posY=pt.y; pDropWnd->SendMessage(CM_DRAGDROP,0,(LPARAM)pRec); } } CListCtrl::OnLButtonUp(nFlags, point); } void CToolBoxList::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); int index = pNMLV->iItem; if(GetItemText(index,0) == "") return; CString filepath; filepath.Format("%s%s.grp",m_pBar->m_dir,GetItemText(index,0)); CGroup_Doc* pDoc=NULL; POSITION pos=theApp.m_GroupTempDoc->GetFirstDocPosition(); while (pos!=NULL) { pDoc=(CGroup_Doc*)theApp.m_GroupTempDoc->GetNextDoc(pos); if (pDoc==NULL) continue; CString str=pDoc->GetPathName(); if (stricmp(str,filepath)==0) { POSITION pos2=pDoc->GetFirstViewPosition( ); if (pos2!=NULL) { CView* pView =pDoc->GetNextView(pos2); if (pView!=NULL) pView->GetParentFrame()->ActivateFrame(); return; } } } CSp_drawApp* pApp=(CSp_drawApp*)AfxGetApp(); if (!pApp->GetPermit()) { return; } pDoc=(CGroup_Doc* )theApp.m_GroupTempDoc->OpenDocumentFile(NULL); if (pDoc==NULL) { assert(false); AfxMessageBox("组合图元无法打开!"); return; } pDoc->OpenGrpFile(filepath); *pResult = 0; } void CToolBoxBar::PostNcDestroy() { // TODO: 在此添加专用代码和/或调用基类 //if (m_pBarList!=NULL) //{ // POSITION pos=m_pBarList->Find(this); // if (pos!=NULL) // m_pBarList->RemoveAt(pos); //} CBCGPDockingControlBar::PostNcDestroy(); } BOOL CToolBoxBar::DestroyWindow() { // TODO: 在此添加专用代码和/或调用基类 CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd(); if (pFrame!=NULL) { POSITION pos=pFrame->m_BarList.Find(this); if (pos!=NULL) return false; else return true; } return CBCGPDockingControlBar::DestroyWindow(); } void CToolBoxList::OnGroupNew() { // TODO: 在此添加命令处理程序代码 CSp_drawApp* pApp=(CSp_drawApp*)AfxGetApp(); //if (!pApp->GetPermit()) //{ // return; //} CGroup_Doc* pDoc=NULL; pDoc=(CGroup_Doc* )theApp.m_GroupTempDoc->OpenDocumentFile(NULL); if (pDoc==NULL) { assert(false); AfxMessageBox("组合图元无法打开!"); return; } if (pDoc->NewGrpFile(m_pBar->m_grouptype)==false) { POSITION pos=pDoc->GetFirstViewPosition(); while (pos!=NULL) { CView * pView= pDoc->GetNextView(pos); CFrameWnd* pWnd=pView->GetParentFrame(); //pWnd->CloseWindow(); pWnd->DestroyWindow(); //pView->DestroyWindow(); //pView->CloseWindow(); } //pDoc->DeleteContents(); } } void CToolBoxList::OnUpdateGroupNew(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->Enable(); } void CToolBoxList::OnGroupDel() { // TODO: 在此添加命令处理程序代码 CString filepath; // filepath.Format("%s%s.grp",m_pBar->m_dir,GetItemText(index,0)); POSITION pos =GetFirstSelectedItemPosition(); int hItem; if (pos!=NULL) hItem=GetNextSelectedItem(pos); else return; CSp_drawApp* pApp=(CSp_drawApp*)AfxGetApp(); if (!pApp->GetPermit()) { return; } CString str; str.Format("确定要删除类型[%s] 中的组合模版:%s",m_pBar->m_grouptype,GetItemText(hItem,0)); if (AfxMessageBox(str,MB_OKCANCEL)==IDOK) { filepath.Format("%s%s.grp",m_pBar->m_dir,GetItemText(hItem,0)); DeleteFile(filepath); } CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd(); if (pFrame!=NULL) pFrame->UpdateBarList(); } void CToolBoxList::OnUpdateGroupDel(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 POSITION pos =GetFirstSelectedItemPosition(); if (pos!=NULL) { pCmdUI->Enable(true); } else pCmdUI->Enable(false); } void CToolBoxList::OnContextMenu(CWnd* pWnd, CPoint point) { // TODO: 在此添加消息处理程序代码 if ((point.x<0)||(point.y<0)) return; CMenu menu; BOOL bSucc=menu.CreatePopupMenu(); if (!bSucc) return; OnUpdataPopMenu(&menu); menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this); menu.DestroyMenu(); } void CToolBoxList::OnUpdataPopMenu(CMenu *pMenu) { pMenu->AppendMenu(MF_ENABLED|MF_STRING,ID_GROUP_NEW,"新增"); POSITION pos =GetFirstSelectedItemPosition(); if (pos!=NULL) { pMenu->AppendMenu(MF_ENABLED|MF_STRING,ID_GROUP_DEL,"删除"); pMenu->AppendMenu(MF_ENABLED|MF_STRING,ID_GROUP_MODIFY,"修改"); } } void CToolBoxList::OnGroupModify() { // TODO: 在此添加命令处理程序代码 CSp_drawApp* pApp=(CSp_drawApp*)AfxGetApp(); if (!pApp->GetPermit()) { return; } POSITION pos =GetFirstSelectedItemPosition(); int index; if (pos!=NULL) index=GetNextSelectedItem(pos); else return; CString filepath; filepath.Format("%s%s.grp",m_pBar->m_dir,GetItemText(index,0)); CGroup_Doc* pDoc=NULL; pos=theApp.m_GroupTempDoc->GetFirstDocPosition(); while (pos!=NULL) { pDoc=(CGroup_Doc*)theApp.m_GroupTempDoc->GetNextDoc(pos); if (pDoc==NULL) continue; CString str=pDoc->GetPathName(); if (stricmp(str,filepath)==0) { POSITION pos2=pDoc->GetFirstViewPosition( ); if (pos2!=NULL) { CView* pView =pDoc->GetNextView(pos2); if (pView!=NULL) pView->GetParentFrame()->ActivateFrame(); return; } } } pDoc=(CGroup_Doc* )theApp.m_GroupTempDoc->OpenDocumentFile(NULL); if (pDoc==NULL) { assert(false); AfxMessageBox("组合图元无法打开!"); return; } pDoc->OpenGrpFile(filepath); } void CToolBoxList::OnUpdateGroupModify(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 POSITION pos =GetFirstSelectedItemPosition(); if (pos!=NULL) { pCmdUI->Enable(true); } else pCmdUI->Enable(false); }
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值