|
|
2008年9月25日
2008年9月16日
// EmboConn.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ras.h"
HANDLE hPort = INVALID_HANDLE_VALUE;
HRASCONN hRasConn = NULL;
BOOL CreatePort()
{
DWORD dwNumBytesWritten;
BOOL fRet;
BYTE Bytes[]={'A','T','+','C','G','D','C','O','N','T','=','1',',','"','I','P','"',',','"','C','M','N','E','T','"',0x0D,'A','T','D','T','*','9','9','#',0x0D};
//open the serial port
hPort = CreateFile(TEXT("COM8:"),GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if ( hPort == INVALID_HANDLE_VALUE )
{
// Could not open the port.
//printf("Open the serial port failed\n\r");
return FALSE;
}
//BYTE Bytes[]={'A','T',0x0D};
//DWORD CgdcontSize = sizeof(Cgdcont);
DWORD BytesSize = sizeof(Bytes);
//fRet = WriteFile(hPort,Cgdcont,CgdcontSize, &dwNumBytesWritten,NULL);
fRet = WriteFile(hPort,Bytes,BytesSize, &dwNumBytesWritten,NULL);
fRet = ReadFile(hPort,Bytes,4,&dwNumBytesWritten,NULL);
if(fRet)
{
//printf("open the serial port successful \n\r");
CloseHandle(hPort);
}
else
{
//close the serial port
CloseHandle(hPort);
//printf("Close serial port successful\n\r");
}
return TRUE;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
RASCONNSTATE RasState = (RASCONNSTATE)wParam;
switch(Msg)
{
case WM_RASDIALEVENT:
switch(RasState)
{
case RASCS_OpenPort:
printf("正在打开端口\n\r");
break;
case RASCS_PortOpened:
printf("端口已经打开\n\r");
break;
case RASCS_ConnectDevice:
printf("正在连接设备\n\r");
break;
case RASCS_Connected:
//int i;
//printf("已连接\n\r");
//scanf("%d",i);
//if(i==1)
//{
// RasHangUp(hRasConn);
//}
break;
case RASCS_Disconnected:
printf("连接已断开\n\r");
break;
default:
printf("未知\n\r");
break;
}
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return TRUE;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = NULL;
wc.lpszMenuName = 0;
wc.lpszClassName = _T("embo");
RegisterClass(&wc);
hwnd = CreateWindow(_T("embo"),_T("EmboConnection"),0,0,0,0,0,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,0);
CreatePort();
RASDIALPARAMS rdParams;//拨号参数
//初始化拨号参数
rdParams.dwSize=sizeof(RASDIALPARAMSW);
wcscpy(rdParams.szEntryName,_T("Embo Connection"));
wcscpy(rdParams.szPhoneNumber,_T(""));
wcscpy(rdParams.szCallbackNumber,_T(""));
wcscpy(rdParams.szUserName,_T(""));
wcscpy(rdParams.szPassword,_T(""));
wcscpy(rdParams.szDomain,_T(""));
DWORD dwRet;
//拨号连接
//printf("connecting \n\r");
dwRet = RasDial(NULL,NULL,&rdParams,0xFFFFFFFF,hwnd ,&hRasConn);
if(dwRet != 0)//connection successful
{
RasHangUp(hRasConn);
}
while ( GetMessage(&msg, NULL, 0, 0) != FALSE )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
2008年8月17日
 Code
#region Windows Api 定义
///
/// 模拟鼠标事件
///
/// 鼠标事件的Enum
/// X座标
/// Y座标
///
///
[DllImport("user32.dll")]
public static extern void mouse_event (MouseEvents dwFlags,long dx, long dy,long cButtons,long dwExtraInfo);
///
/// 定义模拟鼠标的常数
///
public enum MouseEvents:uint
{
MOUSEEVENTF_LEFTDOWN = 0x2,
MOUSEEVENTF_LEFTUP = 0x4,
MOUSEEVENTF_MIDDLEDOWN = 0x20,
MOUSEEVENTF_MIDDLEUP = 0x40,
MOUSEEVENTF_MOVE = 0x1,
MOUSEEVENTF_ABSOLUTE = 0x8000,
MOUSEEVENTF_RIGHTDOWN = 0x8,
MOUSEEVENTF_RIGHTUP = 0x10,
}
///
/// 按指定标题寻找窗口,以获得指定窗口的句柄,为空则返回零。
///
/// 如未知则设置为NULL
/// 窗口名
/// 整型数据,代表窗口句柄
[DllImport("user32.dll")]
public static extern int FindWindow(String lpClassName,String lpWindowName);
///
/// 根据窗口句柄,获得窗口的类名
///
/// 窗口句柄
/// 缓冲区
/// 最大字节数
/// 整型,代表类名
[DllImport("user32.dll")]
public static extern int GetClassName(int hwnd,System.Text.StringBuilder buf, int nMaxCount);
///
/// 根据窗口句柄,获得指定窗口的标题。
///
/// 窗口句柄
/// 缓冲区
/// 最大字节数
/// 返回标题
[DllImport("user32.dll")]
public static extern int GetWindowText(int hwnd,System.Text.StringBuilder buf, int nMaxCount);
///
/// 根据窗口句柄,设置指定窗口为前置窗口。
///
/// 窗口句柄
[DllImport("user32.dll")]
public static extern void SetForegroundWindow(int hwnd);
///
/// 根据窗口句柄和显示的样式,显示窗口。
///
/// 窗口句柄
/// 显示窗口的样式。
/// 成功与否。
[DllImport("user32.dll")]
public static extern bool ShowWindow(int hWnd, nCmdShow nCmdShow);
public enum nCmdShow:uint
{
SW_FORCEMINIMIZE=0x0,
SW_HIDE=0x1,
SW_MAXIMIZE=0x2,
SW_MINIMIZE=0x3,
SW_RESTORE=0x4,
SW_SHOW=0x5,
SW_SHOWDEFAULT=0x6,
SW_SHOWMAXIMIZED=0x7,
SW_SHOWMINIMIZED=0x8,
SW_SHOWMINNOACTIVE=0x9,
SW_SHOWNA=0xA,
SW_SHOWNOACTIVATE=0xB,
SW_SHOWNORMAL=0xC,
}
///
/// 捕捉当前鼠标位置
///
/// 传入参数,代表鼠标的当前位置
///
[DllImport("user32.dll")]
public static extern long GetCursorPos (ref System.Drawing.Point lpPoint);
#endregion
2008年8月16日
 Code
private void getSome(DirectoryInfo dir)
{
progressBar1.Minimum = 1; //设置滚动条的最小值
FileSystemInfo[] file = dir.GetFileSystemInfos(); //获取文件夹内所有文件和文件夹
progressBar1.Maximum = file.Length;
foreach (FileSystemInfo fi in file)
{
if (fi is DirectoryInfo)
{
string directory = dir.FullName + @"\" + fi.Name;
try
{
DirectoryInfo info = new DirectoryInfo(directory);
info.Delete(true);
getSome(info);
}
catch
{
string path = Application.StartupPath + @"\" + "error.log";
StreamWriter w = new StreamWriter(path, true, Encoding.UTF8);
FileInfo f = new FileInfo(path);
if (f.Exists)
{
w.WriteLine("文件:" + directory + " 不能被删除");
w.Close();
}
else
{
File.Create(path);
}
continue;
}
}
else
{
string filename = dir.FullName + @"\" + fi.Name;
try
{
label1.Text = filename;
//File.Delete(filename);
progressBar1.PerformStep();
Thread.Sleep(100);
}
catch
{
string path = Application.StartupPath + @"\" + "error.log";
StreamWriter w = new StreamWriter(path, true, Encoding.UTF8);
FileInfo f = new FileInfo(path);
if (f.Exists)
{
w.WriteLine("文件:" + filename + " 不能被删除");
w.Close();
}
else
{
File.Create(path);
}
continue;
}
}
}
}
这个方法主要是遍历所选文件夹下所有的文件和文件夹,最好使用线程启动,不然文件多的话可能出现假死的状态。
希望对大家有所帮助,我是新手,但我还是会把我每天做的东西写上来的。希望能够慢慢的提高自己的水平。现在工作可不好找。
2008年8月15日
 Code
索引器在语法上方便您创建客户端应用程序可将其作为数组访问的类、结构或接口。索引器经常是在主要用于封装内部集合或数组的类型中实现的。例如,假定具有一个名为 TempRecord 的类,此类表示在 24 小时内的 10 个不同时间记录的华氏度。此类包含一个表示温度的 float 类型的名为“temps”的数组和表示记录温度的日期的 DateTime。通过在此类中实现一个索引器,客户端可以通过 float temp = tr[4] 而不是 float temp = tr.temps[4] 语法访问 TempRecord 实例中的温度。索引器表示法不仅简化了客户端应用程序的语法,还使其他开发人员能够更加直观地理解类及其用途。
要声明类或结构上的索引器,请使用 this 关键字,如下例所示:
复制代码
public int this[int index] // Indexer declaration
{
// get and set accessors
}
备注
索引器类型及其参数类型必须至少如同索引器本身一样是可访问的。有关可访问级别的更多信息,请参见访问修饰符。
有关如何对接口使用索引器的更多信息,请参见接口索引器。
索引器的签名由其形参的数量和类型组成。它不包括索引器类型或形参名。如果在同一类中声明一个以上的索引器,则它们必须具有不同的签名。
索引器值不属于变量;因此,不能将索引器值作为 ref 或 out 参数进行传递。
要为索引器提供一个其他语言可以使用的名字,请使用声明中的 name 属性。例如:
复制代码
[System.Runtime.CompilerServices.IndexerName("TheItem")]
public int this [int index] // Indexer declaration
{
}此索引器将具有名称 TheItem。不提供名称属性将生成 Item 默认名称。
示例 1
说明
下面的示例说明如何声明私有数组字段、temps 和索引器。使用索引器可直接访问实例 tempRecord[i]。另一种使用索引器的方法是将数组声明为 public 成员并直接访问它的成员 tempRecord.temps[i]。
请注意,当计算索引器的访问时(例如,在 Console.Write 语句中),将调用 get 访问器。因此,如果 get 访问器不存在,将发生编译时错误。
代码
C# 复制代码
class TempRecord
{
// Array of temperature values
private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F,
61.3F, 65.9F, 62.1F, 59.2F, 57.5F };
// Auto-Implemented Property
System.DateTime date { get; set; }
// To enable client code to validate input
// when accessing your indexer.
public int Length
{
get { return temps.Length; }
}
// Indexer declaration.
// Input parameter is validated by client
// code before being passed to the indexer.
public float this[int index]
{
get
{
return temps[index];
}
set
{
temps[index] = value;
}
}
}
class MainClass
{
static void Main()
{
TempRecord tempRecord = new TempRecord();
// Use the indexer's set accessor
tempRecord[3] = 58.3F;
tempRecord[5] = 60.1F;
// Use the indexer's get accessor
for (int i = 0; i < 10; i++)
{
// This example validates the input on the client side. You may
// choose to validate it in the class that implements the indexer, and throw an
// exception or return an error code in the case of invalid input.
if (i < tempRecord.Length)
{
System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
}
else
{
System.Console.WriteLine("Index value of {0} is out of range", i);
}
}
//Uncomment this code to see how the .NET Framework handles indexer exceptions
//try
//{
// System.Console.WriteLine("Element #{0} = {1}", tempRecord[tempRecord.Length]);
//}
//catch (System.ArgumentOutOfRangeException e)
//{
// System.Console.WriteLine(e);
//}
}
}
使用其他值进行索引
C# 并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类索引器。由于访问器可被重载,字符串和整数版本可以共存。
示例 2
说明
在此例中,声明了存储星期几的类。声明了一个 get 访问器,它接受字符串(天名称),并返回相应的整数。例如,星期日将返回 0,星期一将返回 1,等等。
代码
C# 复制代码
// Using a string as an indexer value
class DayCollection
{
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
// This method finds the day or returns -1
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in days)
{
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}
// The get accessor returns an integer for a given string
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week["Fri"]);
System.Console.WriteLine(week["Made-up Day"]);
}
}
输出
5
-1
可靠编程
提高索引器的安全性和可靠性有两种主要的方法:
确保结合某一类型的错误处理策略,以处理万一客户端代码传入无效索引值的情况。在本主题前面的第一个示例中,TempRecord 类提供了 Length 属性,使客户端代码能够在将输入传递给索引器之前对其进行验证。也可以将错误处理代码放入索引器自身内部。确保为用户记录在索引器的访问器中引发的任何异常。有关更多信息,请参见异常设计准则。
应当为 get 和 set 访问器的可访问性设置尽可能多的限制。这一点对 set 访问器尤为重要。有关更多信息,请参见非对称访问器可访问性(C# 编程指南)。
|