当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » C/C++  » Windows编程
分享到: 
收藏 +0
3
新群招人一起学习,交流,以后可以的话想组建个团队一起开发!群:234056226,这里只贴上图片,代码上传到群里!
标签: Windows

代码片段(5) [全屏查看所有代码]

1. [图片] 启动_副本.jpg    

2. [图片] 编辑框托放控制大小_副本.jpg    

3. [图片] 最小化置顶搜索框_副本.jpg    

4. [图片] QQ截图20150525225254.jpg    

5. [文件] APImain.c ~ 16KB     下载(84)     跳至 [5] [全屏预览]

/*			主界面处理文件			*/

#include<windows.h>
#include<windowsx.h>
#include<commctrl.h>
#include<locale.h>
//#include<richedit.h>
#include"Share_Func.h"
#include"ListBox.h"
#include"Dir_Tree.h"
//#include"Hooks.h"
#include"TreeItem.h"
#include"TabCon.h"
#include"Configuration.h"
#include"SelectFont.h"
#include"Resource.h"

#define IDB_TREE			0x01
#define ID_TIMER_CONFI      0x0111

extern VOID OpenStartBox (HWND);					//开启窗口
extern VOID EmptyConData (HWND, BOOL);				//使编辑相关控件禁用或启用

//////////////////////////////////////////////////////////////////////////

#define HANDLE_WM_PALLET(hwnd, wParam, lParam, fn) \
		(BOOL)(fn)((HWND)(hwnd), (WPARAM)(wParam), (LPARAM)(lParam))		/*自定义托盘消息分流宏*/

#undef  HANDLE_WM_SYSCOMMAND									/*取消 windowsx.h 中的宏定义,因无返回值会导致窗口的正常显示*/
#define HANDLE_WM_SYSCOMMAND(hwnd, wParam, lParam, fn) \
		(BOOL)(fn)((HWND)(hwnd), (UINT)(wParam), (int)LOWORD(lParam), (int)HIWORD(lParam))	/*重定义,添加了返回值*/

#undef  HANDLE_WM_NOTIFY
#define HANDLE_WM_NOTIFY(hwnd, wParam, lParam, fn) \
			(fn)((hwnd), (int)(wParam), (LPARAM)(lParam))			//重定义

//////////////////////////////////////////////////////////////////////////

#define ID_ICONP  0x01					/*系统托盘ID*/

extern VOID TreeDragMode      (HWND, PPOINT);				//处理拖动树项显示视图

static BOOL CALLBACK MainProc (HWND, UINT, WPARAM, LPARAM);			/*主窗口过程*/
static VOID SelectTextSearch  (HWND, HWND);								//选中文本弹出搜索选择


PAPIDIRLIST	  pdirlist = NULL;						           //数据主链表

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	MSG		msg;
//	HHOOK   Hhook;
	HACCEL  hAccel = LoadAccelerators (hInstance, _T("加速键"));

	hwndM	   = CreateDialog (hInstance, _T("主界面"), NULL, MainProc);

	_tsetlocale (LC_ALL, _T(""));						//解决异常 Expression: (unsigned)(c+1)<=256

//	Hhook = SetupKeystrokeHook (hInstance);			//安装键盘钩子

	while (GetMessage (&msg, NULL, 0, 0))
	{	
		if (GetDlgCtrlID (msg.hwnd) == IDC_Data_Tab)
		{
			NMHDR				pndr;					//自定义响应双击和右击Tab选项卡消息
			BOOL				f = FALSE;

			pndr.hwndFrom = msg.hwnd;
			pndr.idFrom   = IDC_Data_Tab;

			switch (msg.message)
			{
				case WM_RBUTTONDOWN:
					pndr.code = TCN_RBUTTONDOWN;
					f         = TRUE;
					break;

				case WM_LBUTTONDBLCLK:
					pndr.code = TCN_LBUTTONDBLCLK;
					f         = TRUE;
					break;
			}
			
			if (f)
			{
				//SendMessage (hwndM, WM_NOTIFY, 0, (LPARAM) &pndr);		//在消息循环中最好不要使用发送消息,最好用改写消息结构中的数据达到目的
				msg.hwnd    = hwndM;
				msg.message = WM_NOTIFY;
				msg.lParam  = (LPARAM) &pndr;
			}
		}
		else if (msg.message == WM_KEYDOWN)
		{
			HWND    hwnd_L, hwnd_E;

			if (hwndS)
				hwnd_E = hwndS;
			else
				hwnd_E = hwndM;

			if (hwndList)
				hwnd_L = hwndList;
			else
				hwnd_L = hwndM;

			hwnd_E = GetDlgItem (hwnd_E, IDC_EDIT1);
			hwnd_L = GetDlgItem (hwnd_L, IDC_LIST1);

			if (msg.hwnd == hwnd_E || msg.hwnd == hwnd_L)
				ListAndEdit (GetParent (hwnd_L), msg.hwnd, msg.wParam, hwnd_L, hwnd_E);		//列表框与编辑框互动处理
		}
		else if (msg.message == WM_LBUTTONUP)
		{
			switch (GetDlgCtrlID (msg.hwnd))
			{				
				case IDC_EDIT2:			/*   功能实现:在编辑框文本中拖动鼠标弹出api查找快捷菜单   */
				case IDC_EDIT3:
				
					SelectTextSearch (hwndM, msg.hwnd);
					break;
			}
			
		}
																					//子窗口不需要此条
		if (!TranslateAccelerator (hwndM, hAccel, &msg) && 
			!IsDialogMessage      (hwndM, &msg))// && (!hwndCorrela || !IsDialogMessage (hwndCorrela, &msg)))
		{
			TranslateMessage (&msg);
			DispatchMessage  (&msg);
		}

/*		if (GetDlgCtrlID (msg.hwnd) == IDC_Data_Tab && msg.message == WM_PAINT)		//等消息处理完后再进行绘制,不会被刷新
		{
			extern VOID DragFocusState (HWND, HWND, INT);		//功能添加

			DragFocusState (hwndM, msg.hwnd, TabCtrl_GetCurSel (msg.hwnd));
		}*/
	}

//	UnloadHook (Hhook);			//卸载钩子
	DestroyAcceleratorTable (hAccel);

	return msg.wParam;
}

BOOL CALLBACK MainProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static RECT				rcDialog;
	RECT					rect;
	POINT					point;
	static HWND				hwndTree;

//	COLORREF				rgb;


//////////////////////////////////////////////////////////////////////////
/*	static		HBITMAP		hbitmap001, hbitmapD002;
	static		BITMAP		bitmap001, bitmapD002;
	PSTR		BitmapName[6] = {
									"皮肤\\001.bmp",
									"皮肤\\002.bmp",
									"皮肤\\003.bmp",
									"皮肤\\004.bmp",
									"皮肤\\D001.bmp",
									"皮肤\\D002.bmp"
								};
	static      HDC		    hdcMem1;
	HDC						hdc;*/
	static      HBRUSH		hbrush, hbrushS;
	HICON                   hIcon;
//	static      HFONT       hFont;
//	INT			y;
//////////////////////////////////////////////////////////////////////////


	switch (message)
	{
		HANDLE_MSG (hwnd, WM_MOUSEMOVE,	  OnMouseMove);				//消息分流, 鼠标移动消息
		HANDLE_MSG (hwnd, WM_SYSCOMMAND,  OnSysCommand);				//分流系统消息
		HANDLE_MSG (hwnd, WM_COMMAND,	  OnCommand);				
		HANDLE_MSG (hwnd, WM_PALLET,	  OnPallet);					//分流自定义托盘程序消息
		HANDLE_MSG (hwnd, WM_NOTIFY,      OnNotify);
		HANDLE_MSG (hwnd, WM_CONTEXTMENU, OnContextMenu);			//右击控件响应消息
		HANDLE_MSG (hwnd, WM_LBUTTONDOWN, OnLButtonDown);
		HANDLE_MSG (hwnd, WM_LBUTTONUP,   OnLButtonUp);

		case WM_INITDIALOG:

			ModeFlag  = TRUE;							//标记为编辑模式

			GetClientRect (hwnd, &rcDialog);

			SetWindowText (hwnd, _T("API笔记管理器.v1 -- by : FFD.翼"));
			SetClassLong  (hwnd, GCL_HICON,	  (LONG) (hIcon = LoadIcon   ((HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), _T("MainIcon"))));
			DestroyIcon   (hIcon);

			//	SetClassLong  (hwnd, GCL_HCURSOR, (LONG) LoadCursor ((HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), MAKEINTRESOURCE (ID_CUR___)));
			
			
		//	hFont = CreateFont (25, 40, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, PROOF_QUALITY, DEFAULT_PITCH, NULL);
		 //   SendMessage   (GetDlgItem (hwnd, IDC_EDIT2), WM_SETFONT, (WPARAM)hFont, 0);	
		   // DeleteObject  (hFont);									//删除字体

			// 设置分层属性
			//SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
			// 设置透明色
			//SetLayeredWindowAttributes(hwnd, (COLORREF)GetSysColor(COLOR_BTNFACE), 255, LWA_ALPHA | LWA_COLORKEY);
			//SetLayeredWindowAttributes (hwnd, 0, 200, LWA_ALPHA);



//////////////////////////////////////////////////////////////////////////
/*			hbitmap001 = CreateDibHbitmapFile (BitmapName[3]);			//创建皮肤位图
			GetObject (hbitmap001, sizeof (BITMAP), &bitmap001 );		//取得位图信息

			hdc     = GetDC (hwnd);
			hdcMem1 = CreateCompatibleDC (hdc);
			SelectObject (hdcMem1, hbitmap001);

			ReleaseDC    (hwnd, hdc);
			DeleteObject (hbitmap001);*/
//
//			hbrush = CreatePatternBrush (hbitmap001);			//创建位图画刷
//////////////////////////////////////////////////////////////////////////
			
			rgb_W = RGB (0,255,255);
			rgb_K = RGB (200,255,255);
			rgb_S = RGB (0,120,120);

			hbrush  = CreateSolidBrush (rgb_W);		//窗口背景色
			hbrushS = CreateSolidBrush (rgb_K);		//控件背景色

			_SetClassCur_ (GetDlgItem (hwnd, IDC_EDIT1), ID_CUR_Edit);
			//////////////////////////////////////////////////////////////////////////
			/*			树状控件初始化		*/
			
			hwndTree = GetDlgItem (hwnd, IDC_TREE1);
			InitializeTree (hwnd, hwndTree, rgb_K);
			

			//////////////////////////////////////////////////////////////////////////
			/*			列表控件初始化			*/

			InitListBox (GetDlgItem (hwnd, IDC_LIST1));	//初始化列表框

			//////////////////////////////////////////////////////////////////////////
			/*			编辑框初始化				*/
													//设置编辑框的最大字符数
			SendDlgItemMessage (hwnd, IDC_EDIT2, EM_LIMITTEXT, LINE_MAX * ROW_MAX, 0);
			SendDlgItemMessage (hwnd, IDC_EDIT3, EM_LIMITTEXT, LINE_MAX * ROW_MAX, 0);

			//////////////////////////////////////////////////////////////////////////
			
			OpenStartBox (hwnd);					//启动窗口
			
	//		InitiateSlider (hwnd);				//初始化滑块

			/*	设置类的属性会导致将来创建的TAB拥有了相同属性	*/
			SetClassLong (GetDlgItem (hwnd, IDC_TAB2), GCL_HBRBACKGROUND, (LONG) hbrush);		//设置TAB背景色

			InitiateTabC (hwnd);				//初始化TAB控件

			Initialize_OS_Box (hwnd);				//初始化公用打开保存对话框
			
			SetPallet (hwnd, ID_ICONP);				//设置托盘程序

			CreateCorrelaBox (hwnd);				//创建相关主题窗口

			GetToIDRect (hwnd, IDC_EDIT2, IDC_EDIT3, &SlideRt);	//取得编辑2和编辑3之间的矩形
			GetToIDRect (hwnd, IDC_TAB2, IDC_EDIT2, &SlideRt2);

			TabApi_InitList ();					//初始化与tab相关的api链表(TabCon.h)
			EmptyConData    (hwnd, FALSE);		//控件相关禁用
			
			
			SetTimer (hwnd, ID_TIMER_CONFI, 100, NULL);				//配置处理计时器

			return 0;


//						   UI处理代码
//////////////////////////////////////////////////////////////////////////
//

		case WM_CTLCOLORLISTBOX:			//列表框UI
			SetBkMode    ((HDC)wParam, TRANSPARENT );				//字体背景透明
			return (BOOL) hbrushS;

		case WM_CTLCOLOREDIT:				//编辑框UI
			switch (GetDlgCtrlID ((HWND)lParam))
			{
				case IDC_EDIT2:
				case IDC_EDIT3:
					//SetBkMode    ((HDC)wParam, TRANSPARENT);//OPAQUE);				//字体背景透明
					SetTextColor ((HDC)wParam, rgb_S);
					break;

				case IDC_EDIT1:
					SetTextColor ((HDC)wParam, RGB (255,0,0));
					break;
			}
			return (BOOL) GetStockObject (DC_BRUSH);

		case WM_CTLCOLORSTATIC:	//处理静态文本背景
			switch (GetDlgCtrlID ((HWND)lParam))
			{
				case IDC_FileModel:
				case IDC_FileCount:
				case IDC_FileOpenCount:
					SetTextColor ((HDC)wParam, RGB(255, 0, 0));			//白色字体
					break;

				case IDC_EDIT2:
				case IDC_EDIT3:
					SetBkColor ((HDC)wParam, rgb_K);	//设置文本背景
					SetBkMode  ((HDC)wParam, OPAQUE);
					return (BOOL) hbrushS;
			}

			SetBkMode    ((HDC)wParam, TRANSPARENT);				//字体背景透明

		case WM_CTLCOLORDLG:		//绘制对话框背景消息
//																	//通过SetStretchBltMode的设置能使StretchBlt在缩放图像更加清晰   
//			SetStretchBltMode ((HDC)wParam, COLORONCOLOR);  
	//		StretchBlt ((HDC)wParam, 0, 0, rcDialog.right, rcDialog.bottom, hdcMem1, 0, 0, 
		//												bitmap001.bmWidth, bitmap001.bmHeight, SRCCOPY); 
//
		//	return (BOOL) ((HBRUSH) GetStockObject (NULL_BRUSH));								//返回空刷*/
			return (BOOL) hbrush;
//
//////////////////////////////////////////////////////////////////////////
//
/*		case WM_PAINT:
			{
				extern VOID DragFocusState (HWND, HWND, INT, HDC);

				HWND   hwndT = GetDlgItem (hwnd, IDC_Data_Tab);
				INT    i     = TabCtrl_GetCurSel (hwndT);
			
				if (i > -1)
				{
					PAINTSTRUCT  ps;
					HDC    hdc   = BeginPaint (hwnd, &ps);

					DragFocusState (hwnd, hwndT, i, hdc);

					EndPaint (hwnd, &ps);
				}
			}
			return FALSE;*/

		case WM_MOVE:
			if (hwndCorrela)					//发送给相关主题窗口进行相对移动
			{
			//	POINTS   po = MAKEPOINTS ((WORD) lParam);

			//	extern VOID   SetCorrelaBox_MAX_MIN (HWND, BYTE);
			//	extern BYTE   pos;

			//	SetCorrelaBox_MAX_MIN (hwndCorrela, pos);
			//	MoveWindow  (hwnd, po.x, po.y, rcDialog.right, rcDialog.bottom, TRUE);
				SendMessage (hwndCorrela, WM_MOVE, 0, 0);
			}
			break;
			
		case WM_SIZE:
			if (wParam != SIZE_MAXSHOW && wParam != SIZE_MINIMIZED)				//不处理最小化和激活时窗口变更
			{
				//InvalidateRect (hwnd, NULL, FALSE);
				
																//处理控件大小
				GetClientRect (hwnd, &rect);
				point.x  = rect.right  - rcDialog.right;
				point.y  = rect.bottom - rcDialog.bottom;
				rcDialog = rect;

				if (point.x != 0 || point.y != 0)				//判断窗口大小是否改变
				{
					SetCWPos (hwnd, &point);					//设置控件与窗口位置同步
					InvalidateRect (hwnd, NULL, TRUE);

					GetToIDRect (hwnd, IDC_EDIT2, IDC_EDIT3, &SlideRt);
					GetToIDRect (hwnd, IDC_TAB2,  IDC_EDIT2, &SlideRt2);
					return 0;
				}
			}
			if (hwndCorrela)
			{
				SendMessage (hwndCorrela, WM_MOVE, 0, 0);
			}
			break;

		/*case WM_HSCROLL:								//处理滑块滑动
			if (SlideSlider (hwnd, wParam, lParam))
				return 0;
			break;*/

		case WM_TIMER:
			switch (wParam)
			{
				case ID_TREE_TIMER1:			//处理拖动至树项时计时项目自动伸缩(在 OnNotify.c 中应用)
					{
						NMTREEVIEW        pnmt;			//取得当前项图标状态

						pnmt.hdr.hwndFrom   = hwndTree;
						pnmt.hdr.code       = TVN_ITEMEXPANDING;
						pnmt.itemNew.hItem  = TreeView_GetSelection (hwndTree);
						pnmt.itemNew.mask   = TVIF_IMAGE | TVIF_SELECTEDIMAGE;

						TreeView_GetItem (hwndTree, &pnmt.itemNew);

						TreeView_Expand  (hwndTree, pnmt.itemNew.hItem, TVE_TOGGLE);	
						SendMessage      (hwnd, WM_NOTIFY, 0, (LPARAM)&pnmt);	//发送消息进行图标设置

						InvalidateRect (hwndTree, NULL, TRUE);
					}

					break;

				case ID_TREE_SCROLL:			//树控件滚动条自动滚动计时器(在 OnNotify.c中应用)
					{
						POINT   pt;

						GetCursorPos   (&pt);
						ScreenToClient (hwnd, &pt);
						TreeDragMode   (hwndTree, &pt);
					}
					break;

				case ID_TIMER_CONFI:
					KillTimer       (hwnd, ID_TIMER_CONFI);
					InitConfigFile  (hwnd);				//初始化配置文件
					break;

			}
			break;

//		case WM_ACTIVATE:    /*一个窗口被激活或失去激活状态, WM_ACTIVATE 是相对于控件的*/
			
			
	//		_SetClassCur_ (hwnd, IDC_ARROW);		//解决光标在弹窗时不还原(但会使最小化搜索框钩子失效)
		//	break;

		case WM_CLOSE:

//////////////////////////////////////////////////////////////////////////
			/*		释放资源		*/

//			DeleteDC        (hdcMem1);
			DeleteObject    (hbrush);
			DeleteObject    (hbrushS);

			DeleteAllDirList      (&pdirlist);		//删除已创建的内存
			TabApi_DeleteAllNodeX ();			    //删除tab相关api数据		

			Delete_Font ();							//删除已创建的字体

//////////////////////////////////////////////////////////////////////////

			if (hwndS)
			{
				SendMessage (hwndS, WM_CLOSE, 0, 0);
			}
			DeletePallet    (hwnd, ID_ICONP);					//删除托盘图标
			SendMessage     (hwndCorrela, WM_CLOSE, 0, 0);
			DestroyWindow   (hwnd);
			PostQuitMessage (0);

			break;

		case WM_QUERYENDSESSION:
			{
				INT   i;

				if (TabApi_JudgeAllModify (&i))
				{
					MessageBeep (MB_ICONEXCLAMATION);
					switch (MessageBox (hwnd, _T("打开的文件有所改动,需要进行保存吗?"), _T("丢失提示!"), 0x20 | 0x03))
					{
					case IDYES:
						SendMessage (hwnd, WM_COMMAND, (WPARAM) MAKELONG (IDM_TAB_SaveAll, 0), 0);
						break;

					case IDCANCEL:

						return TRUE;
					}
				}
			}
			return FALSE;

	}

	return 0;
}

//计算两个控件之间的矩形
VOID GetToIDRect (HWND hwnd, INT ID1, INT ID2, PRECT rt)
{
	RECT      rc;
	POINT     po;
													//计算出可被拖动区域
	GetWindowRect (GetDlgItem (hwnd, ID1), &rc);
	po.x = rc.right;
	po.y = rc.top;
	ScreenToClient (hwnd, &po);
	rt->left = po.x;
	rt->top  = po.y;

	GetWindowRect (GetDlgItem (hwnd, ID2), &rc);
	po.x = rc.left;
	po.y = rc.bottom;
	ScreenToClient (hwnd, &po);
	rt->right  = po.x;
	rt->bottom = po.y;
}

//弹出拖动文本快捷选项
VOID SelectTextSearch (HWND hwnd, HWND hwndE)
{
	INT  istar, iend;			

	istar = iend = 0;
	SendMessage (hwndE, EM_GETSEL, (WPARAM) (LPDWORD) &istar, (LPARAM) (LPDWORD) &iend);

	if (iend != istar)	
	{
		HMENU   hMenu = LoadMenu ((HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), _T("ShorMenu"));
		POINT   po;

		if (iend - istar > 100)			//控制在100个字符以内可进行搜索
			EnableMenuItem (hMenu, ID_SSM_SearchText, MF_GRAYED);

		if (!ModeFlag)
		{
			EnableMenuItem (hMenu, ID_SSM_CUT,   MF_GRAYED);
			EnableMenuItem (hMenu, ID_SSM_Paste, MF_GRAYED);
			EnableMenuItem (hMenu, ID_SSM_Delete,MF_GRAYED);
		}

		GetCursorPos (&po);
		hMenu = GetSubMenu (hMenu, 0);

		TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, po.x, po.y, 0, hwnd, NULL);
		DestroyMenu    (hMenu);
	}
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(5)

  • 1楼:开源无憾 发表于 2015-05-20 14:19 回复此评论
    乍一看,我以为是写了个操作系统
  • 2楼:Succeed丶翼 发表于 2015-05-20 16:23 回复此评论

    引用来自“码农界傲娇段子手”的评论

    乍一看,我以为是写了个操作系统
    挖苦我了,我哪有那本事,只是个小工具!唉!
  • 3楼:蓝栩液枫 发表于 2015-05-26 14:46 回复此评论
    不明学厉
  • 4楼:abcba 发表于 2015-08-18 09:36 回复此评论
    头文件呢
  • 5楼:ronsardji 发表于 2017-04-18 17:59 回复此评论
    没头文件,差评
开源从代码分享开始 分享代码