vc相关笔记

1、隐藏鼠标

int i = ShowCursor(FALSE);
 for ( i; i >= 0 ;i-- )
 {
  ShowCursor(FALSE);
 }

2、显示鼠标

 int i = ShowCursor(TRUE);
 for ( i;i<= 0;i++ )
 {
  ShowCursor(TRUE);
 }

3、在Picture Control上显示图片

(1)先写一个类

//.h

#pragma once
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
// CImagePrieviewStatic
class CImagePreviewStatic : public CStatic
{
 DECLARE_DYNAMIC(CImagePreviewStatic)
public:
     CImagePreviewStatic();
 virtual   ~CImagePreviewStatic();

 virtual BOOL Create();
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

 void   SetFilename(LPCTSTR szFilename);

protected:
 WCHAR   m_wsFilename[_MAX_PATH];
 Image   *m_img;
 Graphics  *m_graphics;

 DECLARE_MESSAGE_MAP()
}; 

//.cpp

#include "stdafx.h"
#include "ImagePreviewStatic.h"

// CImagePrieviewStatic
IMPLEMENT_DYNAMIC(CImagePreviewStatic, CStatic)

CImagePreviewStatic::CImagePreviewStatic() : CStatic()
{
 m_img = (Image *) NULL;
 m_graphics = (Graphics *) NULL;
}

CImagePreviewStatic::~CImagePreviewStatic()
{
 //modified by yangjiaxun @ 200701226
 if( m_img )
  delete m_img;
 if( m_graphics )
  delete m_graphics;
}

BOOL CImagePreviewStatic::Create()
{
 if (GetSafeHwnd() != HWND(NULL))
 {
  if (m_img != NULL)
  {
   delete m_img;
   m_img = (Image *) NULL;
  }
  if (m_graphics != NULL)
  {
   delete m_graphics;
   m_graphics = (Graphics *) NULL;
  }

  m_img = new Image(m_wsFilename);
  m_graphics = new Graphics(GetSafeHwnd());
  return TRUE;
 }

 return FALSE;
}

void CImagePreviewStatic::SetFilename(LPCTSTR szFilename)
{
#ifndef _UNICODE
 USES_CONVERSION;
#endif

 ASSERT(szFilename);
 ASSERT(AfxIsValidString(szFilename));

 TRACE("%s\n", szFilename);

#ifndef _UNICODE
 wcscpy(m_wsFilename, A2W(szFilename));
#else
 wcscpy_s(m_wsFilename, szFilename);
#endif

 delete m_img;
 m_img = new Image(m_wsFilename, FALSE);
 Invalidate();
}

void CImagePreviewStatic::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
{
 Unit  units;
 CRect rect;

 if (m_img != NULL)
 {
  GetClientRect(&rect);

  RectF destRect(REAL(rect.left), REAL(rect.top), REAL(rect.Width()), REAL(rect.Height())),
   srcRect;
  m_img->GetBounds(&srcRect, &units);
  m_graphics->DrawImage(m_img, destRect, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, UnitPixel, NULL);
 }
}

BEGIN_MESSAGE_MAP(CImagePreviewStatic, CStatic)
END_MESSAGE_MAP()

//在另外的.cpp中进行调用

为该Picture Control添加控件变量 CImagePreviewStatic m_adPic;

 m_adPic.Create();
 m_adPic.SetFilename(theApp.g_szDefaultADPic);

4、根据屏幕分辨率改变窗体控件的大小

void xx::StretchControl(UINT uID)
{
 CRect rcControl;
 CRect rcFrame;
 GetDlgItem( IDC_FRAME )->GetWindowRect( rcFrame );
 ScreenToClient( rcFrame );
 GetDlgItem( uID )->GetWindowRect( rcControl );
 ScreenToClient( rcControl );
 long topRate, leftRate, heightRate, widthRate;
 topRate  = rcControl.top      * GetSystemMetrics( SM_CYSCREEN ) / rcFrame.Height();
 leftRate = rcControl.left     * GetSystemMetrics( SM_CXSCREEN ) / rcFrame.Width();
 heightRate = rcControl.Height() * GetSystemMetrics( SM_CYSCREEN ) / rcFrame.Height();
 widthRate = rcControl.Width()  * GetSystemMetrics( SM_CXSCREEN ) / rcFrame.Width();
 GetDlgItem( uID )->MoveWindow( leftRate, topRate, widthRate, heightRate );
 Invalidate();
}

void xx::ShiftControl(UINT uID)
{
 CRect rcControl;
 CRect rcFrame;

 GetDlgItem( IDC_FRAME )->GetWindowRect( rcFrame );
 ScreenToClient( rcFrame );

 GetDlgItem( uID )->GetWindowRect( rcControl );
 ScreenToClient( rcControl );

 long topRate, leftRate;
 topRate  = rcControl.top  * GetSystemMetrics( SM_CYSCREEN ) / rcFrame.Height();
 leftRate = rcControl.left * GetSystemMetrics( SM_CXSCREEN ) / rcFrame.Width();

 GetDlgItem( uID )->MoveWindow( leftRate, topRate, rcControl.Width(), rcControl.Height() );
 Invalidate();
}

5、在listctrl中显示图片

(1)addpicture

void xx::addPicture(void)
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);

 // validate image directory
 // show hour glass cursor
 BeginWaitCursor();

 // get the names of bitmap files
 if ( !GetImageFileNames() )
 {
  LOG_OUTPUT_WARN(_T("image目录下没有图片。"));
  EndWaitCursor();
  return;
 }
 // draw thumbnail images in list control
 drawPicture();
// set focus and select the first thumbnail in the list control
 //m_ctrList.SetFocus();
 m_ctrList.SetItemState(0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); 

 CRect crt;
 int x = 95;
 int y = 120;
 m_ctrList.GetClientRect(&crt);
 x = (crt.Width() - 20)/3;
 y = crt.Height()/3;
 m_ctrList.SetIconSpacing(x,y);
 
 EndWaitCursor();
}

(2)GetImageFileNames

BOOL xx::GetImageFileNames()
{
 CString strExt;
 CString strName;
 CString strPattern;
 BOOL bRC = TRUE;

 HANDLE     hFind = NULL;
 WIN32_FIND_DATA   FindFileData;
 std::vector<CString> VectorImageNames;

 if ( theApp.g_szPicFilePath[theApp.g_szPicFilePath.GetLength() - 1] == TCHAR(''\\'') ) 
  strPattern.Format( TEXT("%s*.*"), theApp.g_szPicFilePath );
 else
  strPattern.Format( TEXT("%s\\*.*"), theApp.g_szPicFilePath );

 hFind = ::FindFirstFile(strPattern, &FindFileData); // strat search 
 if (hFind == INVALID_HANDLE_VALUE)
 {
  LPVOID  msg;
  ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
   NULL,
   GetLastError(),
   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   (LPTSTR)&msg,
   0,
   NULL);
  //  MessageBox((LPTSTR)msg, CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP);
  ::LocalFree(msg);
  return FALSE;
 }

 // filter off the system files and directories
 if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  &&
  !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)     &&
  !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)     &&
  !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY))
 {    
  // test file extension
  strName = FindFileData.cFileName;
  strExt = strName.Right(3);

  if ( strExt.CompareNoCase( TEXT("jpg") ) == 0 )
  {
   // save the image file name
   VectorImageNames.push_back(strName);
  }
 } 

 // loop through to add all of them to our vector 
 while (bRC)
 {
  bRC = ::FindNextFile(hFind, &FindFileData);
  if (bRC)
  {
   // filter off the system files and directories
   if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  &&
    !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)     &&
    !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)     &&
    !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY))
   {
    // test file extension
    strName = FindFileData.cFileName;
    strExt = strName.Right(3);

    if ( strExt.CompareNoCase( TEXT("jpg") ) == 0)
    {
     // save the image file name
     //strName = theApp.g_szPicFilePath + strName;
     VectorImageNames.push_back(strName);
    }
   }
  } 
  else
  {
   DWORD err = ::GetLastError();
   if (err !=  ERROR_NO_MORE_FILES)
   {
    LPVOID msg;
    ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
     NULL, err,
     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
     (LPTSTR)&msg, 0, NULL);
    //MessageBox((LPTSTR)msg, CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP);
    ::LocalFree(msg);
    ::FindClose(hFind);
    return FALSE;
   }
  }
 } // end of while loop

 // close the search handle
 ::FindClose(hFind);

 // update the names, if any
 if ( !VectorImageNames.empty() )
 {
  // reset the image name vector
  m_VectorImageNames.clear();
  m_VectorImageNames = VectorImageNames;
  return TRUE;
 }

 return FALSE;
}

(3)drawPicture

void xx::drawPicture()
{
 CBitmap*    pImage = NULL;
 HBITMAP  hBmp = NULL;
 POINT  pt;
 CString  strPath;
 int   i;

 // no images
 if (m_VectorImageNames.empty())
  return;

 // set the length of the space between thumbnails
 // you can also calculate and set it based on the length of your list control
 int nGap = 6;

// reset our image list
 for (i = 0; i<m_ImageListThumb.GetImageCount(); i++)
  m_ImageListThumb.Remove(i); 

 // remove all items from list view
 if (m_ctrList.GetItemCount() != 0)
  m_ctrList.DeleteAllItems();
 //add 20070809
 // set the size of the image list
 utility uClass;
 int m_iSid;
 m_iSid = theApp.getSidByMenuSname(theApp.m_strMenuSname);
 if (m_iSid == -1)
 {
  return;
 }
 theApp.m_menuInfoVec = uClass.getMenuInfoBySid(m_iSid);
 m_ImageListThumb.SetImageCount((UINT)theApp.m_menuInfoVec.size());//--modify 20070809
 i = 0;

 CRect crt;
 int iWidth = 95;
 int iHeight = 120;
 m_ctrList.GetClientRect(&crt);
 iWidth = (crt.Width() - 20)/3;
 iHeight = (crt.Height() - 20)/3 - 15;

 for (UINT k = 0;k < (UINT)theApp.m_menuInfoVec.size();k++)
 {
  CString strMid;
  CString strJpg = _T(".jpg");
  CString strMj;
  strMid.Format(L"%d",theApp.m_menuInfoVec[k].Mid);
  strMj = strMid + strJpg;
  std::vector<CString>::iterator iter;
  iter = find(m_VectorImageNames.begin(),m_VectorImageNames.end(),strMj);
  if(iter == m_VectorImageNames.end())
  {
   // load the bitmap
   strPath.Format( TEXT("%s\\%s"), theApp.g_szPicFilePath, _T("default.jpg") );

   USES_CONVERSION;
   //Bitmap img( A2W(strPath) );
   Bitmap img( strPath);
   //Bitmap* pThumbnail = static_cast<Bitmap*>(img.GetThumbnailImage(100, 75, NULL, NULL));
   Bitmap* pThumbnail = static_cast<Bitmap*>(img.GetThumbnailImage(iWidth, iHeight, NULL, NULL));

   // attach the thumbnail bitmap handle to an CBitmap object
   pThumbnail->GetHBITMAP(NULL, &hBmp);
   pImage = new CBitmap();  
   pImage->Attach(hBmp);

   // add bitmap to our image list
   m_ImageListThumb.Replace(k, pImage, NULL);

   // put item to display
   // set the image file name as item text
   //m_ctrList.InsertItem(i, m_VectorImageNames[i], i);
   CString strXS = _T("");
   CString strMname = theApp.m_menuInfoVec[k].Mname;
   CString strMprice = theApp.m_menuInfoVec[k].Mprice;
   CString strMmeasure = theApp.m_menuInfoVec[k].Mmeasure;
   LPCTSTR strTemp = LPCTSTR(strMname);
   strMname = strTemp;
   strTemp = LPCTSTR(strMprice);
   strMprice = strTemp;   
   strTemp = LPCTSTR(strMmeasure);
   strMmeasure = strTemp;
   strXS = (strMname + _T("\n") + _T("(") + strMprice + _T("/") + strMmeasure + _T(")"));
   m_ctrList.InsertItem(k,strXS,k);
   // get current item position 
   m_ctrList.GetItemPosition(k, &pt); 

   // shift the thumbnail to desired position
   pt.x = nGap + k*(75 + nGap);
   //m_ctrList.SetItemPosition(k, pt);//delete by wupeng 2007.08.15 for reslove picture''s positon
   //i++;

   delete pImage;
   delete pThumbnail;

  }
  else{
   // load the bitmap
   strPath.Format( TEXT("%s\\%s"), theApp.g_szPicFilePath,*iter );

   USES_CONVERSION;
   //Bitmap img( A2W(strPath) );
   Bitmap img( strPath);
   //Bitmap* pThumbnail = static_cast<Bitmap*>(img.GetThumbnailImage(100, 75, NULL, NULL));
   Bitmap* pThumbnail = static_cast<Bitmap*>(img.GetThumbnailImage(iWidth, iHeight, NULL, NULL));

   // attach the thumbnail bitmap handle to an CBitmap object
   pThumbnail->GetHBITMAP(NULL, &hBmp);
   pImage = new CBitmap();  
   pImage->Attach(hBmp);

   // add bitmap to our image list
   m_ImageListThumb.Replace(k, pImage, NULL);

   // put item to display
   // set the image file name as item text
   //m_ctrList.InsertItem(i, m_VectorImageNames[i], i);
   CString strXS = _T("");
   CString strMname = theApp.m_menuInfoVec[k].Mname;
   CString strMprice = theApp.m_menuInfoVec[k].Mprice;
   CString strMmeasure = theApp.m_menuInfoVec[k].Mmeasure;
   LPCTSTR strTemp = LPCTSTR(strMname);
   strMname = strTemp;
   strTemp = LPCTSTR(strMprice);
   strMprice = strTemp;   
   strTemp = LPCTSTR(strMmeasure);
   strMmeasure = strTemp;
   strXS = (strMname + _T("\n") + _T("(") + strMprice + _T("/") + strMmeasure + _T(")"));
   m_ctrList.InsertItem(k,strXS,

k);
   // get current item position 
   m_ctrList.GetItemPosition(k, &pt); 

   // shift the thumbnail to desired position
   pt.x = nGap + k*(75 + nGap);
   //m_ctrList.SetItemPosition(k, pt);
   /*i++;*/

   delete pImage;
   delete pThumbnail;
  }
 }
 //end add

 // let''s show the new thumbnails
 m_ctrList.SetRedraw();
}

posted on 2008-09-10 18:32 wrh 阅读(473) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(19)

随笔档案

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜