VC点滴1

过完年了,上了3天的班,感觉还是上班比较充实些,放假回家都不知道干什么了. 这段时间一直沉浸于vc中,感觉到vc真的很麻烦,写个UI太费劲了.不过目前的这个DCOM终于大部分的功能都完成了.  整理下其中使用的一些小的部分,记录以后可能也会用到.
1.获取登陆OS的用户名和域名.
因为这次开发用户都是是用AD进行管理的,所以要获取登陆OS的用户名,然后到DB中捞资料然后登陆分机.
获取方法如下:
#define MAX_SID_SIZE   1024
#define MAX_NAME_STRING   1024
//Get OSUser  Name 
void XXXXXX()
{
TCHAR  errorMessage[
1024];
 TCHAR  userName[MAX_NAME_STRING], domainName[MAX_NAME_STRING];
 TCHAR  subKeyName[MAX_PATH];
 DWORD  subKeyNameSize, index;
 DWORD  userNameSize, domainNameSize;
 FILETIME lastWriteTime;
 HKEY  usersKey;
 PSID  sid;
 SID_NAME_USE sidType;
 SID_IDENTIFIER_AUTHORITY authority;
 BYTE  subAuthorityCount;
 DWORD  authorityVal, revision;
 DWORD  subAuthorityVal[
8= 00000000 };
 
//
 
// Use RegConnectRegistry so that we work with remote computers
 
//

 
if( RegOpenKey( HKEY_USERS, NULL, &usersKey ) != ERROR_SUCCESS ) {

  wprintf( errorMessage, L
"Error opening HKEY_USERS" );
  GetError();
  
//  PrintWin32Error( errorMessage, GetLastError() );
  
//  return FALSE;
 }


 
//
 
// Enumerate keys under HKEY_USERS
 
//
 index = 0;
 subKeyNameSize 
= sizeof( subKeyName );
 
while( RegEnumKeyEx( usersKey, index, subKeyName, &subKeyNameSize,
  NULL, NULL, NULL, 
&lastWriteTime ) == ERROR_SUCCESS ) {

   
//
   
// Ignore the default subkey and win2K user class subkeys
   
//
   if( wcsicmp( subKeyName, L".default" ) &&
    
!wcsstr( subKeyName, L"Classes")) {

     
//
     
// Convert the textual SID into a binary SID
     
//
     subAuthorityCount= swscanf( subKeyName, L"S-%d-%x-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu",
      
&revision, &authorityVal,
      
&subAuthorityVal[0],
      
&subAuthorityVal[1],
      
&subAuthorityVal[2],
      
&subAuthorityVal[3],
      
&subAuthorityVal[4],
      
&subAuthorityVal[5],
      
&subAuthorityVal[6],
      
&subAuthorityVal[7] );

     
if( subAuthorityCount >= 3 ) {

      subAuthorityCount 
-= 2;

      
//
      
// Note: we can only deal with authority values
      
// of 4 bytes in length
      
//
      authority.Value[5= *(PBYTE) &authorityVal;
      authority.Value[
4= *((PBYTE) &authorityVal+1);
      authority.Value[
3= *((PBYTE) &authorityVal+2);
      authority.Value[
2= *((PBYTE) &authorityVal+3);
      authority.Value[
1= 0;
      authority.Value[
0= 0;

      
//
      
// Initialize variables for subsequent operations
      
//
      sid = NULL;
      userNameSize   
= MAX_NAME_STRING;
      domainNameSize 
= MAX_NAME_STRING;

      
if( AllocateAndInitializeSid( &authority,
       subAuthorityCount,
       subAuthorityVal[
0],
       subAuthorityVal[
1],
       subAuthorityVal[
2],
       subAuthorityVal[
3],
       subAuthorityVal[
4],
       subAuthorityVal[
5],
       subAuthorityVal[
6],
       subAuthorityVal[
7],
       
&sid )) {

        
//
        
// We can finally lookup the account name
        
//
        if( LookupAccountSid( NULL,
         sid, 
         userName,
         
&userNameSize,
         domainName,
         
&domainNameSize,
         
&sidType )) {

          
//
          
// We've successfully looked up the user name
          
//
          if(wcsicmp( domainName, L"NT AUTHORITY"))
          
{
           m_User 
= userName;
           m_Domain 
= domainName;
           UpdateData(FALSE);
           wprintf( L
"%s\\%s\n", domainName, userName );   
          }

        }

      }
               
      
if( sid ) FreeSid( sid );
     }

   }

   subKeyNameSize 
= sizeof( subKeyName );
   index
++;
 }

 RegCloseKey( usersKey ); 
}


2.改变窗口背景色.

HBRUSH CDialFrm::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 switch (nCtlColor) {
  case CTLCOLOR_STATIC:
  case CTLCOLOR_DLG:
  {
   pDC->SetBkMode(TRANSPARENT);
   HBRUSH b = CreateSolidBrush(RGB(0,0,0));
   return (HBRUSH) b;
  }
 }
 return hbr;
}


目前还差一个,就是跑马灯的实现,现在写了一个跑马灯的类,但是只能支持左到右的跑,按规格上看,也要支持上下才行。如果有人知道这样的类,告诉俺下,谢谢咯。

原文地址:https://www.cnblogs.com/zhucl1006/p/1070012.html