#include<windows.h>
#include"resource.h"
#include<stdio.h>
#include <process.h>
HINSTANCE hInst;
#define IDC_MAIN_EDIT 101
char* myBaud="CBR_19200";
char* myCom="COM2";
char sString[50]={"串口通信——"};
int n=1;
HWND hwnd;
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
LRESULT CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK Option(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
BOOL LoadTextFileToEdit(HWND hEdit ,LPCTSTR pszFileName);
BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName);
BOOL LoadMusicFiles(LPCTSTR pszFileName);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASSEX wndclass;
hInst=hInstance;
wndclass.cbClsExtra=NULL;
wndclass.cbWndExtra=NULL;
wndclass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinSunProc;
wndclass.lpszClassName="xuejb";
wndclass.lpszMenuName=(LPCTSTR)(IDR_MENU1);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.hIconSm=LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wndclass.cbSize=sizeof(WNDCLASSEX);
RegisterClassEx(&wndclass);
hwnd=CreateWindow("xuejb","串口通信",WS_OVERLAPPEDWINDOW,350,150,
600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
int wmId,wmEvent;
static int cxChar, cyChar ;
HWND hButton[5],hBitmap;
case WM_CREATE:
HFONT hfDefault;
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
cxChar = LOWORD (GetDialogBaseUnits ()) ;
cyChar = HIWORD (GetDialogBaseUnits ()) ;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
ES_AUTOVSCROLL | ES_AUTOHSCROLL ,
0, 0,100, 100,hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL),
NULL);
hButton[0]=CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "接收",
WS_CHILD | WS_VISIBLE ,
cxChar,cyChar,10*cxChar, 2*cyChar,hwnd, (HMENU)IDC_BUTTON, GetModuleHandle(NULL),
NULL);
hButton[1]=CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "发送",
WS_CHILD | WS_VISIBLE ,
cxChar,cyChar*3,10*cxChar, 2*cyChar,hwnd, (HMENU)IDC_BUTTON1, GetModuleHandle(NULL),
NULL);
hButton[2]=CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "选项",
WS_CHILD | WS_VISIBLE ,
cxChar,cyChar*5,10*cxChar, 2*cyChar,hwnd, (HMENU)IDC_BUTTON2, GetModuleHandle(NULL),
NULL);
hButton[3]=CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "打开串口",
WS_CHILD | WS_VISIBLE ,
cxChar,cyChar*7,10*cxChar, 2*cyChar,hwnd, (HMENU)IDC_BUTTON3, GetModuleHandle(NULL),
NULL);
hBitmap=CreateWindowEx(WS_EX_CLIENTEDGE, "STATIC", "串口通信工具 Designed by: 薛加彬 QQ: 798216108",
WS_CHILD | WS_VISIBLE ,
cxChar,cyChar*10,10*cxChar, 10*cyChar,hwnd, (HMENU)IDC_STATIC , GetModuleHandle(NULL),
NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK |MB_ICONERROR);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
strcat(sString,myCom);
strcat(sString,",");
strcat(sString,myBaud);
SetWindowText(hwnd,sString);
break;
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 100, 0, rcClient.right, rcClient.bottom,
SWP_NOZORDER);
}
break;
case WM_COMMAND:
wmId=LOWORD(wParam);
wmEvent=HIWORD(wParam);
switch(wmId)
{
case IDM_OPEN:
{
OPENFILENAME ofn;
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
LoadTextFileToEdit(hEdit ,szFileName);
}
}
break;
case IDM_SAVE:
{
OPENFILENAME ofn;
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetSaveFileName(&ofn))
{
SaveTextFileFromEdit(hEdit, szFileName);
}
}
break;
case IDM_NEW:
{
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowText(hEdit,"");
}
break;
case IDC_BUTTON3:
//hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
break;
case IDC_BUTTON:
case IDM_REC:
{
LPSTR szRec;
COMSTAT Rcs;
HANDLE hCom;
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
hCom=CreateFile(myCom,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hCom!=INVALID_HANDLE_VALUE)
{
SetupComm(hCom,1024,512);
DCB myDCB;
GetCommState(hCom,&myDCB);
myDCB.BaudRate=*myBaud;
myDCB.fBinary=TRUE;
myDCB.fParity=TRUE;
myDCB.ByteSize=8;
myDCB.Parity=NOPARITY;
myDCB.StopBits=ONESTOPBIT;
SetCommState(hCom,&myDCB);
//MessageBox(hwnd,"正在接收!","提示",NULL);
DWORD dwRead;
szRec=(char*)GlobalAlloc(GPTR, 3);
if(ReadFile(hCom,szRec,Rcs.cbInQue,&dwRead,NULL)!=0)
{
szRec[Rcs.cbInQue]=0;
SetWindowText(hEdit,szRec);
MessageBox(hwnd,"接收成功!","提示",NULL);
}
CloseHandle(hCom);
GlobalFree(szRec);
}
else
{
MessageBox(hwnd,"串口不存在!",NULL,NULL);
}
}
break;
case IDC_BUTTON1:
case IDM_TRAN:
{
HANDLE hCom;
LPSTR szTran;
DWORD dwTextLength;
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
dwTextLength = GetWindowTextLength(hEdit);
hCom=CreateFile(myCom,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hCom!=INVALID_HANDLE_VALUE)
{
SetupComm(hCom,1024,512);
DCB myDCB;
GetCommState(hCom,&myDCB);
myDCB.BaudRate=*myBaud;
myDCB.fBinary=TRUE;
myDCB.fParity=TRUE;
myDCB.ByteSize=8;
myDCB.Parity=NOPARITY;
myDCB.StopBits=ONESTOPBIT;
SetCommState(hCom,&myDCB);
//MessageBox(hwnd,"正在发送!","提示",NULL);
DWORD dwWrite;
if(dwTextLength)
{
szTran= (char*)GlobalAlloc(GPTR, dwTextLength+1);
if(GetWindowText(hEdit, szTran, dwTextLength+1))
{
if(WriteFile(hCom,szTran,dwTextLength+1,&dwWrite,NULL))
MessageBox(hwnd,"发送成功!","提示",NULL);
}
}
else
MessageBox(hwnd,"请输入要发送的数据","提示",NULL);
CloseHandle(hCom);
GlobalFree(szTran);
}
else
{
MessageBox(hwnd,"串口不存在!",NULL,NULL);
}
}
break;
case IDC_BUTTON2:
case IDM_OPT:
DialogBox(hInst,(LPCTSTR)IDD_DIALOG3,hwnd,(DLGPROC)Option);break;
cas