#pragma warning(disable:4996)
#include<windows.h>
#include<string>
#include<fstream>
#define HDC_EDIT_1 0x100
#define HDC_BUTTON_1 0x101
#define HDC_BUTTON_2 0x102
//全局變量聲明
HINSTANCE hinst;
//函數聲明
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
char * wchar2char(const wchar_t* wchar);
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //HINSTANCE為應用程序的句柄
WNDCLASSEX wcx; //窗口類
HWND hwnd; //窗口句柄
MSG msg; //消息
BOOL fGotMessage; //是否成功獲取消息
hinst = hinstance; //用來保存當前的應用程序的句柄
static TCHAR szAppName[] = "MyWindows";
//對創建的窗口類進行填充相應的數據結構
wcx.cbSize = sizeof(wcx); //cxSize轉到定義為 該類型為UINT
wcx.style = CS_HREDRAW | CS_VREDRAW; //樣式 大小改變時 重新進行繪制
wcx.lpfnWndProc = MainWndProc; // 窗口消息處理函數
wcx.cbWndExtra = 0; // 不使用類內存
wcx.cbClsExtra = 0; // 不使用窗口內存
wcx.hInstance = hinstance; //所屬的應用程序的實例句柄
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION); //圖標: 默認 指定一個和類相關的圖標資源句柄,如果沒有指定就用默認的。
wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // 光標:默認
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 畫刷背景:WHITE_BRUSH
wcx.lpszMenuName = NULL; // 菜單:無
wcx.lpszClassName = szAppName; // 窗口類的名稱
wcx.hIconSm = (HICON)LoadImage(hinstance, //hIconSm指定一個和類相關的小的圖標資源句柄,如果是空,系統會根據hIcon的圖標來生成一個合適大小的圖標來作為和類相關的小的圖標資源句柄
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CXSMICON),
LR_DEFAULTCOLOR);
if (!RegisterClassEx(&wcx)) { //創建窗口類
return -1;
}
//調用CreateWindow API
hwnd = CreateWindow(szAppName, //窗口類名稱
"First Window", //窗口標題
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, //水平位置:默認
CW_USEDEFAULT, //垂直位置:默認
CW_USEDEFAULT, //寬度位置:默認
500, //高度位置:默認
(HWND)NULL, // 父窗口:無
(HMENU)NULL, //菜單:使用窗口類的菜單
hinstance, //應用程序實例句柄
(LPVOID)NULL); //窗口創建時數據:無
CreateWindow(
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE,
0,
0,
800,
500,
hwnd,
(HMENU)HDC_EDIT_1, //需要進行強轉類型
hinstance,
(LPVOID)NULL
);
CreateWindow(
"BUTTON",
"選擇文件",
WS_CHILD | WS_VISIBLE,
900,
200,
100,
50,
hwnd,
(HMENU)HDC_BUTTON_1, //需要進行強轉類型
hinstance,
(LPVOID)NULL
);
CreateWindow(
"BUTTON",
"保存文本",
WS_CHILD | WS_VISIBLE,
900,
260,
100,
50,
hwnd,
(HMENU)HDC_BUTTON_2, //需要進行強轉類型
hinstance,
(LPVOID)NULL
);
if (!hwnd) { //創建窗口失敗的處理
return -1;
}
//顯示窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//消息循環,作用就是將我們在窗口上產生的msg進行TranslateMessage解析然后再進行DispatchMessage傳輸給窗口消息處理函數進行處理
while ((fGotMessage = GetMessage(&msg, (HWND)NULL, 0, 0)) != 0 && fGotMessage != -1) {
TranslateMessage(&msg); //翻譯作用,比如 在處理按鍵的時候,將接收到的十六進制轉換為字符碼char的時候就可以派上用場了
DispatchMessage(&msg); //傳輸,根據對應的窗口HWND,找到對應的窗口過程函數,比如這里定義的MainWndProc函數進行處理
//當DispatchMessage之后,內核中將該句柄對應的窗口過程函數進行調用
}
return msg.wParam;
}
/*
MainWndProc
功能:窗口消息處理函數 對所有的消息都使用默認處理函數
*/
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { //這里接收的hwnd,uMsg,wParam,lParam
//也就是MSG結構體中的消息,umsg是動作 wParam lParam保存了該動作中的詳細信息的,hwnd為該句柄
TCHAR szchar[100] = {0};
OPENFILENAME ofn;
TCHAR szfile[MAX_PATH]; //用來存儲文件名
std::fstream fs;
std::string mystring;
char szBuf[2048] = {0};
switch (uMsg) { //switch對產生的消息進行處理
case WM_DESTROY: //當進行關閉處理時候 進行ExitThread 結束進程操作
PostQuitMessage(0);
return 0;
case WM_COMMAND: // 十六進制為0x111
switch (wParam) {
case 0x101: //第一個按鈕
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szfile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szfile);
ofn.lpstrFilter = "All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)) {
fs.open(ofn.lpstrFile, std::fstream::in);
while (fs.getline(szBuf, 1024)) {
mystring.append(szBuf);
mystring.append("\r\n");
}
fs.close();
SetDlgItemText(hwnd, HDC_EDIT_1, mystring.c_str());
}
else {
DWORD myDword = CommDlgExtendedError();
sprintf(szchar, "error : %d", myDword);
OutputDebugString(szchar);
}
break;
case 0x102: //第二個按鈕
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szfile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szfile);
ofn.lpstrFilter = "All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)) {
fs.open(ofn.lpstrFile, std::fstream::out)
GetDlgItemText(hwnd, HDC_EDIT_1, szBuf, 2048)
fs.write(szBuf, sizeof(szBuf))
fs.close();
break;
}
default:
break;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam); //不關心的操作都給windows自己的窗口消息處理函數進行處理
}
}
