Win32-系统菜单-右键菜单-图标资源-光标资源-字符串资源-菜单资源-加速键资源-
作者:互联网
系统菜单&右键菜单&图标资源&光标资源&字符串资源&菜单资源&加速键资源
// 系统菜单
// 系统菜单依次删除操作和点击按钮产生消息的事件
// SysMenu.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "stdio.h"
HINSTANCE g_hInst = NULL;
HANDLE g_hStdOut = NULL;
void OnCreate( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{ // 获取系统菜单
HMENU hSysMenu =
GetSystemMenu( hWnd, FALSE );
// 删除菜单项;这里删除系统菜单是按照顺序依次向下删除
// 系统菜单分别如下:还原,移动,大小,最小化,最大化,关闭
RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 还原
RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 移动
RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 大小
RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 最小化
RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 最大化
//RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 关闭
//RemoveMenu( hSysMenu, 0, MF_BYPOSITION ); // 右上角的X
// 关闭的系统菜单要两个同时删除,才生效
// 增加菜单项
InsertMenu( hSysMenu, 0, MF_BYPOSITION|MF_STRING,
1001, "测试1" );
InsertMenu( hSysMenu, 1, MF_BYPOSITION|MF_STRING,
1002, "测试2" );
}
void OnSysCommand( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{
CHAR szText[260] = { 0 };
sprintf( szText,
"OnSysCommand: WPARAM=%08X,LPARAM=%08X\n",
wParam, lParam );
WriteConsole( g_hStdOut, szText,
strlen(szText), NULL, NULL );
int nID = LOWORD( wParam );
switch( nID )
{
case 1001:
MessageBox( NULL, "Hello 1001",
"SysMenu", MB_OK );
break;
case 1002:
MessageBox( NULL, "Hello 1002",
"SysMenu", MB_OK );
break;
}
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
switch( nMsg )
{
case WM_CREATE:
OnCreate( hWnd, nMsg, wParam, lParam );
break;
case WM_SYSCOMMAND:
OnSysCommand( hWnd, nMsg, wParam, lParam );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
}
BOOL RegisterWnd( LPSTR pszClassName )
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_WINDOW);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
{
return FALSE;
}
return TRUE;
}
HWND CreateWnd( LPSTR pszClassName )
{
HWND hWnd = CreateWindowEx( 0,
pszClassName, "MyWnd",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, g_hInst,
NULL );
return hWnd;
}
void DisplayWnd( HWND hWnd )
{
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
}
void Message( )
{
MSG msg = { 0 };
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
void NewConsole( )
{
AllocConsole( );
g_hStdOut =
GetStdHandle( STD_OUTPUT_HANDLE );
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
NewConsole( );
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( );
return 0;
}
--------------------------------------------------------------------------------
// 右键菜单
// 创建弹出式菜单
// PopMenu.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
HINSTANCE g_hInst = NULL;
void OnRButtonUp( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{ // 创建弹出式菜单
HMENU hPopMenu = CreatePopupMenu( );
// 增加菜单项
AppendMenu( hPopMenu, MF_STRING, 1001, "测试1");
AppendMenu( hPopMenu, MF_SEPARATOR, 0, NULL );
AppendMenu( hPopMenu, MF_STRING, 1002, "退出");
// 获取菜单位置
POINT point = { 0 };
point.x = LOWORD( lParam );
point.y = HIWORD( lParam );
ClientToScreen( hWnd, &point );
// 显示菜单
TrackPopupMenu( hPopMenu, TPM_LEFTALIGN,
point.x, point.y, 0, hWnd, NULL );
}
void OnContextMenu( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{ // 创建弹出式菜单
HMENU hPopMenu = CreatePopupMenu( );
// 增加菜单项
AppendMenu( hPopMenu, MF_STRING, 1001, "测试2");
AppendMenu( hPopMenu, MF_SEPARATOR, 0, NULL );
AppendMenu( hPopMenu, MF_STRING, 1002, "退出");
// 坐标获取
int nX = LOWORD( lParam );
int nY = HIWORD( lParam );
// 显示菜单
TrackPopupMenu( hPopMenu, TPM_LEFTALIGN,
nX, nY, 0, hWnd, NULL );
// 删除菜单
DestroyMenu( hPopMenu );
}
void OnCommand( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{
int nCmdID = LOWORD( wParam );
switch( nCmdID )
{
case 1001:
MessageBox( NULL, "Hello Popmenu",
"PopMenu", MB_OK );
break;
case 1002:
PostQuitMessage( 0 );
break;
}
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
switch( nMsg )
{
case WM_RBUTTONUP:
//OnRButtonUp( hWnd, nMsg, wParam, lParam );
break;
case WM_CONTEXTMENU:
OnContextMenu( hWnd, nMsg, wParam, lParam );
break;
case WM_COMMAND:
OnCommand( hWnd, nMsg, wParam, lParam );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
}
BOOL RegisterWnd( LPSTR pszClassName )
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_WINDOW);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
{
return FALSE;
}
return TRUE;
}
HWND CreateWnd( LPSTR pszClassName )
{
HWND hWnd = CreateWindowEx( 0,
pszClassName, "MyWnd",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, g_hInst,
NULL );
return hWnd;
}
void DisplayWnd( HWND hWnd )
{
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
}
void Message( )
{
MSG msg = { 0 };
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( );
return 0;
}
--------------------------------------------------------------------------------
// 资源的使用
创建光标和添加图标并设置图标
// WinRes.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
HINSTANCE g_hInst = NULL;
void OnSetCursor( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
switch( nMsg )
{
case WM_SETCURSOR:
OnSetCursor( hWnd, nMsg, wParam, lParam );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
}
BOOL RegisterWnd( LPSTR pszClassName )
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_WINDOW);
wce.hCursor =
LoadCursor( g_hInst, MAKEINTRESOURCE(IDC_CURSOR1) );
wce.hIcon =
LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_MAIN) );
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
{
return FALSE;
}
return TRUE;
}
HWND CreateWnd( LPSTR pszClassName )
{
HWND hWnd = CreateWindowEx( 0,
pszClassName, "MyWnd",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, g_hInst,
NULL );
return hWnd;
}
void DisplayWnd( HWND hWnd )
{
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
}
void Message( )
{
MSG msg = { 0 };
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( );
return 0;
}
--------------------------------------------------------------------------------
设置光标资源
// WinRes.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
HINSTANCE g_hInst = NULL;
BOOL OnSetCursor( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{
int nHitTest = LOWORD( lParam );
if( HTCLIENT != nHitTest )
{
return FALSE;
}
//获得窗口的客户区
RECT rcClient = { 0 };
GetClientRect( hWnd, &rcClient );
//获得当前光标的位置
POINT ptPos = { 0 };
GetCursorPos( &ptPos );
ScreenToClient( hWnd, &ptPos );
//根据位置加载光标
HCURSOR hCursor = NULL;
if( ptPos.x < rcClient.right/2 )
{
if( ptPos.y < rcClient.bottom/2 )
{
hCursor = LoadCursor( NULL, IDC_SIZEALL );
}
else
{
hCursor = LoadCursor( NULL, IDC_CROSS );
}
}
else
{
if( ptPos.y < rcClient.bottom/2 )
{
hCursor = LoadCursor( NULL, IDC_WAIT );
}
else
{
hCursor = LoadCursor( NULL, IDC_UPARROW );
}
}
// 设置光标
SetCursor( hCursor );
return TRUE;
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
switch( nMsg )
{
case WM_SETCURSOR:
if( TRUE == OnSetCursor( hWnd, nMsg,
wParam, lParam ) )
{
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
}
BOOL RegisterWnd( LPSTR pszClassName )
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_WINDOW);
wce.hCursor =
LoadCursor( g_hInst, MAKEINTRESOURCE(IDC_CURSOR1) );
wce.hIcon =
LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_MAIN) );
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
{
return FALSE;
}
return TRUE;
}
HWND CreateWnd( LPSTR pszClassName )
{
HWND hWnd = CreateWindowEx( 0,
pszClassName, "MyWnd",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, g_hInst,
NULL );
return hWnd;
}
void DisplayWnd( HWND hWnd )
{
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
}
void Message( )
{
MSG msg = { 0 };
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( );
return 0;
}
--------------------------------------------------------------------------------
创建字符串资源和菜单资源
// WinRes.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
HINSTANCE g_hInst = NULL;
BOOL OnSetCursor( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{ //判断是否是位于客户区之上
int nHitTest = LOWORD( lParam );
if( HTCLIENT != nHitTest )
{ //不在客户区,返回FALSE,
//让DefWindowProc处理
return FALSE;
}
//获得窗口的客户区
RECT rcClient = { 0 };
GetClientRect( hWnd, &rcClient );
//获得当前光标的位置
POINT ptPos = { 0 };
GetCursorPos( &ptPos );
ScreenToClient( hWnd, &ptPos );
//根据位置加载光标
HCURSOR hCursor = NULL;
if( ptPos.x < rcClient.right/2 )
{
if( ptPos.y < rcClient.bottom/2 )
{
hCursor = LoadCursor( NULL, IDC_SIZEALL );
}
else
{
hCursor = LoadCursor( NULL, IDC_CROSS );
}
}
else
{
if( ptPos.y < rcClient.bottom/2 )
{
hCursor = LoadCursor( NULL, IDC_WAIT );
}
else
{
hCursor = LoadCursor( NULL, IDC_UPARROW );
}
}
// 设置光标
SetCursor( hCursor );
return TRUE;
}
void OnCommand( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{
int nCmdID = LOWORD( wParam );
switch( nCmdID )
{
case ID_EXIT:
PostQuitMessage( 0 );
break;
case ID_ABOUT:
break;
}
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
switch( nMsg )
{
case WM_COMMAND:
OnCommand( hWnd, nMsg, wParam, lParam );
break;
case WM_SETCURSOR:
if( TRUE == OnSetCursor( hWnd, nMsg,
wParam, lParam ) )
{
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
}
BOOL RegisterWnd( LPSTR pszClassName )
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_WINDOW);
wce.hCursor =
LoadCursor( g_hInst, MAKEINTRESOURCE(IDC_CURSOR1) );
wce.hIcon =
LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_MAIN) );
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
{
return FALSE;
}
return TRUE;
}
HWND CreateWnd( LPSTR pszClassName )
{
//加载字符串资源
CHAR szText[260] = { 0 };
LoadString( g_hInst, IDS_MAIN, szText, 260 );
//加载菜单
HMENU hMenu = LoadMenu( g_hInst,
MAKEINTRESOURCE(IDR_MAIN) );
//创建窗口
HWND hWnd = CreateWindowEx( 0,
pszClassName, szText,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMenu, g_hInst,
NULL );
return hWnd;
}
void DisplayWnd( HWND hWnd )
{
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
}
void Message( )
{
MSG msg = { 0 };
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( );
return 0;
}
--------------------------------------------------------------------------------
加速键资源的使用 - 其实就是设置快捷键
// WinRes.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
HINSTANCE g_hInst = NULL;
BOOL OnSetCursor( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{ //判断是否是位于客户区之上
int nHitTest = LOWORD( lParam );
if( HTCLIENT != nHitTest )
{ //不在客户区,返回FALSE,
//让DefWindowProc处理
return FALSE;
}
//获得窗口的客户区
RECT rcClient = { 0 };
GetClientRect( hWnd, &rcClient );
//获得当前光标的位置
POINT ptPos = { 0 };
GetCursorPos( &ptPos );
ScreenToClient( hWnd, &ptPos );
//根据位置加载光标
HCURSOR hCursor = NULL;
if( ptPos.x < rcClient.right/2 )
{
if( ptPos.y < rcClient.bottom/2 )
{
hCursor = LoadCursor( NULL, IDC_SIZEALL );
}
else
{
hCursor = LoadCursor( NULL, IDC_CROSS );
}
}
else
{
if( ptPos.y < rcClient.bottom/2 )
{
hCursor = LoadCursor( NULL, IDC_WAIT );
}
else
{
hCursor = LoadCursor( NULL, IDC_UPARROW );
}
}
// 设置光标
SetCursor( hCursor );
return TRUE;
}
void OnCommand( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
{
int nCmdID = LOWORD( wParam );
switch( nCmdID )
{
case ID_EXIT:
PostQuitMessage( 0 );
break;
case ID_ABOUT:
break;
}
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
{
switch( nMsg )
{
case WM_COMMAND:
OnCommand( hWnd, nMsg, wParam, lParam );
break;
case WM_SETCURSOR:
if( TRUE == OnSetCursor( hWnd, nMsg,
wParam, lParam ) )
{
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
}
BOOL RegisterWnd( LPSTR pszClassName )
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_WINDOW);
wce.hCursor =
LoadCursor( g_hInst, MAKEINTRESOURCE(IDC_CURSOR1) );
wce.hIcon =
LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_MAIN) );
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
{
return FALSE;
}
return TRUE;
}
HWND CreateWnd( LPSTR pszClassName )
{
//加载字符串资源
CHAR szText[260] = { 0 };
LoadString( g_hInst, IDS_MAIN, szText, 260 );
//加载菜单
HMENU hMenu = LoadMenu( g_hInst,
MAKEINTRESOURCE(IDR_MAIN) );
//创建窗口
HWND hWnd = CreateWindowEx( 0,
pszClassName, szText,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMenu, g_hInst,
NULL );
return hWnd;
}
void DisplayWnd( HWND hWnd )
{
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
}
void Message( HWND hWnd )
{
//加载加速键表
HACCEL hAccel = LoadAccelerators(
g_hInst, MAKEINTRESOURCE(IDR_ACCEL) );
//消息循环
MSG msg = { 0 };
while( GetMessage( &msg, NULL, 0, 0 ) )
{ // 增加加速键的消息处理
if( !TranslateAccelerator( hWnd, hAccel, &msg ) )
{ //字符消息处理
TranslateMessage( &msg );
//消息派发
DispatchMessage( &msg );
}
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( hWnd );
return 0;
}
--------------------------------------------------------------------------------
小练习程序 - 窗口屏幕上一段文字一直向右下角移动,但是有缺点就是到了边角不会停下来,不会反弹再上去;
// WinCase.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];
int g_nX = 0;
int g_nY = 0; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WINCASE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WINCASE);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WINCASE);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WINCASE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_BEGIN:
SetTimer( hWnd, 1001, 1 * 1000, NULL );
break;
case ID_STOP:
KillTimer( hWnd, 1001 );
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
CHAR szText[] = "Hello Ball";
TextOut( hdc, g_nX, g_nY, szText,
strlen(szText) );
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_TIMER:
g_nX += 5;
g_nY += 5;
InvalidateRect( hWnd, NULL, TRUE );
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
效果展示
系统菜单
右键菜单和弹出式菜单
图标&光标&字符串&菜单&加速键等资源
标签:wParam,菜单,return,hWnd,wce,右键,HWND,NULL,资源 来源: https://www.cnblogs.com/autopwn/p/15616587.html