欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Win32使用Windows公共控件库创建工具条

程序员文章站 2024-03-01 12:55:04
...

Win32程序创建工具条,需要使用Windows公共控件库,是另一个DLL;本身Win32 API 是在系统自带的3个DLL;

Win7,CFree 5.0;运行结果;

Win32使用Windows公共控件库创建工具条

引入lib文件的设置如下;

Win32使用Windows公共控件库创建工具条

如果不在工程设置中引入;则对应的C代码为:#pragma comment (lib,"Comctl32.lib");

如果找到如下CFree安装目录中看上去是库文件的,则构建出错;

Win32使用Windows公共控件库创建工具条

需要包含头文件 #include <commctrl.h>,CFree自带了;

dll应该没有;在CFree目录下搜索不到;自己下;如下图;comctl32.dll这个才能用;COMMCTRL.DLL这个不行;

Win32使用Windows公共控件库创建工具条

工程如下;

Win32使用Windows公共控件库创建工具条

Win32使用Windows公共控件库创建工具条

码:

#include <windows.h>
#include <commctrl.h>
#include "resource.h"
 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;
TCHAR szClassName[] = TEXT("cmtlDemo");
HWND hToolBar;

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_CMTLDEMO);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 
    if (!RegisterClassEx (&wincl))
        return 0;
 
    hwnd = CreateWindowEx (
           0,
           szClassName,
           TEXT("cmtlDemo"),
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           300,
           200,
           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;
				
    switch (message)
    {
			case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{

		    case IDM_ABOUT:
				MessageBox (hwnd, TEXT ("cmtlDemo v1.0\nCopyright (C) 2020\n by bo"),
                        TEXT ("cmtlDemo"), MB_OK | MB_ICONINFORMATION);
				break;
			case IDM_EXIT:
				DestroyWindow(hwnd);
				break;
			default:
				return DefWindowProc(hwnd, message, wParam, lParam);	    		
		    }
    		break;
  		case WM_CREATE:
  			hToolBar = CreateWindowEx(NULL, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
        hwnd, NULL, hInst, NULL);
  			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 "resource.h"
#include <windows.h>

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDC_CMTLDEMO MENU 
BEGIN
    POPUP "&File"
    BEGIN
        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_CMTLDEMO		10101
#define 	IDD_ABOUTBOX	10102

程序只使用了一句代码来创建工具条;在WM_CREATE消息处理分支里;