www.pudn.com > DmyNP_source.rar > Enter.c, change:2004-12-13,size:14564b


/*--> 
Damayi Net-Phone, 2004/11 (damayi.org) 
<--*/ 
 
#include "src\winmain.h" 
 
#define ENTERWIN_STYLE WS_SYSMENU|WS_POPUP|WS_BORDER|WS_CAPTION|WS_MINIMIZEBOX 
#define CTRLEDIT_STYLE WS_VISIBLE|WS_CHILD|WS_TABSTOP|WS_BORDER|ES_AUTOHSCROLL 
#define CTRLCHEK_STYLE WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_FLAT|BS_AUTOCHECKBOX 
#define CTRLBUT_STYLE  WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_FLAT 
#define CTRLCBX_STYLE  WS_VISIBLE|WS_CHILD|WS_VSCROLL|WS_TABSTOP|CBS_DROPDOWNLIST 
#define _MAKEINT(a,b)  ((DWORD)(((WORD)(a))|(((DWORD)((WORD)(b)))<<16))) 
 
static char const * const gcpEnterClassName = "Enter@"; 
static HBITMAP ghBitLogon;    //Logon位图句柄 
static int giEnterReturnVar;  //Enter处理过程返回值变量 
static HWND ghFocusWnd;       //键盘输入焦点窗口句柄 
 
#define CTRL_PID   1001 
#define CTRL_PASS  1002 
#define CTRL_IPMD  1003 
#define CTRL_LIEUP 1004 
#define CTRL_PRO   1005 
#define CTRL_IP    1006 
#define CTRL_SER   1007 
#define CTRL_TIME  1008 
#define CTRL_PORT  1009 
#define CTRL_OK    IDOK 
#define CTRL_REG   1010 
#define CTRL_HELP  1011 
 
//号码注册对话框窗口回调函数 
static BOOL CALLBACK RegBoxCallback(HWND h, UINT u, WPARAM w, LPARAM l) 
{ 
   switch(u) 
   { 
      case WM_COMMAND: if(LOWORD(w)==IDOK)EndDialog(h,0); return(TRUE); 
      case WM_CLOSE: EndDialog(h,l); return(TRUE); 
   } 
   return FALSE; 
} 
 
//登录方式切换 
//参数: hWnd Enter窗口句柄 
//      bIP  TRUE=IP模式, FALSE=联机模式 
static void SetIpMode(HWND hWnd, BOOL bIP) 
{ 
   if(bIP) 
   { 
      SetWindowText(GetDlgItem(hWnd,CTRL_OK), "启动电话"); 
      EnableWindow(GetDlgItem(hWnd,CTRL_PID), FALSE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_PASS), FALSE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_LIEUP), FALSE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_IP), FALSE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_SER), FALSE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_TIME), FALSE); 
   } 
   else 
   { 
      SetWindowText(GetDlgItem(hWnd,CTRL_OK), "用户登录"); 
      EnableWindow(GetDlgItem(hWnd,CTRL_PID), TRUE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_PASS), TRUE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_LIEUP), TRUE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_IP), TRUE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_SER), TRUE); 
      EnableWindow(GetDlgItem(hWnd,CTRL_TIME), TRUE); 
   } 
   return; 
} 
 
//模式切换 
//参数: hWnd Enter窗口句柄 
//      bMd  TRUE=高级, FALSE=标准 
static void SetProMode(HWND hWnd, BOOL bMd) 
{ 
   RECT r, rtXY = {0,0,223,190}; 
   GetWindowRect(hWnd, &r); 
   if(bMd) 
   { 
      MoveWindow(GetDlgItem(hWnd,CTRL_OK), 13, 242, 63, 23, TRUE); 
      MoveWindow(GetDlgItem(hWnd,CTRL_REG), 80, 242, 63, 23, TRUE); 
      MoveWindow(GetDlgItem(hWnd,CTRL_HELP), 147, 242, 63, 23, TRUE); 
      ShowWindow(GetDlgItem(hWnd,CTRL_IP), SW_SHOW); 
      ShowWindow(GetDlgItem(hWnd,CTRL_SER), SW_SHOW); 
      ShowWindow(GetDlgItem(hWnd,CTRL_TIME), SW_SHOW); 
      ShowWindow(GetDlgItem(hWnd,CTRL_PORT), SW_SHOW); 
      rtXY.bottom = 276; 
      AdjustWindowRectEx(&rtXY, ENTERWIN_STYLE, 0, 0); 
      MoveWindow(hWnd, r.left, r.top, r.right-r.left, rtXY.bottom-rtXY.top, TRUE); 
      InvalidateRect(hWnd, 0, TRUE); 
   } 
   else 
   { 
      ShowWindow(GetDlgItem(hWnd,CTRL_IP), SW_HIDE); 
      ShowWindow(GetDlgItem(hWnd,CTRL_SER), SW_HIDE); 
      ShowWindow(GetDlgItem(hWnd,CTRL_TIME), SW_HIDE); 
      ShowWindow(GetDlgItem(hWnd,CTRL_PORT), SW_HIDE); 
      MoveWindow(GetDlgItem(hWnd,CTRL_OK), 13, 156, 63, 23, TRUE); 
      MoveWindow(GetDlgItem(hWnd,CTRL_REG), 80, 156, 63, 23, TRUE); 
      MoveWindow(GetDlgItem(hWnd,CTRL_HELP), 147, 156, 63, 23, TRUE); 
      AdjustWindowRectEx(&rtXY, ENTERWIN_STYLE, 0, 0); 
      MoveWindow(hWnd, r.left, r.top, r.right-r.left, rtXY.bottom-rtXY.top, TRUE); 
      InvalidateRect(hWnd, 0, TRUE); 
   } 
   return; 
} 
 
//创建Enter窗口控制 
//参数: Enter窗口句柄 
//返回: 成功返回TRUE, 失败返回FALSE。 
static BOOL CreateEnterWindowCortrol(HWND hWnd) 
{ 
   int i; 
   struct in_addr stInAddr; 
   HOSTENT *pstHEnt; 
   HWND hChw; 
 
   #define _cwEDIT(id,sy,x,y,w,h) CreateWindowEx(0,"EDIT","",\ 
   CTRLEDIT_STYLE|(sy),(x),(y),w,h,hWnd,(HMENU)(id),g_hInst,0) 
   #define _cwCHEK(id,tx,x,y,w,h) CreateWindowEx(0,"BUTTON",(tx),\ 
   CTRLCHEK_STYLE,(x),(y),(w),(h),hWnd,(HMENU)(id),g_hInst, 0) 
   #define _cwCBOX(id,tx,x,y,w,h) CreateWindowEx(0,"COMBOBOX",(tx),\ 
   CTRLCBX_STYLE,(x),(y),(w),(h),hWnd,(HMENU)(id),g_hInst,(0)) 
   #define _cwBUTN(id,tx,x,y,w,h) CreateWindowEx(0,"BUTTON",(tx),\ 
   CTRLBUT_STYLE,(x),(y),(w),(h),hWnd,(HMENU)(id),g_hInst,(0)) 
 
   //创建窗口控制 
   if(!_cwEDIT(CTRL_PID, ES_LOWERCASE|ES_OEMCONVERT, 72,76,138,20)) return FALSE; 
   if(!_cwEDIT(CTRL_PASS, ES_PASSWORD|ES_OEMCONVERT, 72,101,138,20)) return FALSE; 
   if(!_cwCHEK(CTRL_IPMD,"IP模式", 13,131,60,16)) return FALSE; 
   if(!_cwCHEK(CTRL_LIEUP,"隐身登录", 81,131,72,16)) return FALSE; 
   if(!_cwCHEK(CTRL_PRO,"高级", 168,131,48,16)) return FALSE; 
   if(!_cwCBOX(CTRL_IP,"本地IP地址", 84,155,126,96)) return FALSE; 
   if(!_cwCBOX(CTRL_SER,"服务器地址", 84,180,126,96)) return FALSE; 
   if(!_cwEDIT(CTRL_TIME, ES_NUMBER|ES_OEMCONVERT, 72,210,45,20)) return FALSE; 
   if(!_cwEDIT(CTRL_PORT, ES_NUMBER|ES_OEMCONVERT, 168,210,42,20)) return FALSE; 
   if(!_cwBUTN(CTRL_OK,"用户登录", 13,156,63,23)) return FALSE; 
   if(!_cwBUTN(CTRL_REG,"注册号码", 80,156,63,23)) return FALSE; 
   if(!_cwBUTN(CTRL_HELP,"联机帮助", 147,156,63,23)) return FALSE; 
 
   #undef _cwBUTN 
   #undef _cwCBOX 
   #undef _cwCHEK 
   #undef _cwEDIT 
 
   //设置控制属性 
   SendDlgItemMessage(hWnd,CTRL_PID, WM_SETFONT, (WPARAM)g_GJ.hDevFont,0); 
   SendDlgItemMessage(hWnd,CTRL_PASS, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_IPMD, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_LIEUP, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_PRO, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_IP, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_SER, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_TIME, WM_SETFONT, (WPARAM)g_GJ.hDevFont,0); 
   SendDlgItemMessage(hWnd,CTRL_PORT, WM_SETFONT, (WPARAM)g_GJ.hDevFont,0); 
   SendDlgItemMessage(hWnd,CTRL_OK, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_REG, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_HELP, WM_SETFONT, (WPARAM)g_GJ.hGuiFont,0); 
   SendDlgItemMessage(hWnd,CTRL_PID, EM_SETLIMITTEXT, 7, 0); 
   SendDlgItemMessage(hWnd,CTRL_PASS, EM_SETLIMITTEXT, 20, 0); 
   SendDlgItemMessage(hWnd,CTRL_TIME, EM_SETLIMITTEXT, 5, 0); 
   SendDlgItemMessage(hWnd,CTRL_PORT, EM_SETLIMITTEXT, 5, 0); 
   if(!g_SO.bAvaiINI) 
   { 
      SendDlgItemMessage(hWnd,CTRL_PASS, EM_SETPASSWORDCHAR, 0, 0); 
      SendDlgItemMessage(hWnd,CTRL_PASS, WM_SETFONT,(WPARAM)g_GJ.hDevFont,0); 
      SetWindowText(GetDlgItem(hWnd,CTRL_PASS), "(无需密码)"); 
   } 
   SetProMode(hWnd, g_IV.bPro); 
   SetIpMode(hWnd, g_IV.bStartMd); 
   ShowWindow(hWnd, SW_SHOW); 
 
   //初始化控制 
   g_pTmp[7] = 0; 
   if(!ItoC(g_IV.dwPID, g_pTmp)) 
      SetFocus(GetDlgItem(hWnd,CTRL_PID)); 
   else 
   { 
      SetWindowText(GetDlgItem(hWnd,CTRL_PID), g_pTmp); 
      SetFocus(GetDlgItem(hWnd,CTRL_PASS)); 
   } 
 
   if(g_IV.bStartMd) SetFocus(GetDlgItem(hWnd,CTRL_OK)); 
   SendDlgItemMessage(hWnd,CTRL_IPMD, BM_SETCHECK, g_IV.bStartMd, 0); 
   SendDlgItemMessage(hWnd,CTRL_LIEUP, BM_SETCHECK, g_IV.bLieup, 0); 
   SendDlgItemMessage(hWnd,CTRL_PRO, BM_SETCHECK, g_IV.bPro, 0); 
 
   hChw = GetDlgItem(hWnd, CTRL_IP); 
   gethostname(g_pTmp, sizeof(g_pTmp)); 
   pstHEnt = gethostbyname(g_pTmp); 
   if(pstHEnt==0) 
      SendMessage(hChw, CB_ADDSTRING, 0, (LPARAM)"127.0.0.1"); 
   else 
   { 
      for(i=0; i<10; i++) 
      { 
      if(!pstHEnt->h_addr_list[i]) break; 
      stInAddr.s_addr = *((DWORD*)(pstHEnt->h_addr_list[i])); 
      SendMessage(hChw, CB_ADDSTRING, 0, (LPARAM)inet_ntoa(stInAddr)); 
      } 
   } 
 
   SendDlgItemMessage(hWnd,CTRL_IP, CB_SETCURSEL, 0, 0); 
   SendDlgItemMessage(hWnd,CTRL_IP, CB_SETCURSEL, g_IV.dwLocAr, 0); 
   SendDlgItemMessage(hWnd,CTRL_SER, CB_ADDSTRING, 0, (LPARAM)SERNAME); 
   SendDlgItemMessage(hWnd,CTRL_SER, CB_ADDSTRING, 0, (LPARAM)LOCIP_1); 
   SendDlgItemMessage(hWnd,CTRL_SER, CB_ADDSTRING, 0, (LPARAM)LOCIP_2); 
   SendDlgItemMessage(hWnd,CTRL_SER, CB_SETCURSEL, 0, 0); 
   SendDlgItemMessage(hWnd,CTRL_SER, CB_SETCURSEL, g_IV.dwSerAr, 0); 
   wsprintf(g_pTmp, "%d", g_IV.dwKeTime); 
   SetWindowText(GetDlgItem(hWnd,CTRL_TIME), g_pTmp); 
   wsprintf(g_pTmp, "%d", g_IV.wPort); 
   SetWindowText(GetDlgItem(hWnd,CTRL_PORT), g_pTmp); 
   if(g_SO.bPortErr) SetFocus(GetDlgItem(hWnd,CTRL_PORT)); 
   g_SO.bPortErr = FALSE; 
   ghFocusWnd = GetFocus(); 
   ghBitLogon = LoadBitmap(g_hInst, "Logon"); 
   return TRUE; 
} 
 
//获取Enter窗口控制内容 
//参数: Enter窗口句柄 
//返回: 成功返回TRUE, 失败返回FALSE。 
static BOOL GetCtrlvar(HWND hWnd) 
{ 
   GetWindowRect(hWnd, &g_SO.rtEnter); 
   GetWindowText(GetDlgItem(hWnd,CTRL_PORT), g_pTmp, 6); 
   g_IV.wPort = (WORD)atoi(g_pTmp); 
   if(g_IV.wPort==0) g_IV.wPort=5002; 
 
   GetWindowText(GetDlgItem(hWnd,CTRL_TIME), g_pTmp, 6); 
   g_IV.dwKeTime = atoi(g_pTmp); 
   if(g_IV.dwKeTime<10000) g_IV.dwKeTime=10000; 
   if(g_IV.dwKeTime>30000) g_IV.dwKeTime=30000; 
 
   g_IV.dwSerAr = SendDlgItemMessage(hWnd,CTRL_SER, CB_GETCURSEL,0,0); 
   g_IV.dwLocAr = SendDlgItemMessage(hWnd,CTRL_IP, CB_GETCURSEL,0,0); 
   g_IV.bPro = SendDlgItemMessage(hWnd,CTRL_PRO, BM_GETCHECK,0,0); 
   g_IV.bLieup = SendDlgItemMessage(hWnd,CTRL_LIEUP, BM_GETCHECK,0,0); 
   g_IV.bStartMd = SendDlgItemMessage(hWnd,CTRL_IPMD, BM_GETCHECK,0,0); 
 
   ZeroMemory(g_pTmp, 20); 
   GetWindowText(GetDlgItem(hWnd,CTRL_PASS), g_pTmp, 21); 
   CopyMemory(g_SO.pass, g_pTmp, sizeof(g_SO.pass)); 
 
   GetWindowText(GetDlgItem(hWnd,CTRL_PID), g_pTmp, 8); 
   g_IV.dwPID = CtoI(g_pTmp); 
   if(g_IV.dwPID==0 && !g_IV.bStartMd) 
   { 
      MessageBox(hWnd, strGETCTRLVAR, "提示", MB_ICONINFORMATION); 
      SetFocus(GetDlgItem(hWnd,CTRL_PID)); 
      return FALSE; 
   } 
   return TRUE; 
} 
 
//WM_PAINT消息处理过程 
//参数: Enter窗口句柄 
static void PaintMes(HWND hWnd) 
{ 
   PAINTSTRUCT ps; 
   HDC hDC; 
   HGDIOBJ hObj; 
 
   hDC = BeginPaint(hWnd, &ps); 
   if(hDC==NULL) return; 
   hObj = SelectObject(g_GJ.hMemDC, ghBitLogon); 
   BitBlt(hDC, 1, 2, 220, 62, g_GJ.hMemDC, 0, 0, SRCCOPY); 
   SelectObject(g_GJ.hMemDC, hObj); 
 
   SetTextColor(hDC, 0); 
   SetBkMode(hDC, TRANSPARENT); 
   hObj = SelectObject(hDC, g_GJ.hGuiFont); 
   TextOut(hDC, 13, 81, "用户号码:", 9); 
   TextOut(hDC, 13,106, "用户密码:", 9); 
   if(g_IV.bPro) 
   { 
      TextOut(hDC, 13, 159, "本地IP地址:", 11); 
      TextOut(hDC, 13, 183, "服务器地址:", 11); 
      TextOut(hDC, 13, 215, "联机间隔:", 9); 
      TextOut(hDC, 133, 215, "端口:", 5); 
   } 
   SelectObject(hDC, hObj); 
   EndPaint(hWnd, &ps); 
   return; 
} 
 
//WM_COMMAND消息处理过程 
//参数: hWnd  Enter窗口句柄 
//      hChw  控制句柄 
//      wBID  控制ID代码 
static void CommandMes(HWND hWnd, HWND hChw, WORD wBID) 
{ 
   switch(wBID) 
   { 
      case CTRL_IPMD: 
         g_IV.bStartMd = SendMessage(hChw, BM_GETCHECK,0,0); 
         SetIpMode(hWnd, g_IV.bStartMd); 
         break; 
      case CTRL_PRO: 
         g_IV.bPro = SendMessage(hChw, BM_GETCHECK,0,0); 
         SetProMode(hWnd, g_IV.bPro); 
         break; 
      case CTRL_OK: 
         SendMessage(hChw, BM_SETSTYLE, CTRLBUT_STYLE, _MAKEINT(1,0)); 
         if(!GetCtrlvar(hWnd)) break; 
         giEnterReturnVar = 0; 
         DestroyWindow(hWnd); 
         break; 
      case CTRL_REG: 
         DialogBox(g_hInst, "RegDialog", hWnd, RegBoxCallback); 
         SendMessage(hChw, BM_SETSTYLE, CTRLBUT_STYLE, _MAKEINT(1,0)); 
         break; 
      case CTRL_HELP: 
         ShellExecute(0,0, HLP_ENTER, 0,0, SW_SHOW); 
         SendMessage(hChw, BM_SETSTYLE, CTRLBUT_STYLE, _MAKEINT(1,0)); 
         break; 
   } 
   return; 
} 
 
//WM_ACTIVATE消息处理过程 
//参数: hWnd  Enter窗口句柄 
static void ActivateMes(HWND hWnd, WPARAM wParam) 
{ 
   if(LOWORD(wParam)==WA_INACTIVE) 
      ghFocusWnd = GetFocus(); 
   else 
   { 
      if(HIWORD(wParam)) return; 
      if(ghFocusWnd) SetFocus(ghFocusWnd); 
      else SetFocus(GetDlgItem(hWnd,CTRL_OK)); 
   } 
   return; 
} 
 
//Enter窗口回调函数 
static LRESULT CALLBACK EnterCallback(HWND hWnd, UINT u, WPARAM w, LPARAM l) 
{ 
   switch(u) 
   { 
      case WM_CREATE: 
         if(CreateEnterWindowCortrol(hWnd)) break; 
         PromptError("加载登录对话框失败!"); 
         return -1; 
      case WM_DESTROY: 
         if(ghBitLogon!=NULL) DeleteObject(ghBitLogon); 
         ghBitLogon = NULL; 
         PostQuitMessage(0); 
         break; 
      case WM_ACTIVATE: 
         ActivateMes(hWnd, w); 
         break; 
      case WM_PAINT: 
         PaintMes(hWnd); 
         break; 
      case WM_COMMAND: 
         CommandMes(hWnd, (HWND)l, LOWORD(w)); 
         break; 
      default: 
         return(DefWindowProc(hWnd, u, w, l)); 
   } 
   return 0; 
} 
 
//Enter处理过程入口函数 
//返回: 继续返回0, 退出返回-1。 
int EnterMain(void) 
{ 
   WNDCLASS WndClass = {0}; 
   RECT rect = {0,0,223,190}; 
   RECT rtspi; 
   ATOM atom; 
   HWND hWnd; 
   MSG  msg; 
 
   giEnterReturnVar = -1; 
   ghBitLogon = NULL; 
   ghFocusWnd = NULL; 
 
   //注册窗口类 
   WndClass.lpszClassName = gcpEnterClassName; 
   WndClass.hInstance     = g_hInst; 
   WndClass.lpfnWndProc   = EnterCallback; 
   WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW); 
   WndClass.hIcon         = g_GJ.hIconB; 
   WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); 
   atom = RegisterClass(&WndClass); 
   if(atom==0) 
   { 
      PromptError("注册窗口类失败!"); 
      goto EnExit; 
   } 
 
   //创建Enter窗口 
   SystemParametersInfo(SPI_GETWORKAREA, 0, &rtspi, 0); 
   AdjustWindowRectEx(&rect, ENTERWIN_STYLE, 0, 0); 
   hWnd = CreateWindowEx(0, gcpEnterClassName, "DmyNP - 用户登录", 
          ENTERWIN_STYLE, (rtspi.right-(rect.right-rect.left))/2, 
          (rtspi.bottom-(rect.bottom-rect.top))/2, rect.right-rect.left, 
          rect.bottom-rect.top, NULL, NULL, g_hInst, NULL); 
   if(hWnd==NULL) 
   { 
      PromptError("创建登录对话框失败!"); 
      goto EnExit; 
   } 
 
   while(GetMessage(&msg, NULL, 0, 0)) 
   { 
      if(IsDialogMessage(hWnd, &msg)) continue; 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
   } 
 
   EnExit: 
      if(atom!=0) UnregisterClass(gcpEnterClassName, g_hInst); 
   return giEnterReturnVar; 
}