Currently i have the Magic Jack project coding by C#.
I can use this application to active, make the call via Magic Jack service.
It run well but i don't know how to get the time - the signal the from other the fone when pick up and answer?
Thanks so much.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Media;
namespace MJWindow
{
public partial class Form1 : Form
{
string strFileName = System.Configuration.ConfigurationSettings.AppSettings["System_Value"];
SoundPlayer pm = null;
public Form1()
{
InitializeComponent();
string strbu = @""+ strFileName;
pm = new SoundPlayer(strbu);
}
int inthWnd=0;
private int MJWindow;
private int CallIDWindow;
private int MaxState;
private object EditWnd;
private int MYDialBtn;
private bool ReadyState;
private int AreaCode;
private int RingDelay;
private int CallWait;
private Object PopUp;
private const int IDOK = 1;
private const int IDCANCEL = 2;
private const int IDABORT = 3;
private const int IDRETRY = 4;
private const int IDIGNORE = 5;
private const int IDYES = 6;
private const int IDNO = 7;
//For SetWindowPos()
private const int SWP_NOMOVE = 2;
private const int SWP_NOSIZE = 1;
private const int SWP_SHOWWINDOW = 0x40;
private const int SWP_HIDEWINDOW = 0x80 ;
private const int Flags = SWP_NOMOVE;// : SWP_NOSIZE;
private const int HWND_TOPMOST = -1;
private const int HWND_NOTOPMOST = -2;
private const int SW_HIDE = 0;
private const int SW_NORMAL = 1;
private const int SW_MINIMIZED = 2;
private const int SW_MAXIMIZED = 3;
private const int SW_NOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_MINNOACTIVE = 7;
private const int SW_NA = 8;
private const int SW_RESTORE = 9;
private const int WM_CHAR = 0x102;
private const int WM_CLOSE = 0x10;
private const int WM_COMMAND = 0x111;
private const int WM_GETTEXT = 0xD;
private const int WM_GETTEXTLENGTH = 0xE;
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
private const int WM_LBUTTONDBLCLK = 0x203;
private const uint WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
private const int WM_MOVE = 0xF012;
private const int WM_SETTEXT = 0xC;
private const int WM_SYSCOMMAND = 0x112;
//Virtual Keys, Standard Set
private const int VK_DOWN = 0x28;
private const int VK_LEFT = 0x25;
private const int VK_MENU = 0x12;
private const int VK_RETURN = 0xD;
private const int VK_RIGHT = 0x27;
private const int VK_SHIFT = 0x10;
private const int VK_SPACE = 0x20;
private const int VK_UP = 0x26;
private const int VK_END = 0x23;
private const int VK_MULTIPLY = 0x6A;
private const int VK_ESCAPE = 0x1D;
private const int VK_CONTROL = 0x11;
private const int VK_R = 0x52;
private const short GWL_STYLE = (-16);
private const short GWL_EXSTYLE = (-20);
private const int WS_VISIBLE = 0x10000000;
private const int WS_EX_APPWINDOW = 0x40000;
private const int WS_EX_NOACTIVATE = 0x8000000;
private const int WS_EX_TOPMOST = 0x8;
private const int WS_CHILD = 0x40000000;
private const int WS_CHILDWINDOW = (WS_CHILD);
private const int WS_CLIPCHILDREN = 0x2000000;
private const int WS_CLIPSIBLINGS = 0x4000000;
private const int WS_EX_LEFT = 0x0;
private const int WS_EX_LTRREADING = 0x0;
private const int WS_EX_RIGHTSCROLLBAR = 0x0;
private const int WS_OVERLAPPED = 0x0;
//private const int WS_POPUP = Ox80000000;
[DllImport("user32.dll")]private static extern int GetAsyncKeyState(int vKey);
[DllImport("user32.dll")]private static extern int GetForegroundWindow();
[DllImport("user32.dll")]private static extern int SetForegroundWindow(int hwnd);
[DllImport("user32.dll", SetLastError = true)]private static extern int GetWindowLong(int hwnd, int wIndx);
[DllImport("user32.dll", SetLastError = true)]private static extern int SendMessage(int hwnd, int wMsg,int wParam,object Any);
[DllImport("user32.dll", EntryPoint ="SendMessageA", SetLastError = true)]private static extern int SendMessageByString(int hwnd,int wMsg,int wParam,string lParam);
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true,CharSet = CharSet.Auto)]private static extern uint PostMessage(int hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]private static extern int FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]private static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]private static extern int ShowWindow(int hwnd,int nCmdShow);
[DllImport("user32.dll")]private static extern int SetWindowPos(int hwnd, int hwndInsertAfter, int x, int y, int CX, int CY, int wflags);
[DllImport("KERNEL32")]private static extern int GetprivateProfileString(string lpApplicationName,object lpKeyName,string lpDefault,string lpReturnedString,int nSize,string lpFileName);
[DllImport("KERNEL32")]private static extern int WriteprivateProfileString(string lpApplicationName, object lpKeyName, object lpstring, string lpFileName);
//
[DllImport("winmm.dll")]private static extern long PlayFileMp3(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
private void Form1_Load(object sender, EventArgs e)
{
SetWindowPos(this.Handle.ToInt32(), 2, 0, 0, 0, 0, Flags);
MJWindow = FindWindow("#32770", "magicJack");
EditWnd = FindWindowEx(MJWindow, 0, "Edit", "");
MYDialBtn = FindWindowEx(MJWindow, 0, "SJSkinButton", "MY");
SetWindowPos(MJWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE);
ShowMJ();
if (MJWindow != 0){
btnHideMagic.Enabled = true;
btnCall.Enabled = true;
button3.Enabled = true;
}
else {
btnHideMagic.Enabled = false;
btnCall.Enabled = false;
button3.Enabled = false;
}
}
private void btnCall_Click(object sender, EventArgs e)
{
// if (MYDialBtn != 0) {
// ClickMJButton(MJWindow, "MWI");
SendCall(textBox1.Text,true);
// }
}
private void ClickMJButton(int hwnd,string Btn)
{
int MJBtn = FindWindowEx(hwnd, 0, "Button", Btn);
if (MJBtn!= 0){
PostMessage(MJBtn, WM_LBUTTONDOWN, 0, 0);
PostMessage(MJBtn, WM_LBUTTONUP, 0, 0);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
// string sCaption =(new StringBuilder(256)).ToString();
// int GetWinState;
// int TextLen ;
// int Syslist ;
// MJWindow = FindWindow("#32770", "magicJack");
//if(MJWindow != 0){
// btnCall.Enabled = true;
// btnHideMagic.Enabled = true;
// button3.Enabled = true;
// textBox1.Enabled = true;
// MYDialBtn = FindWindowEx(MJWindow, 0, "SJSkinButton", "MY");
// if(btnHideMagic.Text == "SHOW magicJack")
// {
// GetWinState = GetWindowLong(MJWindow, GWL_STYLE);
// if ( GetWinState == MaxState && ReadyState == true){
// ReadyState = false;
// ClickMJButton(MJWindow, "Minimize");
// }
// else if(GetWinState == MaxState){
// ReadyState = true;
// }
// }
// if(this.WindowState == 0){
// if(EditWnd == null){
// EditWnd = FindWindowEx(MJWindow, 0, "Edit","");
// }
// if(MYDialBtn == 0){
// SendMessageByString(Convert.ToInt32(EditWnd), WM_GETTEXT, 256,sCaption);
// if(sCaption != textBox1.Text)
// textBox1.Text = sCaption;
// }
// }
// }
// CallIDWindow = FindWindow("#32770", "magicJack - Incoming Call");
// if(btnHideMagic.Text == "SHOW Caller ID"){
// if( CallIDWindow != 0){
// ShowWindow(CallIDWindow, 0);
// if(Convert.ToInt32(PopUp) == IDYES)this.WindowState = System.Windows.Forms.FormWindowState.Normal;
// if( this.WindowState == 0){
// Syslist = FindWindowEx(CallIDWindow, 0, "Static", "");
// SendMessageByString(Syslist, WM_GETTEXT, 256, sCaption);
// }
// }
// }
// else{
// EditWnd = "";
// MaxState =0;
// btnHideMagic.Enabled = false;
// btnCall.Enabled = false;
// button3.Enabled = false;
// textBox1.Enabled = false;
// }
}
private void button3_Click(object sender, EventArgs e)
{
ClickMJButton(MJWindow, "&Hangup");
textBox1.Focus();
textBox1.Text = "";
pm.Stop();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//string TxtString;
string sCaption = (new StringBuilder(256)).ToString();
int i;
if(MYDialBtn != 0){
if(GetAsyncKeyState(VK_CONTROL)!=0 && GetAsyncKeyState(VK_RETURN)!=0){
//TxtString = ConvAtoN(textBox1.Text);
SendCall(textBox1.Text, true);
}
if(GetAsyncKeyState(VK_MENU)!=0 && GetAsyncKeyState(VK_RETURN)!=0){
SendCall(textBox1.Text, false);
}
if(GetAsyncKeyState(VK_CONTROL)!=0 && GetAsyncKeyState(VK_R)!=0){
for(i = 1;i< 24;i++){
SendMessageByString(Convert.ToInt32(EditWnd), WM_KEYDOWN, VK_DOWN, string.Empty);
SendMessageByString(Convert.ToInt32(EditWnd), WM_GETTEXT, 256, sCaption);
}
//Command3_Click
}
}
}
private void SendCall(string txt ,bool val)
{
if (txt == "" || txt == string.Empty) return;
SendMessageByString(Convert.ToInt32(EditWnd), WM_SETTEXT, 0, txt);
SendMessageByString(Convert.ToInt32(EditWnd), WM_KEYDOWN, VK_END, "");
SendMessageByString(Convert.ToInt32(EditWnd), WM_KEYUP, VK_END, "");
timer1.Interval = 2000;
pm.PlayLooping();
}
private string TrimText(string txt)
{
string x="";
int i;
for( i = 1;i < txt.Length;i++){
x = Mid(txt, i, 1);
if (IsNumeric(x) == true)
{
x = Mid(TrimText(x), 1, 11);
}
}
return x;
}
public string Mid(string textString,int Start ,int Length)
{
if (Start <= 0) {return null;}
if (Length > textString.Length - Start ) { return textString; }
textString = textString.Substring(Start-1, Length);
return textString;
}
public bool IsNumeric(object numberString)
{
char[] ca = numberString.ToString().ToCharArray();
for (int i = 0; i < ca.Length; i++)
{
if (!char.IsNumber(ca))
if (ca != '.')
return false;
}
if (numberString.ToString().Trim() == "")
return false;
return true;
}
//private string GetPhoneNum(int hwnd)
//{
// int Syslist;
// string sCaption = (new StringBuilder(256)).ToString();
// Do
// Syslist = FindWindowEx(hwnd, Syslist, "Static", vbNullString)
// Call SendMessageByString(Syslist, WM_GETTEXT, 256, sCaption.Value)
// Loop Until InStr(sCaption.Value, Chr(45)) <> 0
// GetPhoneNum = sCaption.Value
//}
private void btnHideMagic_Click(object sender, EventArgs e)
{
int ShellEm ;
int GoogleBtn;
if (MaxState == 0)
{
SetWindowPos(MJWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE);
ShowWindow(MJWindow, SW_NORMAL);
MaxState = GetWindowLong(MJWindow, GWL_STYLE);
GoogleBtn = FindWindowEx(MJWindow, 0, "Button", "F1");
if (GoogleBtn != 0)
{
PostMessage(GoogleBtn, WM_CLOSE, 0, 0);
}
do
{
ShellEm = FindWindowEx(MJWindow, 0, "Shell Embedding", string.Empty);
PostMessage(ShellEm, WM_CLOSE, 0, 0);
}
while (ShellEm > 0);
}
if( btnHideMagic.Text == "&Hide Magic Jack"){
SetWindowPos(MJWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE);
ReadyState = false;
btnHideMagic.Text = "SHOW magicJack";
}
else if (btnHideMagic.Text == "SHOW magicJack") {
ShowMJ();
btnHideMagic.Text= "&Hide Magic Jack";
}
}
private void ShowMJ(){
ShowWindow(MJWindow, SW_NORMAL);
SetWindowPos(MJWindow, HWND_NOTOPMOST, 0, 0, 2000, 2000, SWP_NOMOVE);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
ClickMJButton(MJWindow, "&Hangup");
textBox1.Focus();
textBox1.Text = "";
pm.Stop();
}
}
}