wince应用开发的一些小功能(备忘)

 

整理下最近开发时用到得一些小功能,在网上都能找得到,整理下免得以后再找麻烦。

持续添加

一、自动隐藏鼠标

 

//自动计时,超过5次计时隐藏鼠标

void CMyDlg::OnTimer(UINT_PTR nIDEvent)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

switch (nIDEvent)

{

case TIMER_HIDECURSOR:

{

m_nTimeCount += 1;

if (m_nTimeCount >= 5)

{

int i = ShowCursor(FALSE);                //隐藏光标

TRACE(_T("ShowCursor(FALSE)%d/n"),i);

for (int j=0; j<=i; j++)

{

ShowCursor(FALSE);                //隐藏光标

}

m_nTimeCount = 0;

}

}

break;

default:

break;

}

CDialog::OnTimer(nIDEvent);

}

 

 

//在MouseMove时清空隐藏鼠标计数,并显示

void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

m_nTimeCount = 0;    // 隐藏鼠标事件累积清零

int i = ShowCursor(TRUE);    // 显示光标

for(int j=0;j>i;j--)

{

ShowCursor(TRUE);

}

CDialog::OnMouseMove(nFlags, point);

}

 

 

二、设置开机自动启动

 

//如果希望取消wince桌面显示,将Launch99改为Launch50

BOOL SetAutoRun()

{

CString szKey(_T("init"));

HKEY hKey = 0;

HKEY hModuleKey;            // Module key handle

//LSTATUS rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, &hkeyFilter);

DWORD dwType, dwValueSize;

char* data = (char*)malloc(30);

memset(data, 0, 30);

LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ, &hKey );

 

if (lResult == ERROR_SUCCESS)

{

TCHAR szLaunch[PATH_MAX] = {0};

GetModuleFileName(0, szLaunch, PATH_MAX);

LSTATUS rc = RegSetValueEx(

hKey,                   // Handle of an open key

_T("Launch99"),                // Address of subkey name

(DWORD) 0,              // Reserved field

REG_SZ,                 // Type of the key field

(PBYTE)szLaunch,          // Value for the field

PATH_MAX);            // Size of the field buffer

 

if (rc != ERROR_SUCCESS)

{

//AfxMessageBox(_T("Set path Value failed/r/n"));

return FALSE;

}

 

DWORD dwValue = 0x0014;

rc = RegSetValueEx(

hKey,                   // Handle of an open key

_T("Depend99"),                // Address of subkey name

(DWORD) 0,              // Reserved field

REG_BINARY,                 // Type of the key field

(PBYTE)(&dwValue),          // Value for the field

sizeof(DWORD));            // Size of the field buffer

 

if (rc != ERROR_SUCCESS)

{

//AfxMessageBox(_T("Set depend Value failed/r/n"));

return FALSE;

}

RegFlushKey(HKEY_LOCAL_MACHINE);

RegCloseKey(hKey);

 

return TRUE;

}

else

{

return FALSE;

}

}

 

 

三、设置系统分表率(需要系统支持)

 

BOOL SetSystemMetrics(DWORD dwWidth, DWORD dwHeight)

{

DEVMODE dm;

 

memset((char*)&dm, 0, sizeof(DEVMODE));   

dm.dmSize = sizeof(DEVMODE);

dm.dmPelsWidth = dwWidth;//1920

dm.dmPelsHeight = dwHeight;//1080

dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;

if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(NULL, &dm, NULL, 0, NULL))

{

return TRUE;

}

else

{

return FALSE;

}

}

 

 

四、读取本地IP地址(只能读IP,子网掩码/网关等不能读,也不能设置,要设置需要修改注册表信息并重启网卡/设备)

 

CString GetLocalIP()

{

HOSTENT *LocalAddress;

char *Buff;

TCHAR *wBuff;

CString strReturn = _T("");

 

//创建新的缓冲区

Buff = new char[256];

wBuff = new TCHAR[256];

//置空缓冲区

memset(Buff, '/0', 256);

memset(wBuff, TEXT('/0'), 256*sizeof(TCHAR));

//得到本地计算机名

if (gethostname(Buff, 256) == 0)

{

//转换成双字节字符串

size_t convertedChars = 0;

mbstowcs_s(&convertedChars, wBuff, 256*sizeof(TCHAR),Buff, 256);

//得到本地地址

LocalAddress = gethostbyname(Buff);

//置空buff

memset(Buff, '/0', 256);

//组合本地IP地址

sprintf_s(Buff, 256, "%d.%d.%d.%d/0", LocalAddress->h_addr_list[0][0] & 0xFF,

LocalAddress->h_addr_list[0][1] & 0x00FF, LocalAddress->h_addr_list[0][2] & 0x0000FF, LocalAddress->h_addr_list[0][3] & 0x000000FF);

//置空wBuff

memset(wBuff, TEXT('/0'), 256*sizeof(TCHAR));

//转换成双字节字符串

mbstowcs_s(&convertedChars, wBuff, 256*sizeof(TCHAR), Buff, 256);

//设置返回值

strReturn = wBuff;

}

else

{

}

 

//释放Buff缓冲区

delete[] Buff;

Buff = NULL;

//释放wBuff缓冲区

delete[] wBuff;

wBuff = NULL;

return strReturn;

}

 

五、通过注册表来实现读取/设置网卡相关信息(IP,MASK, GATEWAY,DNS等)

BOOL SetIpAddress(CString strIp,CString strMask,CString strGateWay, CString strDNS)
{
 //获得网卡的设备名
 WCHAR Names[50];
 DWORD bytes;
 HANDLE m_hFileHandle = CreateFile(_T("NDS0:"), 0, 0, NULL,
  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  (HANDLE) INVALID_HANDLE_VALUE);

 if( m_hFileHandle == INVALID_HANDLE_VALUE )
 {
  return FALSE;
 }

 // Get list of adapter names
 if (!DeviceIoControl(m_hFileHandle,
  IOCTL_NDIS_GET_ADAPTER_NAMES,
  NULL,0,
  Names,MAX_PATH,&bytes,NULL))
 {
  return FALSE;
 }
 DWORD len = wcslen(Names);
 Names[len] = 0;
 Names[len+1] = 0;
 CString strKeyName;
 strKeyName.Format(_T("Comm//%s//Parms//TCPIP"),Names);


 //打开注册表对网卡IP信息对应子健进行修改
 HKEY   hkey;
 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,strKeyName,0,KEY_WRITE,&hkey)   !=   ERROR_SUCCESS)  
 {
  return FALSE;
 }
 DWORD   value;  
 value   =   0;
 WCHAR buffer[50];

 //set   EnableDHCP  
 if(RegSetValueEx(hkey,TEXT("EnableDHCP"),0,REG_DWORD,(const   unsigned   char   *)&value,sizeof(DWORD))   !=   ERROR_SUCCESS)  
  return FALSE;  
 //set   dns
 memset(buffer,0,100);
 memcpy(buffer,strDNS.GetBuffer(0),strDNS.GetLength()*sizeof(TCHAR));
 if(RegSetValueEx(hkey,TEXT("DNS"),0,REG_MULTI_SZ,(const   unsigned   char   *)buffer, strDNS.GetLength()*2+2) !=   ERROR_SUCCESS)  
  return FALSE;
 //set   ip   address  
 memset(buffer,0,100);
 memcpy(buffer,strIp.GetBuffer(0),strIp.GetLength()*sizeof(TCHAR));
 if(RegSetValueEx(hkey,TEXT("IpAddress"),0,REG_MULTI_SZ,(const   unsigned   char   *)buffer, strIp.GetLength()*2+2) !=   ERROR_SUCCESS)  
  return FALSE;
 //set   subnet   mask
 memset(buffer,0,100);
 memcpy(buffer,strMask.GetBuffer(0),strMask.GetLength()*sizeof(TCHAR));
 if(RegSetValueEx(hkey,TEXT("SubnetMask"),0,REG_MULTI_SZ,(const   unsigned   char   *)buffer, strMask.GetLength()*2+2) !=   ERROR_SUCCESS)  
  return FALSE;
 //set   gateway
 memset(buffer,0,100);
 memcpy(buffer,strGateWay.GetBuffer(0),strGateWay.GetLength()*sizeof(TCHAR));
 if(RegSetValueEx(hkey,TEXT("DefaultGateway"),0,REG_MULTI_SZ,(const   unsigned   char   *)buffer, strGateWay.GetLength()*2+2) !=   ERROR_SUCCESS)  
  return FALSE;
 RegFlushKey(hkey);
 RegCloseKey(hkey);

 // 重启网卡,不用机器热启动
 HANDLE hNdis = CreateFile(_T("NDS0:"), 0, 0, NULL,
  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  (HANDLE) INVALID_HANDLE_VALUE);
 if( hNdis == INVALID_HANDLE_VALUE )
 {
  //MessageBox(NULL,_T( "重启网络驱动时打开设备错误"),NULL,0);
  return FALSE;
 }

 // Send the device command.
 if (!DeviceIoControl( hNdis, IOCTL_NDIS_REBIND_ADAPTER,
  Names, _tcslen( Names) + sizeof( TCHAR ),    // buf contains the name of the
  NULL, 0, NULL, NULL ) )
 {
  CloseHandle(hNdis);
  return FALSE;
 }

 CloseHandle( hNdis );
 return TRUE;
}

BOOL GetIpAddress(CString& strIp,CString& strMask,CString& strGateWay, CString& strDNS)
{
 //获得网卡的设备名
 WCHAR Names[50];
 DWORD bytes;
 HANDLE m_hFileHandle = CreateFile(_T("NDS0:"), 0, 0, NULL,
  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  (HANDLE) INVALID_HANDLE_VALUE);

 if( m_hFileHandle == INVALID_HANDLE_VALUE )
 {
  return FALSE;
 }

 // Get list of adapter names
 if (!DeviceIoControl(m_hFileHandle,
  IOCTL_NDIS_GET_ADAPTER_NAMES,
  NULL,0,
  Names,MAX_PATH,&bytes,NULL))
 {
  return FALSE;
 }
 DWORD len = wcslen(Names);
 Names[len] = 0;
 Names[len+1] = 0;
 CString strKeyName;
 strKeyName.Format(_T("Comm//%s//Parms//TCPIP"),Names);


 //打开注册表对网卡IP信息对应子健进行修改
 HKEY   hkey;
 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,strKeyName,0,KEY_READ,&hkey)   !=   ERROR_SUCCESS)  
 {
  MessageBox(NULL,TEXT("打开注册表错误"),NULL,0);  
  return FALSE;
 }
 DWORD   dwEnableDHCP;  
 TCHAR buffer[50];
 DWORD dwType = 0, cbData = 0xffff;
 //query   EnableDHCP  
 if(RegQueryValueEx(hkey,TEXT("EnableDHCP"),0,&dwType,(PBYTE)&dwEnableDHCP,&cbData) !=   ERROR_SUCCESS)  
  return FALSE;   
 //query   dns
 cbData = 100;
 ZeroMemory(buffer, 50*sizeof(TCHAR));
 if(RegQueryValueEx(hkey,TEXT("DNS"),0,&dwType,(PBYTE)buffer, &cbData) !=   ERROR_SUCCESS)  
  return FALSE;
 strDNS = buffer;
  //set   ip   address  
 cbData = 100;
 ZeroMemory(buffer, 50*sizeof(TCHAR));
 if(RegQueryValueEx(hkey,TEXT("IpAddress"),0,&dwType,(PBYTE)buffer, &cbData) !=   ERROR_SUCCESS)  
  return FALSE;
 strIp = buffer;
//  //set   subnet   mask
 cbData = 100;
 ZeroMemory(buffer, 50*sizeof(TCHAR));
 if(RegQueryValueEx(hkey,TEXT("SubnetMask"),0,&dwType,(PBYTE)buffer, &cbData) !=   ERROR_SUCCESS)  
  return FALSE;
 strMask = buffer;
  //set   gateway
 cbData = 100;
 ZeroMemory(buffer, 50*sizeof(TCHAR));
 if(RegQueryValueEx(hkey,TEXT("DefaultGateway"),0,&dwType,(PBYTE)buffer, &cbData) !=   ERROR_SUCCESS)  
  return FALSE;
 strGateWay = buffer;
 
 RegCloseKey(hkey);

 return TRUE;
}

 

六、关闭屏幕显示(需要驱动支持,我当前用的SDK不支持该功能)

// GDI Escapes for ExtEscape()
#define QUERYESCSUPPORT    8

#define GETVFRAMEPHYSICAL   6144
#define GETVFRAMELEN    6145
#define DBGDRIVERSTAT    6146
#define SETPOWERMANAGEMENT   6147
#define GETPOWERMANAGEMENT   6148


typedef enum _VIDEO_POWER_STATE {
 VideoPowerOn = 1,
 VideoPowerStandBy,
 VideoPowerSuspend,
 VideoPowerOff
} VIDEO_POWER_STATE, *PVIDEO_POWER_STATE;


typedef struct _VIDEO_POWER_MANAGEMENT {
 ULONG Length;
 ULONG DPMSVersion;
 ULONG PowerState;
} VIDEO_POWER_MANAGEMENT, *PVIDEO_POWER_MANAGEMENT;


void CloseOpenScreen(boolean yes)
{

 HDC gdc;
 int iESC=SETPOWERMANAGEMENT;

 gdc = ::GetDC(NULL);
 if (ExtEscape(gdc, QUERYESCSUPPORT, sizeof(int), (LPCSTR)&iESC, 0, NULL)==0)
  MessageBox(NULL,
  L"Sorry, your Pocket PC does not support DisplayOff",
  NULL,MB_OK);
 else
 {
  VIDEO_POWER_MANAGEMENT vpm;
  vpm.Length = sizeof(VIDEO_POWER_MANAGEMENT);
  vpm.DPMSVersion = 0x0001;

  // Power off the display
  if (yes)
  {
   vpm.PowerState = VideoPowerOff;
   ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, (LPCSTR) &vpm,
    0, NULL);
  }

  else
  {
   vpm.PowerState = VideoPowerOn;
   // Power on the display
   ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, (LPCSTR) &vpm,
    0, NULL);
  }
  ::ReleaseDC(NULL, gdc);
 }
}

你可能感兴趣的:(list,video,null,buffer,Path,WinCE)