本文共 4170 字,大约阅读时间需要 13 分钟。
15windows_15_Edit编辑框
#include <windows.h>
#include <iostream>
#include "resource.h"
CHAR szText[256] = { 0 };
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
HWND g_hEdit = NULL; //保存Edit句柄
//WM_CREATE消息处理
void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//创建Edit文本编辑框
g_hEdit = CreateWindow("EDIT", "Hello Edit", WS_EX_CLIENTEDGE|WS_VSCROLL | WS_HSCROLL|WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_PASSWORD | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 100, 200, hWnd, (HMENU)1001, g_hInst, NULL);
}
//让Edit铺满整个客户区
void OnSize(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int nWidth = LOWORD(lParam);
int nHeight = HIWORD(lParam);
if (g_hEdit != NULL)
{
MoveWindow(g_hEdit, 0, 0, nWidth, nHeight, TRUE);
}
}
void OnSave()
{
//获取文字长度 WM_GETTEXTLENGTH 用这个消息`
LRESULT nTextLen = SendMessage(g_hEdit, WM_GETTEXTLENGTH, 0, 0);
//获取文字
CHAR *pszBuff = NULL;
pszBuff = (CHAR *)malloc(nTextLen + 1); //+1就是为了不越界
memset(pszBuff, 0, nTextLen + 1);
SendMessage(g_hEdit, WM_GETTEXT, nTextLen + 1 , (LPARAM)pszBuff);
//保存文件
FILE *pFile = NULL;
fopen_s(&pFile, "c:\\1.txt", "w+"); //打开文件
fwrite(pszBuff, nTextLen, 1, pFile); //把文件写进文件
fclose(pFile);
free(pszBuff);
}
void OnOpen()
{
//打开文件,读取数据
FILE *pFile = NULL;
fopen_s(&pFile, "c:\\1.txt", "r");
fseek(pFile, 0, SEEK_END);//把fseek移动到文件结尾
long nFileLen = ftell(pFile); //获得文件结尾的长度
fseek(pFile, 0, SEEK_SET); //把fseek指针移动到原来位置
CHAR *pszBuff = (CHAR*)malloc(nFileLen + 1); //多一个字符留给'\0'用
memset(pszBuff, 0, nFileLen + 1); //把pszBuff置零
fread(pszBuff, nFileLen, 1, pFile); //读取文件内容到buff
fclose(pFile);
//将字符显示在Edit窗口
SendMessage(g_hEdit, WM_SETTEXT, 0, (LPARAM)pszBuff);
}
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int nNotifyCode = HIWORD(wParam);//Ctrl ID
int nEventID = LOWORD(wParam); // 事件ID
switch (nEventID)
{
case 1001:
switch (nNotifyCode)
{
case EN_CHANGE: //内容发生变化
//MessageBox(NULL, "EN_CHANGE", "EN", MB_OK);
break;
}
break;
case ID_SELALL: //全部选择
//发送全选消息
SendMessage(g_hEdit, EM_SETSEL, 0, -1); //从第0个开始,到-1结尾
break;
case ID_COPY: //复制
SendMessage(g_hEdit, WM_COPY, 0, 0);
break;
case ID_PASTE: //粘贴消息
SendMessage(g_hEdit, WM_PASTE, 0, 0);
break;
case ID_SAVE:
OnSave();
break;
case ID_OPEN:
OnOpen();
break;
case ID_EXIT:
PostQuitMessage(0);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_CREATE:
OnCreate(hWnd, nMsg, wParam, lParam);
break;
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_SIZE:
OnSize(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wce);
if (atom == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
HMENU hMainMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
HWND hWnd = CreateWindowEx(0, pszClassName, "飞天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMainMenu, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
//ConsoleWnd();
RegisterWnd("oooo");
HWND hWnd = CreateWnd("oooo");
ShowWnd(hWnd);
Msg();
return 0;
}
转载于:https://www.cnblogs.com/nfking/p/5573175.html