📄 chatclient.c
字号:
#include <windows.h>
#include "resource.h"
#include "time.h"
#define ID_RECEIVEEDIT 1
#define ID_SENDEDIT 2
#define ID_BUTTONSEND 3
#define ID_BUTTONRECVFILE 4
#define IDC_MAIN_EDIT 5
#define ID_SERVPORT 6
#define ID_BUTTONCONN 7
#define ID_BUTTONSENDFILE 8
#define SERVER_PORT 1111
#define WM_SOCKET (WM_USER+1)
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char SERVER_ADDR[50]="";
const int ID_TIMER = 1;
const int BALL_MOVE_DELTA = 2;//小球每次移动的距离
typedef struct _BALLINFO
{
int width;
int height;
int x;
int y;
int dx;
int dy;
}BALLINFO;
BALLINFO g_ballInfo;
HBITMAP g_hbmBall = NULL;
HBITMAP g_hbmMask = NULL;
//创建位图
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, hbmMask);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbmMask;
}
//显示小球
void DrawBall(HDC hdc, RECT* prc)
{
HBRUSH hbrush=CreateSolidBrush(RGB(100,180,100));//设置小球背景
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = SelectObject(hdcMem, g_hbmMask);
FillRect(hdcBuffer,prc,hbrush);
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width,
g_ballInfo.height, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, g_hbmBall);
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
}
//更新小球
void UpdateBall(RECT* prc)
{
g_ballInfo.x += g_ballInfo.dx;
g_ballInfo.y += g_ballInfo.dy;
if(g_ballInfo.x < 0)
{
g_ballInfo.x = 0;
g_ballInfo.dx = BALL_MOVE_DELTA;
}
else if(g_ballInfo.x + g_ballInfo.width > prc->right)
{
g_ballInfo.x = prc->right - g_ballInfo.width;
g_ballInfo.dx = -BALL_MOVE_DELTA;
}
if(g_ballInfo.y < 0)
{
g_ballInfo.y = 0;
g_ballInfo.dy = BALL_MOVE_DELTA;
}
else if(g_ballInfo.y + g_ballInfo.height > prc->bottom)
{
g_ballInfo.y = prc->bottom - g_ballInfo.height;
g_ballInfo.dy = -BALL_MOVE_DELTA;
}
}
//帮助对话框显示
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpcmdLine, int nCmdShow)
{
static TCHAR szAppName[]= TEXT("ChatClient");
static TCHAR szClassName[]= TEXT("ChatClientClass");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);//LoadIcon( NULL, IDI_APPLICATION );
wndclass.hCursor = LoadCursor( NULL,IDC_ARROW );
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndclass.lpszClassName = szClassName;
if ( !RegisterClass( &wndclass ) )
{
MessageBox( NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR );
return 0;
}
hwnd = CreateWindow( szClassName, TEXT("Simple WinSock Chat Client"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL );
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
while ( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HINSTANCE hInstance;
static HWND hwndReceiveEdit, hwndSendEdit,hwndButtonSend,hwndButtonRecvFile,
hwndServPort,hwndButtonConn,hwndSendFile;
WSADATA WSAData;
static struct sockaddr_in server;
static SOCKET remote_server;
int nError;
WORD wEvent, wError;
static byte szReceive[100000],szChar[100000];
static TCHAR szSend[10000];
DWORD dwWrite,dwRead,dwFileSize;
HANDLE hf;
static int cxClient,cyClient;
static HPEN hpen;
static HRGN hrgn,hrgnx,hrgny;
int x,y;
//接收文件句柄
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
//发送文件句炳
HANDLE hfsend;
OPENFILENAME sendfile; // common dialog box structure
char szFile[260]="";
char szShow[1000]="",tbuffer[10];
// HBRUSH hbrush;
UINT ret;
BITMAP bm;
RECT rcClient;
//选定客户区
rcClient.bottom=600;
rcClient.top=0;
rcClient.left=0;
rcClient.right=200;
//send file
ZeroMemory(&sendfile, sizeof(OPENFILENAME));
sendfile.lStructSize = sizeof(OPENFILENAME);
sendfile.hwndOwner = hwnd;
sendfile.lpstrFile = szFile;
sendfile.nMaxFile = sizeof(szFile);
sendfile.nFilterIndex = 1;
sendfile.lpstrFileTitle = NULL;
sendfile.nMaxFileTitle = 0;
sendfile.lpstrInitialDir = NULL;
sendfile.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST|OFN_EXPLORER;
sendfile.nFileOffset=0;
sendfile.nFileExtension=0;
sendfile.lpstrDefExt=NULL;
sendfile.lCustData=0;
sendfile.lpfnHook=NULL;
//recv file
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
switch( message )
{
case WM_CREATE:
//选入小球
g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
if(g_hbmBall == NULL)
MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0));
if(g_hbmMask == NULL)
MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
GetObject(g_hbmBall, sizeof(bm), &bm);
ZeroMemory(&g_ballInfo, sizeof(g_ballInfo));
g_ballInfo.width = bm.bmWidth;
g_ballInfo.height = bm.bmHeight;
g_ballInfo.dx = BALL_MOVE_DELTA;
g_ballInfo.dy = BALL_MOVE_DELTA;
ret = SetTimer(hwnd, ID_TIMER, 50, NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -