1. GDI+双缓冲绘图
DateTime t1 = DateTime.Now;
Bitmap bmp = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bmp);
if(flag)
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Red, Color.Blue);
flag = false;
}
else
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Blue, Color.Red);
flag = true;
}
for(int j = 0; j < 60; j ++)
{
for(int i = 0; i < 60; i++)
{
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
}
}
this.CreateGraphics().DrawImage(bmp, 0, 0);
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
this.label1.Text = "速度:" + per.ToString() + "帧/秒";
2. 绘制圆角矩形
BOOL yourClass::pathAddRoundRecangle(
GraphicsPath *path ,
RectF rect ,
float roundSize )
{
float left = rect.X;
float top = rect.Y;
float right = rect.Width + rect.X;
float bottom = rect.Height + rect.Y;
//如果取值不合法,指定roundSize为10或较短边的1/4
if( roundSize <= 0 )
roundSize = 10;
float shortOne = rect.Width < rect.Height ? rect.Width : rect.Height;
if( roundSize > shortOne / 4 )
roundSize = shortOne / 4;
//左上角
path->AddArc( left , top , 2 * roundSize , 2 * roundSize , 180 , 90 );
//上横线
path->AddLine( left + roundSize , top , right - roundSize , top );
//右上角
path->AddArc( right - 2 * roundSize , top , 2 * roundSize , 2 * roundSize , 270 , 90 );
//右竖线
path->AddLine( right , top + roundSize , right , bottom - roundSize );
//右下角
path->AddArc( right - 2 * roundSize , bottom - 2 * roundSize , 2 * roundSize , 2 * roundSize , 0 , 90 );
//下横线
path->AddLine( left + roundSize , bottom , right - roundSize , bottom );
//左下角
path->AddArc( left , bottom - 2 * roundSize , 2 * roundSize , 2 * roundSize , 90 , 90 );
//左竖线
path->AddLine( left , top + roundSize , left , bottom - roundSize );
return TRUE;
}
2. 网络
//取得本机名称
int CFileSendDlg::GetLocalHostName(CString &sHostName)
{
char szHostName[256];
int code;
code = gethostname(szHostName, sizeof(szHostName));
if(code != 0)
{
sHostName = "无法取得本机名称";
return GetLastError();
}
sHostName = szHostName;
return 0;
}
//由计算机名取得本机IP地址
int CFileSendDlg::GetLocalIpByName(const CString &sHostName, CString &sIpAddress)
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
//产生错误
sIpAddress=_T("");
return GetLastError();
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
sIpAddress=inet_ntoa(inAddr);
if(sIpAddress.IsEmpty())
sIpAddress=_T("没有取得");
}
return 0;
}
int CFileSendDlg::GetNameByIp(const CString &IpAddress, CString &Name)
{
unsigned long addr;
addr=inet_addr(IpAddress);
struct hostent FAR * lpHostEnt=gethostbyaddr((char *)&addr,4,AF_INET);
if(lpHostEnt==NULL)
{
//产生错误
Name=_T("");
AfxMessageBox("无法获取计算机名");
return -1;
}
CString name=lpHostEnt->h_name;
Name=name;
return 0;
}
int CServerSocket::Readn(char *bp, int len)
{
int cnt;
int rc;
cnt = len;
while (cnt > 0)
{
rc = Receive(bp, cnt, 0);
if (rc < 0)
return -1;
if (rc == 0)
return len - cnt;
bp += rc;
cnt -= rc;
}
return len;
}
int CServerSocket::Writen(const char *bp, int len)
{
int cnt;
int rc;
cnt = len;
while (cnt > 0)
{
rc = Send(bp, cnt, 0);
if (rc < 0)
return -1;
if (rc == 0)
return len - cnt;
bp += rc;
cnt -= rc;
}
return len;
}
if(inet_addr(servername)==INADDR_NONE)
{
hp=gethostbyname(servername);
}
else { addr=inet_addr(servername); hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}
if(hp==NULL)
{
closesocket(conn);
return;
}
一段优秀代码
UINT ServerThread(LPVOID pParam)
{
cout << "Starting up TCP server/r/n";
//A SOCKET is simply a typedef for an unsigned int.
//In Unix, socket handles were just about same as file
//handles which were again unsigned ints.
//Since this cannot be entirely true under Windows
//a new data type called SOCKET was defined.
SOCKET server;
//WSADATA is a struct that is filled up by the call
//to WSAStartup
WSADATA wsaData;
//The sockaddr_in specifies the address of the socket
//for TCP/IP sockets. Other protocols use similar structures.
sockaddr_in local;
//WSAStartup initializes the program for calling WinSock.
//The first parameter specifies the highest version of the
//WinSock specification, the program is allowed to use.
int wsaret=WSAStartup(0x101,&wsaData);
//WSAStartup returns zero on success.
//If it fails we exit.
if(wsaret!=0)
{
return 0;
}
//Now we populate the sockaddr_in structure
local.sin_family=AF_INET; //Address family
local.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
local.sin_port=htons((u_short)20248); //port to use
//the socket function creates our SOCKET
server=socket(AF_INET,SOCK_STREAM,0);
//If the socket() function fails we exit
if(server==INVALID_SOCKET)
{
return 0;
}
//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
{
return 0;
}
//listen instructs the socket to listen for incoming
//connections from clients. The second arg is the backlog
if(listen(server,10)!=0)
{
return 0;
}
//we will need variables to hold the client socket.
//thus we declare them here.
SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);
while(true)//we are looping endlessly
{
char temp[512];
//accept() will accept an incoming
//client connection
client=accept(server,
(struct sockaddr*)&from,&fromlen);
sprintf(temp,"Your IP is %s/r/n",inet_ntoa(from.sin_addr));
//we simply send this string to the client
send(client,temp,strlen(temp),0);
cout << "Connection from " << inet_ntoa(from.sin_addr) <<"/r/n";
//close the client socket
closesocket(client);
}
//closesocket() closes the socket and releases the socket descriptor
closesocket(server);
//originally this function probably had some use
//currently this is just for backward compatibility
//but it is safer to call it as I still believe some
//implementations use this to terminate use of WS2_32.DLL
WSACleanup();
return 0;
}
创建多级目录
/** 检查目录是否存在, 如果不存在, 则创建目录 */
#include <io.h>
#include <Mmsystem.h>
#pragma comment(lib, "winmm.lib")
BOOL CRecordSession::CreatePath(LPCTSTR lpszPathName)
{
if (_access(lpszPathName, 0) != -1)
return TRUE;
typedef int (WINAPI *SHCREATEDIRECTORYEX)(HWND, LPCTSTR, SECURITY_ATTRIBUTES *);
SHCREATEDIRECTORYEX SHCreateDirectoryEx = NULL;
BOOL bCreated = FALSE;
HMODULE hModule = LoadLibrary("shell32.dll");
if (hModule)
{
SHCreateDirectoryEx = (SHCREATEDIRECTORYEX)GetProcAddress(hModule, "SHCreateDirectoryExA");
if (SHCreateDirectoryEx)
bCreated = (SHCreateDirectoryEx(NULL, lpszPathName, NULL) == ERROR_SUCCESS);
FreeLibrary(hModule);
}
return bCreated;
}
/** 删除目录及文件 */
CHAR path[256] = "/0";
::GetTempPath(256,path);
strcat(path, "emap");
SHFILEOPSTRUCT ops;
ZeroMemory(&ops, sizeof(ops));
ops.wFunc = FO_DELETE;
ops.pFrom = path;
ops.fFlags = FOF_NOCONFIRMATION| FOF_NOERRORUI;
DWORD dwErr = 0;
TRY
{
SetLastError(0);
SHFileOperation(&ops);
}
CATCH_ALL(e)
{
char buff[MAX_PATH];
e->GetErrorMessage(buff, sizeof(buff));
_snprintf(buff, sizeof(buff), "Delete Temp Path Failed: %s", buff);
TRACE(buff);
}
END_CATCH_ALL