工作中总结的编程小技巧[转]

技巧一:int-->bool要注意的!
bool a=255;
bool b= 254;
理论上应该认为a==b,但是根据编译器的不同,结果可能完全不一样
所以更好的写法是
char ch =1; 或者0
bool a = (0 != atoi(ch));

技巧二:将std::string转int,double
先利用c_str()转成C string,再用atoi()与atof()

技巧三:const_cast操作符可以强置去除const指针的const属性

技巧四:CString读取文本的每一行
CStdioFile LogIDFile;
char* pFileName = "Log.txt ";
CString strtemp;
if( LogIDFile.Open( pFileName, CFile::modeRead ))
{
LogIDFile.SeekToBegin();
LogIDFile.ReadString(strtemp);
}
LogIDFile.Close();

技巧五:win32下给任何一个句柄添加消息处理函数:
为了简单假如在About对话框里贴了一个 IDC_STATIC_1。在IDC_STATIC_1上挂了一个 IDB_BITMAP1。
IDC_STATIC_1 控件属性,“通知”=Notify设为TRUE。
有了上面的假设后,就可以用下面的程序控制picture的全部行为。

LONG OldPicProc;
LRESULT CALLBACK PicProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch( message )
    {
        case WM_RBUTTONDOWN:
            {
                WORD xPos = LOWORD(lParam);
                WORD yPos = HIWORD(lParam);
            }
            break;
    }
  return CallWindowProc((WNDPROC)OldPicProc, hWnd, message, wParam, lParam);
}


LRESULT CALLBACK About( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch( message )
    {
        case WM_INITDIALOG:
            {
              HWND hWnd = GetDlgItem(hDlg, IDC_STATIC_1);
                OldPicProc = SetWindowLong(hWnd, GWL_WNDPROC, (LONG)PicProc);
            }
       
            return TRUE;

        case WM_COMMAND:
            if( LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL )
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }
    return FALSE;
}

技巧六:屏幕坐标转三维坐标(反变换的过程)
void GetOGLPos(int x, int y, double * posX, double * posY, double * posZ)
{
GLint viewport[4];
GLdouble projection[16];
GLfloat winX, winY, winZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, posX, posY, posZ);
}

技巧七 宽字符字符串转多字节字符串
//===方法1
TCHAR tcScriptName[MAX_PATH];
tcScriptName = "filename";
size_t i;
  char tmp[MAX_PATH];
  mbstate_t       mbstate;

  // Reset to initial shift state
  ::memset((void*)&mbstate, 0, sizeof(mbstate));

  const wchar_t* wt = tcScriptName;
  wcsrtombs_s(&i, tmp, MAX_PATH, &wt, MAX_PATH,&mbstate);
//===方法2
char * input = null;
wchar_t wc_input[MAX_USERNAME_LEN] = L"test";

  int nLen = WideCharToMultiByte( CP_ACP, 0, wc_input, -1, NULL, 0, NULL, NULL );
  int i = wcslen(wc_input)*sizeof(wchar_t);
  input = (char*)realloc(input,i+1);
  if (!input)
  {
   free(input);
   input = (char*)malloc(i + 1);
  }
  WideCharToMultiByte( CP_ACP, 0, wc_input, -1, input, nLen, NULL, NULL );

技巧八 获得界面上所有控件
CWnd *pWnd = this->GetWindow( GW_CHILD );
while( pWnd )
{
  if( pWnd->IsKindOf( RUNTIME_CLASS(CEdit) ) )
      pWnd->ShowWindow(SW_SHOW);
       pWnd = pWnd->GetWindow( GW_HWNDNEXT );
}

技巧九 判断基类指针指向的派生类类型

方法一:
class A
{
public:
    virtual int get_type_id()
    {
        return 0;   
    }
};

class B: public A
{
public:
    int get_type_id()
    {
        return 1;
    }
};

方法二:
用RTTI
class A
{
public:
    virtual ~A(){} //必须

};

class B: public A
{
};

int main()
{
    A * p = new B;
        bool p_is_B = (typeid(B) == typeid(*p));
    std::cout<<p_is_B<<std::endl;

        p_is_B = (dynamic_cast<B*>(p) != 0);
    std::cout<<p_is_B<<std::endl;
}

技巧十 窗口ID,句柄,指针三者相互转换函数
ID--HANDLE--HWND三者之间的互相转换
===============
id->句柄
hWnd = ::GetDlgItem(hParentWnd,id);
===============
id->指针
CWnd::GetDlgItem();
===============
句柄->id
id = GetWindowLong(hWnd,GWL_ID);
===============
句柄->指针
CWnd *pWnd=CWnd::FromHandle(hWnd);
===============
指针->ID
id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);
=================
指针->句柄
hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;

(不断增加ing)

你可能感兴趣的:(编程,工作,input,callback,float,Matrix)