Win32 api编程模板和 加速键编程例子

A 加速键编写过程
1)在资源中刚添加 Accelerator, 然后再资源编辑器中编辑
  格式:
加速键ID ACCELERATORS
BEGIN
    键名,  命令ID,  [,类型]  [,选项]
    ……
END
// 用Ctrl+F1键控制 file open
IDC_PEDUMP ACCELERATORS 
BEGIN
    VK_F1,          IDM_FILE_OPEN,          VIRTKEY, CONTROL, NOINVERT
END

程序实现:
        HACCEL hAccelTable;
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PEDUMP));
	while(GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}


//

Win32 api编程模板



// PEDump.h
#pragma once

#ifndef WINVER                          // Specifies that the minimum required platform is Windows 

Vista.
#define WINVER 0x0600           // Change this to the appropriate value to target other versions of 

Windows.
#endif

#ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600     // Change this to the appropriate value to target other versions of 

Windows.
#endif

#ifndef _WIN32_WINDOWS          // Specifies that the minimum required platform is Windows 98.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE                       // Specifies that the minimum required platform is Internet 

Explorer 7.0.
#define _WIN32_IE 0x0700        // Change this to the appropriate value to target other versions of IE.
#endif


#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>


// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>




/
// resource.h
#define IDS_CAPTION                     101
#define IDS_CLASSNAME                   102
#define IDI_PEDUMP                      103
#define IDR_MENU_PEDUMP                 104
#define IDR_ACCELERATOR1                105
#define ID_FILE_OPEN40001               40001
#define ID_FILE_EXIT                    40002
#define ID_HELP_ABOUT                   40003
#define IDM_HELP_ABOUT                  40004
#define IDM_FILE_OPEN                   40005
#define IDM_FILE_EXIT                   40006
#define IDC_PEDUMP                      40007

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        106
#define _APS_NEXT_COMMAND_VALUE         40009
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif





PEDump.cpp

#include "PEDump.h"
#include "resource.h"


#define  MAX_STR_CAPTION	100

TCHAR szTitle[MAX_STR_CAPTION];
TCHAR szWinClass[MAX_STR_CAPTION];

ATOM RegisterClass(HINSTANCE hInstance);
LRESULT CALLBACK PEDumpWndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);

int APIENTRY _tWinMain(HINSTANCE hInstance,
					   HINSTANCE hPrevInstance,
					   LPTSTR    lpCmdLine,
					   int       nCmdShow)
{
	MSG msg;
	HACCEL hAccelTable;

	LoadString(hInstance,IDS_CAPTION,szTitle, MAX_STR_CAPTION);
	if (RegisterClass(hInstance) == 0)
	{
		return FALSE;
	}

	if (!InitInstance(hInstance, nCmdShow))
	{
		return FALSE;
	}
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PEDUMP));

	while(GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return 0;
}


ATOM RegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX winClass;
	LoadString(hInstance, IDS_CLASSNAME, szWinClass, MAX_STR_CAPTION);

	winClass.cbSize			= sizeof(WNDCLASSEX);
	winClass.style			= CS_HREDRAW | CS_VREDRAW;
	winClass.lpfnWndProc	= PEDumpWndProc;
	winClass.cbClsExtra		= 0;
	winClass.cbWndExtra		= 0;
	winClass.hInstance		= hInstance;
	winClass.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PEDUMP));
	winClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground	= (HBRUSH)(COLOR_WINDOW + 1);
	winClass.lpszMenuName	= MAKEINTRESOURCE(IDR_MENU_PEDUMP);
	winClass.lpszClassName	= szWinClass;
	winClass.hIconSm		= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PEDUMP));
	
	return RegisterClassEx(&winClass);
}

LRESULT CALLBACK PEDumpWndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch(Msg)
	{
	case WM_COMMAND:
		wmId	= LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch(wmId)
		{
		case IDM_FILE_OPEN:
			MessageBox(NULL, L"Open", L"Open",NULL);
			break;
		case IDM_FILE_EXIT:
			DestroyWindow(hWnd);
			break;
		case IDM_HELP_ABOUT:
			break;
		default:
			return DefWindowProc(hWnd, Msg,wParam,lParam);
		}
		break;

	case WM_PAINT:
		hdc = BeginPaint(hWnd,&ps);
		EndPaint(hWnd, &ps);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, Msg,wParam,lParam);
	}
	return 0;
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	hWnd = CreateWindow(szWinClass,szTitle,WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
	int err = GetLastError();
	if (NULL == hWnd)
	{
		return FALSE;
	}
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值