VS2005 C++ 手机开发杂记(下)

来源:互联网 发布:cdn nginx缓存原理 编辑:程序博客网 时间:2024/06/10 14:12

书接上文。。。

 

———————————————————————————- 
VS2005 C++ MFC  智能设备应用程序,怎样添加事件 
———————————————————————————- 
在“类视图”点击右键,打开属性页。属性页的最上一排按钮中有添加“事件”、“消息”等按钮。

———————————————————————————- 
只允许一个应用程序实例运行 
———————————————————————————- 
// 创建一个名为“No Previous Instance”的命名互斥对象 
if (!CreateMutex(NULL,TRUE,"No Previous Instance!"))        
{        
        MessageBox(NULL,"创建Mutex失败!","NoPrev",MB_OK|MB_SYSTEMMODAL);        
        return FALSE;        

// 是否已有一个实例在运行 
if (GetLastError()==ERROR_ALREADY_EXISTS)        
{        
        MessageBox(NULL,"已有NoPrev的一个实例在运行, 当前实例将被终止!",            
                "NoPrev",MB_OK|MB_SYSTEMMODAL);        
        return FALSE;        
}

———————————————————————————- 
捕捉手机 Home 键 (虽然能捕捉到,但是好像最后还是执行了返回桌面操作) 
———————————————————————————- 
1. 注册热键

BOOL CMiniBlogClientDlg::OnInitDialog() 

    …

    // 处理 VK_HOME 
    BYTE appkey = SHGetAppKeyAssoc(_T("MiniBlogClient.exe")); 
    ::RegisterHotKey ( m_hWnd, appkey, MOD_WIN, VK_THOME);

    … 
}

2. 重写 PreTranslateMessage 函数

BOOL CMiniBlogClientDlg::PreTranslateMessage(MSG* pMsg) 

    // TODO: 在此添加专用代码和/或调用基类 
    if( pMsg->message == WM_HOTKEY ) 
    { 
        SetForegroundWindow();

        return TRUE; 
    }

    return CDialog::PreTranslateMessage(pMsg); 
}

———————————————————————————- 
为什么按 HOME 键后,MFC  智能设备应用程序的窗口在“任务管理器(CeleTask.exe)”里找不到 
———————————————————————————- 
在窗口编辑器里,将窗口的 style 属性修改为 Overlapped

———————————————————————————- 
屏蔽 KeyDown 事件 
———————————————————————————- 
BOOL CBlogEdit::PreTranslateMessage(MSG* pMsg) 

    // TODO: 在此添加专用代码和/或调用基类 
    if ( pMsg->message == WM_KEYDOWN ) 
    {       
        return TRUE; 
    }

    return CEdit::PreTranslateMessage(pMsg); 
}

 

———————————————————————————- 
关闭输入法 
———————————————————————————- 
HIMC hIMC = ImmGetContext( this->GetSafeHwnd() ); 
ImmSetOpenStatus ( hIMC, FALSE );

———————————————————————————- 
在 CEdit 的扩展类中添加翻页功能 
———————————————————————————- 
1. 控件获得焦点时,屏蔽输入法

void CBlogEdit::OnSetFocus(CWnd* pOldWnd) 

    CEdit::OnSetFocus(pOldWnd);

    // 关闭输入法 
    HIMC hIMC = ImmGetContext( this->GetSafeHwnd() ); 
    ImmSetOpenStatus ( hIMC, FALSE ); 
}

2. 映射 WM_CHAR 消息到 OnChar ()

void CBlogEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 

    // nChar 的值在不同手机上可能有所不同 
    switch ( nChar ) 
    { 
        // Up 
        case 50: 
        { 
            this->LineScroll ( -1 );

            break; 
        }

        // Down 
        case 56: 
        { 
            this->LineScroll ( 1 );

            break; 
        }

        // PageUp 
        case 52: 
        { 
            this->LineScroll ( -11 );

            break; 
        }

        // PageDown 
        case 54: 
        { 
            this->LineScroll ( 11 );

            break; 
        } 
    }

    // CEdit::OnChar(nChar, nRepCnt, nFlags); 
}

 

———————————————————————————- 
自己构造时间 
———————————————————————————- 
#include <windows.h> 
#include <atltime.h> 
#include <stdio.h> 
#include <string.h> 
#include <time.h>

int _tmain(int argc, _TCHAR* argv[]) 

    char           buff[80]; 
    __time64_t result; 
    CTime        t(2008,4,28,22,18,39);

    CString s = t.Format( _T("%A, %B %d, %Y") );

    struct tm when; 
    t.GetLocalTm(&when);

    when.tm_hour = when.tm_hour + 8;

    if( (result = mktime( &when )) != (time_t)-1 ) 
    { 
        asctime_s( buff, sizeof(buff), &when ); 
        printf( "the time will be %s/n", buff ); 
    } 
    else 
    { 
        printf( "mktime failed" ); 
    } 
}

———————————————————————————- 
C++ 读写文件 
———————————————————————————- 
#include <fstream> 
using namespace std;

struct AuthInfo auth_info; // AuthInfo 是自定义的 struct 
string susername, spassword;

// 写 
ZeroMemory ( &auth_info, sizeof ( auth_info ) );

susername = "tanggaowei@gmail.com"; 
spassword = "000000";

memcpy(auth_info.username, susername.c_str(), susername.length()); 
memcpy(auth_info.password, spassword.c_str(), spassword.length());

ofstream fout("mbc.dat", ios::binary);

fout.write((char *)(&auth_info), sizeof(auth_info));

fout.close();

// 读 
ZeroMemory ( &auth_info, sizeof ( auth_info ) );

ifstream fin ( "mbc.dat", ios::binary );

fin.read((char *)(&auth_info), sizeof(auth_info));

susername = auth_info.username; 
spassword = auth_info.password;

ZeroMemory ( auth_info.username, 100 ); // AuthInfo.username[100] 
ZeroMemory ( auth_info.password, 50 );  // AuthInfo.password[50]

memcpy(auth_info.username, susername.c_str(), susername.length()); 
memcpy(auth_info.password, spassword.c_str(), spassword.length());  

fin.close();

 

———————————————————————————- 
获取应用程序路径 
———————————————————————————-

CString CMiniBlogClientApp::GetModulePath(void) 

    CString sPath = _T(""); 
    int max_len = 256; 
    LPTSTR lpszPath = new TCHAR[max_len];

    ::GetModuleFileName ( AfxGetInstanceHandle(), lpszPath, max_len );

    sPath = lpszPath; 
    sPath.Replace ( _T("/"), _T("//") ); 
    sPath = sPath.Left( sPath.ReverseFind ( _T(’//’) ) ); 
    sPath += "//";

    delete lpszPath; 
    lpszPath = NULL;

    return sPath; 
}

———————————————————————————- 
无法解析的外部符号 
———————————————————————————- 
1. lib 文件未引入。 
2. 类方法的实现未加类标识,如 CTest::Connect(void) 写成了 Connect(void) 
3. 找不到 *.obj 文件。

原创粉丝点击