木感想

常常思索 没有结果 来来去去 依稀记得

导航

Gdiplus 状态处理

在使用Gdi+的时候,我们需要处理各种状态。对于不同的状态都要在自己的掌握中。
下面的程序是经受过严格考验的,能够帮助你提示错误或者进行有效的对象检测。
例如:

     Bitmap* pBitmap;
     if(GoodGdiPointer(_T("Some method"), pBitmap))

      {

            SIT_DELETE(pBitmap);

      }

      else

      {

            SGTRACE_ERR(_T("Some method"),  OutOfMemory);

      }
这样的程序,能够安全的使用pBitmap对象。主要的目标就是对一个Gdi+对象的各种
使用,做到最安全,并能对于整个处理过程良好的掌握。 
这套工作过程,在我已经设计的项目中得到广泛的采用,应该来说是高效可靠的。
我们也能够增加更多的类似处理过程。

// Copyright (C) Sincere Corporation
//trace of gdi status
//you can call SGTRACE_MSG to trace information.
//call SGTRACE_ERR, SGTRACE_ERR_MSGBOX to trace gdi error.
#pragma once

// the trace error information
#define SIT_STATUS_0 _T("not any error!")
#define SIT_STATUS_1 _T("GenericError!")
#define SIT_STATUS_2 _T("InvalidParameter!")
#define SIT_STATUS_3 _T("OutOfMemory!")
#define SIT_STATUS_4 _T("ObjectBusy!")
#define SIT_STATUS_5 _T("InsufficientBuffer!")
#define SIT_STATUS_6 _T("NotImplemented!")
#define SIT_STATUS_7 _T("Win32Error!")
#define SIT_STATUS_8 _T("WrongState!")
#define SIT_STATUS_9 _T("Aborted!")
#define SIT_STATUS_10 _T("FileNotFound!")
#define SIT_STATUS_11 _T("ValueOverflow!")
#define SIT_STATUS_12 _T("AccessDenied!")
#define SIT_STATUS_13 _T("UnknownImageFormat!")
#define SIT_STATUS_14 _T("FontFamilyNotFound!")
#define SIT_STATUS_15 _T("FontStyleNotFound!")
#define SIT_STATUS_16 _T("NotTrueTypeFont!")
#define SIT_STATUS_17 _T("UnsupportedGdiplusVersion!")
#define SIT_STATUS_18 _T("GdiplusNotInitialized!")
#define SIT_STATUS_19 _T("PropertyNotFound!")
#define SIT_STATUS_20 _T("PropertyNotSupported!")
#define SIT_STATUS_21 _T("ProfileNotFound!")

//const mean you can't written variable
//static mean that you can used variable based this file.
static const CString SIT_Status_Trace_Msg[22] = {
 _T("not any error!"),        // Status 0
 _T("GenericError! an error on the method call."), // Status 1
 _T("InvalidParameter!"),       // Status 2
 _T("OutOfMemory!"),         // Status 3
 _T("ObjectBusy!"),         // Status 4
 _T("InsufficientBuffer!"),       // Status 5
 _T("NotImplemented!"),        // Status 6
 _T("Win32Error!"),         // Status 7
 _T("WrongState!"),         // Status 8
 _T("Aborted!"),          // Status 9
 _T("FileNotFound!"),        // Status 10
 _T("ValueOverflow!"),        // Status 11
 _T("AccessDenied!"),        // Status 12
 _T("UnknownImageFormat!"),       // Status 13
 _T("FontFamilyNotFound!"),       // Status 14
 _T("FontStyleNotFound!"),       // Status 15
 _T("NotTrueTypeFont!"),        // Status 16
 _T("UnsupportedGdiplusVersion!"),     // Status 17
 _T("GdiplusNotInitialized!"),      // Status 18
 _T("PropertyNotFound!"),       // Status 19
 _T("PropertyNotSupported!"),      // Status 20
 _T("ProfileNotFound!"),        // Status 21
};

// 状态询问

#define StatusOk(s) ((Status)(s)==0)

#define StatusBad(s) ((Status)(s)>0)


// 显示Gdi函数工作状态的调试信息以及弹出式对话框。
inline Status WINAPI SitGdiTrace(const TCHAR* strFile, DWORD dwLine, Status s, const TCHAR* msg, BOOL bBox)
{
 CString status;
 CString strLog;

 //set strfile info of mouse double clicked.
 //this formatted is needed.
 status.Format(_T("%s"), strFile);
 status.AppendFormat(_T("(%d) : "), dwLine);
 status.AppendFormat(_T("%s\t"), msg);
 
 if(StatusOk(s))
 {
  status.AppendFormat(_T("\n"));
  ATLTRACE(status); return s;
 }

#ifdef SIT_LOG_STATUS
 strLog.Format(_T("%s"), status);
 strLog.AppendFormat(_T("错误类型 : %s"), SIT_Status_Trace_Msg[s]);

 SIT_LOG(strLog);
#endif

 status.AppendFormat(_T("错误类型 : %s\n"), SIT_Status_Trace_Msg[s]);

 ATLTRACE(status);

 if(bBox)
 {
  MessageBox(NULL, status, _T("调试信息"), MB_OK|MB_ICONINFORMATION);
 }

 return s;
}

inline CString STGetStatusMessage(Status s)
{
 CString status;

#if defined(DEBUG) | defined(_DEBUG)
 status.Format(_T("%s"), __FILE__);
 status.AppendFormat(_T("(%d) : "), __LINE__);
#endif
 status.AppendFormat(_T("(%s)"), SIT_Status_Trace_Msg[s]);

 return status;
}


//debug have not trace info.
#if defined(DEBUG) | defined(_DEBUG)
#define SGTRACE_MSG(str)   SitGdiTrace( __FILE__, (DWORD)__LINE__, Ok, str, FALSE);
#define SGTRACE_ERR(str, s)   SitGdiTrace( __FILE__, (DWORD)__LINE__, s, str, FALSE);
#define SGTRACE_ERR_MSGBOX(str,s) SitGdiTrace( __FILE__, (DWORD)__LINE__, s, str, TRUE);
#else
#define SGTRACE_MSG(str)   (0L)
#define SGTRACE_ERR(str, s)   (s)
#define SGTRACE_ERR_MSGBOX(str,s) (s)
#endif

//Test gdiplus object pointer is valid.
#define TestGdiPointer(s, x) { \
 if(!(x)) \
  return SGTRACE_ERR(s, OutOfMemory);  \
 Status status;\
 if(StatusBad(status = (x)->GetLastStatus())) \
 { \
  delete (x); (x) = NULL;\
  return SGTRACE_ERR(s, status); \
 }\
}

#define TestGdiStatus(s, x) { \
 if(StatusBad(x)) \
  return SGTRACE_ERR(s, x); \
}

#define TestPointer(s, x) { \
 if(!(x)) \
  return SGTRACE_ERR(s, OutOfMemory); \
}

#define GoodGdiPointer(x) ((x) && StatusOk((x)->GetLastStatus()))


#define Sit_Gdi_Startup(token)\
 if((token) == 0) \
 {\
  GdiplusStartupInput gdiplusStartupInput; \
  GdiplusStartup(&(token),&gdiplusStartupInput, NULL); \
 }

#define Sit_Gdi_End(token)\
 if((token))\
 {\
  GdiplusShutdown((token));\
  (token) = 0;\
 }

#define Sit_Gdi_Reset(token) Sit_Gdi_End((token)); Sit_Gdi_Startup((token));

#define SIT_DELETE_GDI(p) if(p){ GdipFree(p); (p) = NULL; }


posted on 2005-03-23 09:46  跌跌撞撞  阅读(2514)  评论(0)    收藏  举报