|
4楼
楼主 |
发表于 2004-10-30 16:50:00
|
只看该作者
#include ) K& @+ F6 i+ D8 z" L+ q
#include "resource.h"
% V* ~5 L$ z. D) H4 }( C' D#define APPNAME "AutoDial"
; x& Q) y2 Q$ R( N#define WM_MyIcon WM_USER + 100
4 @0 p; C% x& x* q4 e8 zconst char szRegKeyName[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Components\\51AF6AC6C47EC36459D07877B938F76E";
1 J0 S% W6 R7 u3 A# a9 G6 Uconst char szRegChar[] = "A0CDAE5BF3ECD0F439C33E627F3DA3E5";& ^! x d. i! {3 g. x- N1 C
const char szHzcncClassName[] = "TfrmMainNetClient";4 w6 t% e0 X) f
const char szClassName[ ] = "AutoDialForm";# t. u$ ?! t- w' g6 v$ @
HWND hNetClient,hConnectButton;+ `1 N) C0 ] L1 T# R L) h* w) k
HICON hIcon;# ^$ v% \/ n4 c6 T2 a- Y, Q4 s
HMENU hPopMenu;3 [/ _* _/ ~+ l3 s0 `( G) L
LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);2 f) z: U# O) p
LRESULT CALLBACK EditProc(HWND hWnd,
S, O z) ^& G) ^, o- y UINT Msg,
3 T; k4 c$ B; B7 @+ }" a" Q WPARAM wParam,
5 Q9 U) |( P0 u* C LPARAM lParam' f" U5 v7 x& a7 D& P0 u. H
);6 s1 x* z9 s5 ~+ m- ]- v
int RunNetClient(void);. ~/ r; t7 }! }! b- E% b0 _
NOTIFYICONDATA MyIcon;( R+ k% H' f8 M& o& Q# j) ~
WNDPROC EditOldWndFunc; R; [9 F6 S+ C4 f4 M
int WINAPI WinMain (HINSTANCE hThisInstance, q% G$ [( C4 P( ]3 p1 T: T
HINSTANCE hPrevInstance,
" | c/ e4 l# k' M; ? LPSTR lpszArgument,8 A, o @: N1 F# M6 V
int nFunsterStil)+ }1 O8 V1 o4 w
{- u6 J, s' Q8 Q0 ?6 Y; ^5 w. V
hIcon = LoadIcon(hThisInstance,MAKEINTRESOURCE(IDI_ICON));6 _& _6 ?2 D7 r
hPopMenu = GetSubMenu(LoadMenu(hThisInstance,MAKEINTRESOURCE(IDR_MENU1)),0);, m$ m9 _1 S7 z
DialogBox(hThisInstance, MAKEINTRESOURCE(IDD_MAIN_DLG), NULL, (DLGPROC)WindowProc);
3 L/ j7 }1 w5 Z/ i2 _ return 0;9 ^( _- n" s1 c: ^6 r2 r+ ?
}
4 X/ ~; N7 t1 u' CLRESULT CALLBACK WindowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)) e# c' O* ^2 x. U9 b" Z
{
$ O e$ ~4 I) s; c, P static UINT times = 0;3 }( a. R& Y |4 P
switch (message) /* handle the messages */. V) R H' x* j5 d' r& @
{
( c1 C$ \' q7 r" H case WM_INITDIALOG:! X' Y( ]$ J- h, `7 q! F6 W
hNetClient = FindWindow(szHzcncClassName,NULL);
1 D% {6 m1 g' x* X5 G. a% l' `: E( K. } if(!hNetClient)7 Z/ I+ k: E2 L w* S
{
0 k8 o( H5 U' d S4 A' @6 ~; l switch(RunNetClient()). ~& k* p/ }; W
{
" c9 |' Y# B) M9 N; ? case 0 :2 ]8 p1 `" }4 u! W) G) [8 h
break;# z4 ^. H' B3 O" ~ G4 r t
case 1 :) S4 W) W) I/ ~& R. f
MessageBox(hwnd,"请确定是否正确安装网通拨号软件!",APPNAME,MB_OK);
/ r2 q" X8 H; M; c break;3 z; q0 A$ m: P* `0 x
case 2 :$ }9 L8 i4 O N3 Z. [
MessageBox(hwnd,"无法运行拨号软件!",APPNAME,MB_OK);1 {, ?( f. [' u1 x o- a4 V
}
1 D) I, a% N* f! ^4 M hNetClient = FindWindow(szHzcncClassName,NULL);* T& C! n5 Q( R6 h- ^& J
}- q6 t* P# y w! o
hConnectButton = FindWindowEx(hNetClient,NULL,NULL,"连接");+ M" B& A9 W! h. U( K3 o
SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
: u7 @# j6 w5 ]' k+ r: f MyIcon.cbSize = sizeof(NOTIFYICONDATA);& Y8 R0 g& M) f0 q& Z
MyIcon.hWnd = hwnd; q) N3 N' V' ^, {' w. \
MyIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;' m8 e2 `. z5 i
MyIcon.uID = 1;+ [+ k" }. ?+ M- `/ T- [2 }+ g
MyIcon.hIcon =hIcon;
8 l( C# p; e' A ^ t( ^/ K0 \$ f, L MyIcon.uCallbackMessage = WM_MyIcon;* g6 k T$ O, _6 D* B: }8 J& Z, L! ^
lstrcpy(MyIcon.szTip,"自动拨号");
" Z& e; f3 E5 h4 q2 S( Z3 Y" J SetDlgItemInt(hwnd,IDC_EDIT1,1,0);
: a) [. j8 y- D8 O SetDlgItemInt(hwnd,IDC_EDIT2,0,0);( S" f( R& l. z' @- ~1 D" k
SetDlgItemText(hwnd,IDC_EDIT3,"11:29:00");
, y4 ~ K$ R1 r! p EditOldWndFunc = (WNDPROC)SetWindowLong(GetDlgItem(hwnd,IDC_EDIT3),GWL_WNDPROC,EditProc);
8 }2 U6 }3 v8 o K) X return 1;6 C' M, V+ O6 r5 f
break;
' n/ Y6 j" g2 v" ] case WM_TIMER:
3 i4 t3 s0 u- h7 m* m$ W hNetClient = FindWindow(szHzcncClassName,NULL);/ L( R, m" I& w$ \
if (!hNetClient)
7 h/ B0 R h( ?8 _' z* l. \$ E+ v {
/ {. {1 S7 Y1 S5 g( ^2 {: E KillTimer(hwnd,0);
5 E; m$ Z# U9 d! r4 C9 L break;
3 k' |" H3 u# o2 |, C }
7 s7 M. @" r H6 P4 Z: A6 p if(IsWindowEnabled(hConnectButton))5 C& }% h! V$ [3 F( u: r
{
' }' g" [; H$ {8 Z1 d' f+ D if(times < GetDlgItemInt(hwnd,IDC_EDIT2,NULL,0) || GetDlgItemInt(hwnd,IDC_EDIT2,NULL,0) == 0)3 c/ i- X$ u! a$ }
{
. ~7 \6 ~) S/ } SendMessage(hConnectButton,BM_CLICK,0,0);
- h& e5 a, l7 O' D# h; Y8 X" k times++;
4 l/ u) z$ m' \0 ]' G6 d4 S/ E/ x. u }
. [% s- i) B/ B7 `4 n t' ] 1 H% [# Z3 z; Y" f8 T) {
}" f8 g# V5 t% s* {7 T$ A
break;
( ]9 f% d! }! Q- L% ]9 f# ^( w; m case WM_COMMAND:) p0 h7 t7 D2 N7 y6 G
" k7 d; K7 o1 z+ r: s" o% f$ t switch( LOWORD(wParam) )
0 V) o* d; i, ]' p9 [2 { {' _7 d3 K' Z# `9 w0 i+ m
" u5 U# A9 U- n" p6 `5 t case IDOK:, V# B- ]- i/ }2 a }$ j
SetTimer(hwnd,0,1000 * GetDlgItemInt(hwnd,IDC_EDIT1,NULL,0),NULL);( S- j2 b9 t+ e' C7 `. \8 i7 C( S
ShowWindow(hwnd,SW_HIDE);7 O$ D- ~: J, G" @
Shell_NotifyIcon(NIM_ADD,&MyIcon);* @! X- u! q6 Z; U. r( I: l' \
times = 0;5 a$ X4 L+ o3 W, ^
break;
8 L9 N. {( P. a% v case IDM_SETTING:
& e1 c, d) L# ] ShowWindow(hwnd,SW_NORMAL);) z: s7 n( Z& }. T6 {
Shell_NotifyIcon(NIM_DELETE,&MyIcon);
+ T d& D6 Z) t0 ?2 ]2 f KillTimer(hwnd,0);
( L0 `# S2 L3 _ @# K break;$ l; n I, C# i$ f! G9 {
case IDCANCEL:
& I P2 |+ E+ B" p' P case IDM_EXIT:
: F% [0 S0 U, I- {+ L8 v! i8 H+ ]1 G Shell_NotifyIcon(NIM_DELETE,&MyIcon);- e- p# f! I! s6 u( S, p1 u
KillTimer(hwnd,0);
; N0 r# L) l! K1 z: [8 C! {) q EndDialog( hwnd, 0 );/ p1 ]4 j& C) e! M: h
return TRUE;
6 L# R5 _, L/ ]0 w1 z+ h: i9 v }
* K/ S. j+ \7 P# ~0 R5 \) D return 1;
. Y8 X# P' o, \! A+ V. ], F0 a" X case WM_MyIcon:# }! l2 a" l5 N
if(lParam == WM_RBUTTONDOWN )
y! z y7 ?0 F/ X {* D. u0 Q" U+ e( W: u0 w4 v
POINT pt;& Q* G) O# Z7 u
GetCursorPos(&pt);; P7 @" p+ S. a& x+ z/ v, R7 l
SetForegroundWindow(hwnd);( h6 S" V; q: {- H+ d
TrackPopupMenu(hPopMenu,TPM_RIGHTBUTTON,pt.x,pt.y,NULL,hwnd,NULL);
( n* f( _/ T5 T: U9 B6 S PostMessage(hwnd,WM_NULL,NULL,NULL);
) E3 w; s( A& F5 X' u. ] }) K3 J; `: y* U3 C- D0 I
break;
; x: [- u; ?* T. @0 ?0 { case WM_DESTROY:
1 _" n/ b( X- `, W, a6 F* {+ @& ] EndDialog(hwnd,0);- q" ?, ]6 e8 P0 ]4 c
return 1;
) A2 N% f, J' t& |, F* | break;$ s0 F# I/ V/ x7 h5 X1 ?) O
}8 a, ^! g. Z; U& _! K! i
return 0;
% U+ R( K! W l8 }1 w}; X2 m/ F* r6 I( B1 E+ o" s* }4 c0 ?
LRESULT CALLBACK EditProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
+ o! \7 o0 [; G& r2 E+ \4 G{
( Z' I# v0 A& G% c switch(Msg)
+ R5 j y- {8 R& A5 H2 I5 @ {
+ q& D8 b, v. k* I3 @8 \ case WM_KEYDOWN:
8 l3 l: s$ _ i4 Z case WM_KEYUP:* B- G2 D8 h# y" U
case WM_CHAR:6 c3 G9 q+ B* n7 l
break;
- s; H% `+ Z) X: H default:2 B' g5 B- |) H) e, C! ]9 m& Z( X
CallWindowProc(EditOldWndFunc,hWnd,Msg,wParam,lParam);3 d4 j' i/ h/ i9 n% ~7 M" E; B2 T
}
}% }7 s8 F. T( `! U 8 v! e- I! e$ A0 n
return 1;
# Z! {2 ~/ U" Q}
- A# `* i6 I- _int RunNetClient(void)& i/ ]% m& d( P5 @. ^9 d
{1 @( @# M& S! |2 f$ ~" _
HKEY hkey;; n |( W) l2 Q/ m7 P3 q+ ]0 C6 I
char szAppPath[256];
8 i) f! ]4 q. x4 N. o/ A z DWORD ButterSize = 256;% O/ _* G: m3 I8 X7 {6 T
/*读取网通拨号程序的硬盘位置*/
" ^* L9 R# k; e8 N. | | if (RegOpenKey(HKEY_LOCAL_MACHINE,szRegKeyName,&hkey) != ERROR_SUCCESS)
- L9 V7 P+ T: Z3 e return 1;
3 l5 w* E& d' A d6 f( e( l u8 } if (RegQueryValueEx(hkey,szRegChar,NULL,NULL,szAppPath,&ButterSize) != ERROR_SUCCESS)+ X8 Q! A% N* D$ W3 E; ]) l4 P3 W
return 1;
9 \& b& @' }( f9 v, J RegCloseKey(hkey);5 @6 {0 y. ?8 Z: A$ W
if (ShellExecute(0,"open",szAppPath,NULL,NULL,SW_SHOWNORMAL) <= 32)1 z+ }4 z+ G, w) ]0 m
return 2;3 I! D# E5 t+ e0 B( ~- d/ y/ W }
return 0;- }7 ~2 ?& |: L1 o4 B0 |( P$ `; d
}
, Y+ |: w9 B# [4 y
; o+ g ^3 F1 y( C B8 N6 ]% E" H8 p以上代码 |
|