📄 剪切板示例.c
字号:
#include <windows.h>
#include <commdlg.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <commctrl.h>
#include <malloc.h>
#include "剪切板示例.h"
HWND hwndEdit,hStatusBar,hToolBar;
OPENFILENAME ofn;
HINSTANCE hInst;
HANDLE hFile,hText,hTextp;
LPTSTR lpBuffer,lpTextp, lpText;
RECT ClientRect,TBRect,SBRect;
DWORD dwFileRead,dwFileWrite;
int nIsNew=0,nFileExist=0,nLength=0,nWidth,nHeight,
nX=0,nY=0;
int RightEnd[2];
BOOL bReadFile,bWriteFile;
LPNMHDR lpnmhdr;
LPTOOLTIPTEXT lpttext;
TBBUTTON tbButton[]=
{
STD_FILENEW,IDM_NEW,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
STD_FILEOPEN,IDM_OPEN,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
STD_FILESAVE,IDM_SAVE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
STD_DELETE,IDM_CLOSE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0,0,0,0,
STD_CUT,IDM_CUT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
STD_COPY,IDM_COPY,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
STD_PASTE,IDM_PASTE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0,0,0,
};
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow)
{
HWND hwnd ;
MSG Msg ;
HACCEL hAccel;
WNDCLASS wndclass ;
char lpszMenuName[]="Menu";
char lpszClassName[] = "剪切板";
char lpszTitle[]= "剪切板示例";
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject( WHITE_BRUSH) ;
wndclass.lpszMenuName = lpszMenuName ;
wndclass.lpszClassName = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL) ;
ShowWindow( hwnd, nCmdShow) ;
UpdateWindow(hwnd);
InitCommonControls();
hInst=hInstance;
hAccel=LoadAccelerators(hInst,lpszMenuName);
while( GetMessage(&Msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hwnd,hAccel,&Msg))
{
TranslateMessage( &Msg) ;
DispatchMessage( &Msg) ;
}
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
static char lpszFilter[] = "TXT Files (*.txt)\0";
static char lpstrFileName[]="";
//int i=0;
switch(message)
{
case WM_CREATE:
//初始化通用对话框
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwndEdit ;
ofn.hInstance = hInst ;
ofn.lpstrFilter = NULL;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 1 ;
ofn.lpstrFile = NULL ;
ofn.nMaxFile = 0 ;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = NULL ;
ofn.lCustData = 0 ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
hToolBar=CreateToolbarEx(hwnd,
WS_CHILD|WS_BORDER|WS_VISIBLE
|TBSTYLE_TOOLTIPS,
ID_TOOLBAR,
8,
HINST_COMMCTRL,
IDB_STD_SMALL_COLOR,
tbButton,
8,
0,0,
0,0,
sizeof(TBBUTTON));
hStatusBar=CreateStatusWindow(
WS_CHILD|WS_VISIBLE,
"",
hwnd,
ID_STATUSBAR);
GetWindowRect(hToolBar,&TBRect);
GetWindowRect(hStatusBar,&SBRect);
GetClientRect(hwnd,&ClientRect);
nY=TBRect.bottom+ClientRect.bottom-SBRect.bottom;
nWidth=ClientRect.right;
nHeight=SBRect.top-TBRect.bottom;
RightEnd[0]=ClientRect.right*2/3;
RightEnd[1]=ClientRect.right;
SendMessage(hStatusBar,SB_SETPARTS,(WPARAM)2,
(LPARAM)RightEnd);
SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)0,
(LPARAM)"欢迎使用状态栏");
SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
(LPARAM)"应用程序已准备好");
hwndEdit=CreateWindow("edit",
NULL,
WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|WS_BORDER
|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,
nX,nY,
nWidth,nHeight,
hwnd,
(HMENU)IDE_EDIT,
hInst,
NULL) ;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_NEW:
SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
(LPARAM)"新建文件");
if (nFileExist==1)
{
MessageBox(hwnd,"请先关闭现有文件","提示",MB_OK);
break;
}
nIsNew=1;
break;
case IDM_OPEN:
SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
(LPARAM)"打开文件");
if (nFileExist==1)
{
MessageBox(hwnd,"请先关闭当前文件","提示",MB_OK);
break;
}
else
{
ofn.lpstrFilter=lpszFilter;
ofn.lpstrFileTitle=lpstrFileName;
ofn.lpstrTitle="打开";
ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|
OFN_HIDEREADONLY;
GetOpenFileName(&ofn);
hFile=CreateFile(lpstrFileName,GENERIC_READ|GENERIC_WRITE
,0,NULL,OPEN_EXISTING,0,NULL);
if ((DWORD)hFile==-1)
{
MessageBox(hwnd,"文件打开失败!",NULL,MB_OK);
break;
}
else
{
nFileExist=1;
nLength=GetFileSize(hFile,NULL);
if (nLength>32000)
{
MessageBox(hwnd,"文件太大,无法读取!","提示",MB_OK);
CloseHandle(hFile);
nFileExist=0;
break;
}
else
{
lpBuffer=(LPTSTR)malloc(nLength+1);
bReadFile=ReadFile(hFile,lpBuffer,nLength,
&dwFileRead,NULL);
if (bReadFile==FALSE)
{
MessageBox(hwnd,"文件读取失败!",NULL,MB_OK);
CloseHandle(hFile);
nFileExist=0;
free(lpBuffer);
break;
}
else
{
lpBuffer[nLength]='\0';
SetWindowText(hwndEdit,lpBuffer);
free(lpBuffer);
}
}
}
}
break;
case IDM_SAVE:
SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
(LPARAM)"保存文件");
if (nIsNew==1)
{
MessageBox(hwndEdit,"保存新文件请使用“另存为”","提示",MB_OK);
break;
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -