⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mian.cpp

📁 用win32sdk实现的迷你阅读器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include"main.h"


#define		nWndHeight	500
#define		nWndWidth	600



CHAR	szAppName[]	   = "MyReader";
CHAR	szClassName[]  = "MyReaderClass";

int		numline=0;

COLORREF fgcolor = RGB(255,255,0); //前景色
COLORREF bgcolor = RGB(100,20,100); //背景色
COLORREF colors[16];
HFONT g_hfFont;//= GetStockObject(DEFAULT_GUI_FONT);
LOGFONT lf; 


HINSTANCE		Instance;
HWND			hTree;
HWND			hStr;
HWND			hWinToolbar;

BSTTREE			*MySTRTree;
LPCTSTR			TmpStr="";
BOOL			split;
UINT			CurWidth=nWndWidth;
UINT			BarLeft;
UINT			BarRight;
WNDPROC			OldEditProc;
BOOL			ScrollCheck;

HMENU		hSysMenu;
HMENU		hMenu;
HMENU		hRMenu;
const char* szMenuAbout="关于本程序(&A)...";
//HINSTANCE	hInst;
//HWND		hList;
//HWND		hTree;
TBBUTTON tbButtons[] = { 
		{0,IDM_FILEOPEN ,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0,0},
		{1,IDM_ZOOMIN,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{2,IDM_ZOOMOUT,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0,0},
		{3,IDM_PRVPAGE,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{4,IDM_NEXTPAGE,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0,0},
		{5,IDM_FONT,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{6,IDM_BGCOLOR,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
		{0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0,0},
		{7,IDM_ABOUT,TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0}
}; 


void CenterWindow(HWND hWnd)
{
	HWND hWndOwner;
	RECT rc, rc2;
	int	x,y;

	if((hWndOwner=GetParent(hWnd))==NULL){
		SystemParametersInfo(SPI_GETWORKAREA,0,&rc,0);
	}else{
		GetClientRect(hWndOwner, &rc);
	}
	GetWindowRect(hWnd, &rc2);
	x = ((rc.right-rc.left) - (rc2.right-rc2.left)) / 2 +rc.left;
	y = ((rc.bottom-rc.top) - (rc2.bottom-rc2.top)) / 2 +rc.top;

	SetWindowPos(hWnd,HWND_TOP,x, y,0, 0,SWP_NOSIZE);
}

void SetScrollLength()
{
		HDC hStrDC=GetDC(hStr);
	//	HFONT hF_black;
	//	hF_black = CreateFont(30, 0, 0, 0,FW_BOLD, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, "楷体");
	//	SelectObject(hStrDC,hF_black);
		SelectObject(hStrDC,g_hfFont);
		TEXTMETRIC tm;
		GetTextMetrics(hStrDC,&tm);
		int cyChar=tm.tmHeight+tm.tmExternalLeading;
		RECT rc,rc1;
		GetClientRect(hStr,&rc);
		rc1=rc;
	 //   rc1.bottom=rc.bottom;
	//	rc1.top=rc.top;
//		rc1.right =rc.right; 
//		rc1.left=rc.left ;


	//	rc.right =rc.right -20;
		DrawText(hStrDC,TmpStr,lstrlen(TmpStr),&rc,DT_CALCRECT|DT_WORDBREAK);
		numline=(rc.bottom-rc.top)/cyChar;

	//	TCHAR	nodeStr[10]= {0};
	//	wsprintf(nodeStr,"%d",rc.right);
		//  MessageBox(hStr,nodeStr,"",MB_OK);
	//	SetWindowText(GetParent(hStr),nodeStr);
		
		SCROLLINFO si;
		si.cbSize=sizeof(si);
		si.fMask=SIF_RANGE|SIF_PAGE;
		si.nMin=0;
		si.nMax=numline-1;
		si.nPage=(rc1.bottom-rc1.top)/cyChar;
		SetScrollInfo(hStr,SB_VERT,&si,TRUE);
	
		ReleaseDC(hStr,hStrDC);
	//	DeleteObject(hF_black);
	//	numline=(rc.bottom-rc.top)/cyChar;
	//	si.fMask=SIF_POS;
	//	si.nMax=numline;
	//	SetScrollInfo(hStr,SB_VERT,&si,TRUE);
}

void PrintStr()
{
	//	SetScrollLength();
		HDC hStrDC=GetDC(hStr);
//		HFONT hF_black;
//		hF_black = CreateFont(30, 0, 0, 0,FW_BOLD, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, "楷体");
/*	FW_BOLD	hF_black=CreateFont(20,0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"Arial");
//	"楷体");//	"测试字"//	);*/
//		SelectObject(hStrDC,hF_black);
//		SelectObject(hStrDC,g_hfFont); 

		SCROLLINFO si;
		si.cbSize=sizeof(si);
		//si.fMask=SIF_ALL;
		si.fMask=SIF_POS;
		GetScrollInfo(hStr,SB_VERT,&si);

		RECT rc;
		GetClientRect(hStr,&rc);
	 // GetStockObject (WHITE_BRUSH);
		HBRUSH BGbrush=CreateSolidBrush(bgcolor);

		HBITMAP hBmpBack;
		HDC     hDcBack;
		hDcBack = CreateCompatibleDC(hStrDC);
		hBmpBack = CreateCompatibleBitmap(hStrDC, rc.right-rc.left,rc.bottom-rc.top);
		SelectObject(hDcBack, hBmpBack);
		FillRect(hDcBack,&rc,BGbrush);
		SetBkColor(hDcBack, bgcolor);
		SetTextColor(hDcBack,fgcolor);
/*
		FillRect(hStrDC,&rc,BGbrush);
		SetBkColor( hStrDC, bgcolor);
	//	SetBkColor( hStrDC, RGB(0,0,0));
		SetTextColor(hStrDC,fgcolor);//RGB(255,0,255));	
//lstrlen(TmpStr);
*/



		SelectObject(hDcBack,g_hfFont); 
		TEXTMETRIC tm;

//		GetTextMetrics(hStrDC,&tm);
		GetTextMetrics(hDcBack,&tm);
		int cyChar=tm.tmHeight+tm.tmExternalLeading;
		rc.top-=si.nPos*cyChar;
	
//		DrawText(hStrDC,TmpStr,-1,&rc,/*DT_CALCRECT|*/DT_EXPANDTABS|DT_WORDBREAK);
		DrawText(hDcBack,TmpStr,-1,&rc,/*DT_CALCRECT|*/DT_EXPANDTABS|DT_WORDBREAK);
	//	TCHAR		nodeStr[100];
	//	wsprintf(nodeStr,"%d %d",rc.top,rc.bottom );
	//	TextOut(hStrDC,0,0,nodeStr,lstrlen(nodeStr));
		//TextOut(hStrDC,0,0,TmpStr,lstrlen(TmpStr));
		BitBlt(hStrDC,0,0,rc.right-rc.left,rc.bottom-rc.top,hDcBack,0,0,SRCCOPY);

		ReleaseDC(hStr,hStrDC);
		DeleteObject(BGbrush);
		DeleteDC(hDcBack);
		DeleteObject(hBmpBack);
//		DeleteObject(hF_black);
}

BOOL Cls_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
	memset(colors,255,sizeof(colors));
     //初始化選擇顏色對話方塊,儲存自訂顏色的陣列
//	g_hfFont= CreateFont(20, 0, 0, 0,FW_BOLD, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, "宋体");
	memset(&lf, 0, sizeof(LOGFONT)); 
	lf.lfCharSet = GB2312_CHARSET;
	strcpy((LPSTR)&(lf.lfFaceName),"宋体");// "Fixedsys");  
	lf.lfHeight=30;
	lf.lfWidth=0;
	g_hfFont = CreateFontIndirect(&lf);

	hRMenu=CreatePopupMenu();
	AppendMenu(hRMenu,0,IDM_PRVPAGE,TEXT("上一章"));
	AppendMenu(hRMenu,0,IDM_NEXTPAGE,TEXT("下一章"));
	AppendMenu(hRMenu,MF_SEPARATOR,0,NULL);
	AppendMenu(hRMenu,0,IDM_ZOOMOUT,TEXT("放大"));
	AppendMenu(hRMenu,0,IDM_ZOOMIN,TEXT("缩小"));
	AppendMenu(hRMenu,MF_SEPARATOR,0,NULL);
	AppendMenu(hRMenu,0,IDM_EXIT,TEXT("退出"));

	CenterWindow(hWnd);
    hWinToolbar=CreateToolbarEx(hWnd,
			WS_CHILD | TBSTYLE_FLAT |CCS_ADJUSTABLE
			| TBSTYLE_TOOLTIPS | 
			WS_CLIPCHILDREN | 
			WS_CLIPSIBLINGS | 
			//WS_EX_TRANSPARENT|
			//CCS_NODIVIDER |
			CCS_TOP|
			//TBRS_GRIPPER|
			//TBSTYLE_GRIPPER|
			//TBSTYLE_WRAPABLE|
			//TBSTYLE_LIST|
			//TBSTYLE_TRANSPARENT|
			//CCS_NORESIZE | 
			WS_VISIBLE,
			ID_TOOLBAR,12,Instance,
			IDR_TOOLBAR1,
			(LPCTBBUTTON)&tbButtons,12,
			16,15,16,15,sizeof(TBBUTTON)); 
	
	//ShowWindow(hWinToolbar, SW_SHOWMAXIMIZED);
	//RECT rc2;
	//GetClientRect(hWinToolbar,&rc2);
	//FillRect(GetDC(hWinToolbar),&rc2,(HBRUSH)(COLOR_BTNFACE+1));

	hStr=InitStrView(Instance,hWnd);
	hTree = InitTreeView(Instance,hWnd);
	OldEditProc =(WNDPROC)GetWindowLong(hTree,GWL_WNDPROC);
	SetWindowLong(hTree,GWL_WNDPROC,(LONG)WndProcTree);	
	ScrollCheck=FALSE;
	return (TRUE);
}

//WM_DESTROY
void Cls_OnDestroy(HWND hWnd)
{
	delete MySTRTree;
	DeleteObject(g_hfFont);
	DestroyMenu(hMenu);
	DestroyMenu(hRMenu);
	PostQuitMessage(0);
}

void FillBar(HWND hWnd)
{
	RECT rc,rc1,rc2;//TreeRect,StrRect;
	GetClientRect(hWnd,&rc);
	SendMessage(hWinToolbar,TB_AUTOSIZE,0,0);
	GetClientRect(hWinToolbar,&rc2);
//	GetClientRect(hStr, &StrRect);
//	GetClientRect(hTree,&TreeRect);
	HDC hDC=GetDC(hWnd);
	HBRUSH BGbrush=(HBRUSH)(COLOR_BTNFACE+1);//CreateSolidBrush(RGB(255,255,255));
    //SetBkMode(GetDC(hWinToolbar),OPAQUE);
//	FillRect(GetDC(hWinToolbar),&rc2,BGbrush);
	//SetBkMode(GetDC(hWinToolbar),TRANSPARENT);
	//SetBkColor(GetDC(hWinToolbar),RGB(255,0,255));
	rc1.left=BarLeft-1;
	rc1.right=BarRight;
	rc1.bottom =rc.bottom;
	rc1.top =rc.top+rc2.bottom-rc2.top;
	FillRect(hDC,&rc1,BGbrush);
//	FloodFill(GetDC(hWinToolbar),1,1,RGB(255,0,255));
//	ShowWindow(hWinToolbar, SW_SHOWMAXIMIZED);
//	InvalidateRect(hWinToolbar,NULL, TRUE);

	ReleaseDC(hWnd,hDC);
	DeleteObject(BGbrush);
}

void Resizer(HWND hWnd,LPARAM lParam)
{
	RECT	stClRect,stToolRect;
	UINT	cx,cy;
	GetClientRect(hWnd, &stClRect);
	GetWindowRect(hWinToolbar,&stToolRect);
	cx= stClRect.right - stClRect.left;
	cy = stClRect.bottom -stClRect.top-(stToolRect.bottom-stToolRect.top);

	HDWP hdwp;
	hdwp = BeginDeferWindowPos(2);
	DeferWindowPos(hdwp,hTree,NULL,0,stToolRect.bottom-stToolRect.top,cx/3,cy, SWP_NOZORDER);
	DeferWindowPos(hdwp,hStr,NULL,cx/3+4,stToolRect.bottom-stToolRect.top,2*cx/3-4,cy,SWP_NOZORDER);
	EndDeferWindowPos(hdwp);
	//MoveWindow(hTree,0,stToolRect.bottom-stToolRect.top,cx/3,cy,TRUE);	
	//MoveWindow(hStr, cx/3+4,stToolRect.bottom-stToolRect.top,2*cx/3-4,cy,TRUE);
	BarLeft=cx/3;
	BarRight=cx/3+4;

/*
  HDWP hdwp;
  RECT rect, rectStatus, rectTree;
  hdwp = BeginDeferWindowPos(3);
  GetClientRect(GetParent(hwnd), &rect);
  GetClientRect(hStatus, &rectStatus);
  GetWindowRect(hTree, &rectTree);
  DeferWindowPos(hdwp, hTree, NULL, 0, 0, rectTree.right - rectTree.left + (short)LOWORD(lParam), rect.bottom   - rectStatus.bottom, SWP_NOMOVE | SWP_NOZORDER);
  DeferWindowPos(hdwp, hSplitter, NULL, rectTree.right - rectTree.left + (short)LOWORD(lParam), 0, 0, 0,   SWP_NOSIZE | SWP_NOZORDER);
  DeferWindowPos(hdwp, hList, NULL, rectTree.right - rectTree.left + (short)LOWORD(lParam) + 2, 0, rect.right -   rectTree.right + rectTree.left - (short)LOWORD(lParam) - 2, rect.bottom - rectStatus.bottom, SWP_NOZORDER);
  EndDeferWindowPos(hdwp);
*/
//	FillBar(hWnd);
/*	RECT	ClientRect;
	RECT	WindowRect;
	GetClientRect(hWnd, &ClientRect);
	GetClientRect(hTree,&WindowRect);
	WindowRect.right=ClientRect.right*WindowRect.right/CurWidth;
//	WindowRect.right=LOWORD(lParam)*WindowRect.right/CurWidth;//WindowRect.right/nWndWidth*
    MoveWindow(hTree,0,0,WindowRect.right,ClientRect.bottom,TRUE);
    MoveWindow(hStr,WindowRect.right+4,0,ClientRect.right,ClientRect.bottom,TRUE);
	CurWidth=ClientRect.right;
	//	CurWidth=LOWORD(lParam);*/
}

void Splitter(HWND hWnd,LPARAM lParam)
{
  POINT pPOINT;
  RECT  pRECT,stToolRect;
  if(split == TRUE)
  {
	  GetCursorPos(&pPOINT);
	  ScreenToClient(hWnd,&pPOINT);
	  GetClientRect(hWnd,&pRECT);
	  GetWindowRect(hWinToolbar,&stToolRect);
	  if(pPOINT.x>3&&pPOINT.x<pRECT.right-5)
	  {
		  MoveWindow(hTree,0,stToolRect.bottom-stToolRect.top,LOWORD(lParam),pRECT.bottom-stToolRect.bottom+stToolRect.top,TRUE);
		  MoveWindow(hStr,LOWORD(lParam)+4,stToolRect.bottom-stToolRect.top,pRECT.right-LOWORD(lParam)-4,pRECT.bottom-stToolRect.bottom+stToolRect.top,TRUE);
		  BarLeft=LOWORD(lParam);
		  BarRight=LOWORD(lParam)+4;
	  }
  }
  return ;
}

//WM_NOTIFY
BOOL DoNotify(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
	#define lpNM   ((LPNMHDR)lParam)
	#define lpnmTB ((LPNMTOOLBAR)lParam)
//	switch(lpNM->code){
//  }
	if(((LPNMHDR)lParam)->hwndFrom == hTree){
		HTREEITEM hSelected=((LPNMTREEVIEW)lParam)->itemNew.hItem;
		switch( ((LPNMTREEVIEWA)lParam)->hdr.code)
		{
//		case NM_NCHITTEXT:
//		TreeView_Expand(hTree,hSelected,TVE_EXPAND);
 		//	TVE_COLLAPSE TVE_EXPAND TVE_TOGGLE  TVM_EXPAND 
//			break;
//		case NM_CLICK:
	//	TreeView_Expand(hTree,hSelected,TVE_EXPAND);
 		//	TVE_COLLAPSE TVE_EXPAND TVE_TOGGLE  TVM_EXPAND 
//			break;
//		case TVN_ITEMEXPANDING:
//			MessageBox(hWnd,"1","",MB_OK);
//			OnExpanding( hList, hTree,(LPNMTREEVIEW)lParam);
//			break;
//		case TVN_ITEMEXPANDED:
//		TreeView_Expand(hTree,((LPNMTREEVIEW)lParam)->itemNew.hItem,TVE_TOGGLE);
//			break;
//		case TVN_SELCHANGING:
		case TVN_SELCHANGED:
			keytype myItem=(keytype)hSelected;
			BSTNODE *Mystr=MySTRTree->search(myItem);
			TmpStr	=Mystr->data.MyStr;
//			MessageBox(hTree,TmpStr,"",MB_OK);

			SCROLLINFO si;
			si.cbSize=sizeof(si);
			//si.fMask=SIF_ALL;
			si.fMask=SIF_POS;
			si.nPos=0;
			SetScrollInfo(hStr,SB_VERT,&si,TRUE);
			ScrollCheck=FALSE;
			KillTimer(hStr,ID_TIMER);
			SetScrollLength();
			PrintStr();

		//	MessageBox(hWnd,Mystr->data.MyStr,"",MB_OK);
		/*	HDC hStrDC=GetDC(hStr);
			TextOut(hStrDC,0,0,Mystr->data.MyStr,lstrlen(Mystr->data.MyStr));
			SetBkColor( hStrDC, RGB(0,0,0));
			SetTextColor(hStrDC, RGB(255,255,255));
			ReleaseDC(hStr,hStrDC);*/
			//GetItemText(hTree,hSelected);
		//	MessageBox(hWnd,GetItemText(hTree,hSelected),"",MB_OK);
			//	lstrcpy( szPath, GetItemPath(hTree, ((LPNMTREEVIEW)lParam)->itemNew.hItem));
			//	FillList( hList, szPath);
			break;
		}
			DeleteObject(hSelected);
	}
	else
	{
		LPTBNOTIFY lptbn;
		int nItem;
		switch (((LPNMHDR)lParam)->code)  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -