Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹

#include 
#include 
#include
#include "resource.h"
 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
 
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("commDlgDemo");
 
int WINAPI
WinMain (HINSTANCE hThisInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpszArgument,
         int nFunsterStil)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
 
	hInst = hThisInstance;
	
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);
 
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE (IDC_COMMDLGDEMO);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 
    if (!RegisterClassEx (&wincl))
        return 0;
 
    hwnd = CreateWindowEx (
           0,
           szClassName,
           TEXT("commDlgDemo"),
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           400,
           400,
           HWND_DESKTOP,
           NULL,
           hThisInstance,
           NULL
           );
 
    ShowWindow (hwnd, nFunsterStil);
 
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
 
    return messages.wParam;
}
 
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rt;
	int ret;
	char szBuffer[100];
	COLORREF acrCustClr[16];// array of custom colors
	CHOOSECOLOR stChooseColor; 
	COLORREF rgbLineColor; 
	CHOOSEFONT cf ;
	LOGFONT lf ;
	DWORD myfsize;
	char FileName[MAX_PATH]= {0};
 	OPENFILENAME ofn = {0};
 	char myfolder[MAX_PATH] = { 0 };
	BROWSEINFO bi = { 0 };
				
    switch (message)
    {
			case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
   			case IDM_sysColorDlg:
				stChooseColor.lStructSize    = sizeof(CHOOSECOLOR) ;
	            stChooseColor.hwndOwner      = hwnd ;
	            stChooseColor.rgbResult      = rgbLineColor ;
	            stChooseColor.lpCustColors   = (LPDWORD) acrCustClr ;
	            stChooseColor.Flags          = CC_RGBINIT ;
	            stChooseColor.lCustData      = 0 ;
	            stChooseColor.lpfnHook       = NULL ;
	            stChooseColor.lpTemplateName = NULL ;
				
				if (ChooseColor(&stChooseColor))
				{
    				rgbLineColor = stChooseColor.rgbResult;
    				BYTE r = GetRValue(rgbLineColor);
					BYTE g = GetGValue(rgbLineColor);
					BYTE b = GetBValue(rgbLineColor);
    				wsprintf(szBuffer, "红:0x%x,绿:0x%x,蓝:0x%x",r,g,b);
    				MessageBox(NULL,szBuffer,TEXT("返回颜色值"),0);
				}
   				break;
			case IDM_sysFontDlg:
				cf.lStructSize = sizeof (CHOOSEFONT) ;
		        cf.hwndOwner = hwnd ;
		        cf.hDC = NULL ;
		        cf.lpLogFont = &lf ;
		        cf.iPointSize = 0 ;
		        cf.Flags = CF_INITTOLOGFONTSTRUCT |
		            CF_SCREENFONTS | CF_EFFECTS ;
		        cf.rgbColors = 0 ;
		        cf.lCustData = 0 ;
		        cf.lpfnHook = NULL ;
		        cf.lpTemplateName = NULL ;
		        cf.hInstance = NULL ;
		        cf.lpszStyle = NULL ;
		        cf.nFontType = 0 ;
		        cf.nSizeMin = 0 ;
		        cf.nSizeMax = 0 ;
		        if (ChooseFont (&cf))
		        {
        			myfsize=cf.iPointSize;
        			wsprintf(szBuffer, "选择的字号大小是:%d (单位1/10磅)",myfsize);
    				MessageBox(NULL,szBuffer,TEXT("系统字体对话框"),0);
        		}
				break;
			case IDM_sysFileDlg:
				ofn.lStructSize = sizeof(OPENFILENAME);
        		ofn.hwndOwner = hwnd ;
        		ofn.lpstrFilter = "All Files (*.*)\0*.*\0\0";
        		ofn.nMaxFile = MAX_PATH ;
        		ofn.nMaxFileTitle = MAX_PATH ;
        		ofn.lpstrFile = FileName ;
        		ofn.lpstrTitle = "打开文件对话框Demo";
        		ofn.Flags = OFN_HIDEREADONLY |OFN_CREATEPROMPT ;
        		if(GetOpenFileName(&ofn))
        		{
		        	MessageBox(NULL,FileName,"选择的文件是",0);	
		        }        
				break;
			case IDM_SHFolderDlg:
				bi.hwndOwner = hwnd;
				bi.pszDisplayName = myfolder;//接收文件夹的缓冲区
				bi.lpszTitle = TEXT("浏览选择文件夹对话框Demo");//标题
				bi.ulFlags = BIF_NEWDIALOGSTYLE;
				LPITEMIDLIST idl = SHBrowseForFolder(&bi);
				if (SHGetPathFromIDList(idl, myfolder)){
					MessageBox(NULL, myfolder, TEXT("你选择的文件夹"), 0);
				}
				break;
		    case IDM_ABOUT:
				MessageBox (hwnd, TEXT ("commDlgDemo v1.0\nCopyright (C) 2020\n by bo"),
                        TEXT ("commDlgDemo"), MB_OK | MB_ICONINFORMATION);
				break;
			case IDM_EXIT:
				DestroyWindow(hwnd);
				break;
			default:
				return DefWindowProc(hwnd, message, wParam, lParam);	    		
		    }
    		break;
    	case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);					
			GetClientRect(hwnd, &rt);			
			
			EndPaint(hwnd, &ps);
			break;
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}

字体,颜色,打开文件对话框,包含头文件 #include

都是先定义一个结构体,赋值,再调用函数打开对话框;然后获取返回值;具体见代码;

系统颜色对话框;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第1张图片

选择了红色返回;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第2张图片

选择了一种类似绿色返回;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第3张图片

选择了黑色返回;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第4张图片

返回的颜色值使用如下代码提取颜色分量;

BYTE r = GetRValue(rgbLineColor);

系统字体对话框:

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第5张图片

选择的字号:返回的结构体成员.iPointSize是选择的字号大小;四号字体返回;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第6张图片

八号字体返回;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第7张图片

打开文件对话框;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第8张图片

返回选择的文件;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第9张图片

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第10张图片

系统浏览文件夹对话框;跟前面三个不同;这个是Windows API的Shell编程;

需要包含#include;并且函数是SH开头的;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第11张图片

返回选择的文件夹;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第12张图片

菜单;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第13张图片

工程;

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第14张图片

Win32通用对话框编程Demo - 颜色、字体、打开文件、浏览文件夹_第15张图片

资源和头文件;

#include "resource.h"
#include 

/
//
// Menu
//

IDC_COMMDLGDEMO MENU 
BEGIN
    POPUP "&File"
    BEGIN
    	MENUITEM "系统颜色对话框",    IDM_sysColorDlg
    	MENUITEM "字体选择对话框",    IDM_sysFontDlg
    	MENUITEM "打开文件对话框",    IDM_sysFileDlg
    	MENUITEM "浏览文件夹对话框",  IDM_SHFolderDlg 
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_COMMDLGDEMO		10101
#define 	IDD_ABOUTBOX	10102
#define     IDM_sysColorDlg    40001
#define     IDM_sysFontDlg    40002
#define     IDM_sysFileDlg    40003
#define     IDM_SHFolderDlg   40004

这是初始化结构体所有成员为0;

BROWSEINFO bi = { 0 };

你可能感兴趣的:(VC++,ChooseColor,ChooseFont,GetOpenFileName,SHBrowse,BROWSEINFO)