C++ 右键弹出菜单 标题栏弹出菜单 2014.4.19笔记

本文介绍了一个简单的Windows窗口编程实例,包括窗口的创建、显示、消息处理等关键步骤,并详细展示了如何通过不同消息处理函数实现窗口的基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdafx.h"
#include "stdio.h"
#define WM_FISTRMSG (WM_USER+1)
HWND      g_hButton = NULL;
class WinFrom{
	HINSTANCE g_hInst;
public:
	WinFrom(HINSTANCE C_hInst):g_hInst(C_hInst){}
//消息处理函数

//创建窗口

HWND CreateWnd(LPSTR pszClassName )
{
	HWND hWnd = CreateWindowEx( 0, pszClassName, 
		"MyWnd",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, 500,
		400, NULL, NULL, g_hInst, NULL );
	return hWnd;
}

//显示窗口
void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}

//paint绘图
void OnPaint( HWND hWnd, UINT nMsg, 
			 WPARAM wParam, LPARAM lParam){
	//WM_PAINT开始的时候必须调用的
	PAINTSTRUCT ps={0};
	HDC hDC=BeginPaint(hWnd,&ps);
	
	char szText[]="Hello WM_PAINT";
	TextOut(hDC,100,100,szText,strlen(szText));
	Rectangle(hDC,200,200,300,300);

	//WM_PAINT处理后必须调用的
	EndPaint(hWnd,&ps);

}
//窗口创建时的消息
void OnCreate( HWND hWnd, UINT nMsg, 
			   WPARAM wParam, LPARAM lParam)
{
	LPCREATESTRUCT pCreateStruct = 
			LPCREATESTRUCT(lParam);
	//MessageBox( NULL,pCreateStruct->lpszName,"OnCreate", MB_OK );
	//创建按钮
	//g_hButton = CreateWindowEx( 0, "BUTTON", 
	//	"BUTTON",
	//	WS_CHILD|WS_VISIBLE, 0, 0, 100, 100,
	//	hWnd, NULL, g_hInst, NULL );
	//发送消息
	//SendMessage(hWnd,WM_FISTRMSG,0,0);
}
//窗体重绘时消息处理函数

void OnSize( HWND hWnd, UINT nMsg, 
			 WPARAM wParam, LPARAM lParam)
{
	int nWidth  = LOWORD( lParam );
	int nHeight = HIWORD( lParam );
	CHAR szText[260] = {0};
	sprintf( szText, "WIDTH:%d,HEIGHT:%d",
		nWidth, nHeight );

	if( NULL != g_hButton )
	{
		int nX = ( nWidth - 100 )/2;
		int nY = ( nHeight- 100 )/2;
		MoveWindow( g_hButton, nX, nY, 
			100, 100, TRUE );
	}

}

//系统命令处理函数
BOOL OnSysCommand( HWND hWnd, UINT nMsg, 
			       WPARAM wParam, LPARAM lParam)
{
	switch( wParam )
	{
	case SC_CLOSE:
		if( IDOK == MessageBox( NULL, "是否将文件存盘?",
			"提示", MB_OKCANCEL|MB_ICONWARNING ) )
		{
			return TRUE;
		}
		return FALSE;
	}
	return FALSE;
}
//窗体消息循环

void Message( )
{
	MSG msg = { 0 };
	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
}




};

user_function.h

#include "stdafx.h"
#include "stdio.h"
#define WM_FISTRMSG (WM_USER+1)
HWND      g_hButton = NULL;


class WinFrom{
	HINSTANCE g_hInst;
public:
	WinFrom(HINSTANCE C_hInst):g_hInst(C_hInst){}
//消息处理函数

//创建窗口

HWND CreateWnd(LPSTR pszClassName )
{
	HWND hWnd = CreateWindowEx( 0, pszClassName, 
		"MyWnd",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, 500,
		400, NULL, NULL, g_hInst, NULL );
	return hWnd;
}

//显示窗口
void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}

//paint绘图
void OnPaint( HWND hWnd, UINT nMsg, 
			 WPARAM wParam, LPARAM lParam){
	//WM_PAINT开始的时候必须调用的
	PAINTSTRUCT ps={0};
	HDC hDC=BeginPaint(hWnd,&ps);
	
	char szText[]="Hello WM_PAINT";
	TextOut(hDC,100,100,szText,strlen(szText));
	Rectangle(hDC,200,200,300,300);

	//WM_PAINT处理后必须调用的
	EndPaint(hWnd,&ps);

}
//窗口创建时的消息
void OnCreate( HWND hWnd, UINT nMsg, 
			   WPARAM wParam, LPARAM lParam)
{
	LPCREATESTRUCT pCreateStruct = 
			LPCREATESTRUCT(lParam);
	//MessageBox( NULL,pCreateStruct->lpszName,"OnCreate", MB_OK );
	//创建按钮
	//g_hButton = CreateWindowEx( 0, "BUTTON", 
	//	"BUTTON",
	//	WS_CHILD|WS_VISIBLE, 0, 0, 100, 100,
	//	hWnd, NULL, g_hInst, NULL );
	//发送消息
	//SendMessage(hWnd,WM_FISTRMSG,0,0);
}
//窗体重绘时消息处理函数

void OnSize( HWND hWnd, UINT nMsg, 
			 WPARAM wParam, LPARAM lParam)
{
	int nWidth  = LOWORD( lParam );
	int nHeight = HIWORD( lParam );
	CHAR szText[260] = {0};
	sprintf( szText, "WIDTH:%d,HEIGHT:%d",
		nWidth, nHeight );

	if( NULL != g_hButton )
	{
		int nX = ( nWidth - 100 )/2;
		int nY = ( nHeight- 100 )/2;
		MoveWindow( g_hButton, nX, nY, 
			100, 100, TRUE );
	}

}

//系统命令处理函数
BOOL OnSysCommand( HWND hWnd, UINT nMsg, 
			       WPARAM wParam, LPARAM lParam)
{
	switch( wParam )
	{
	case SC_CLOSE:
		if( IDOK == MessageBox( NULL, "是否将文件存盘?",
			"提示", MB_OKCANCEL|MB_ICONWARNING ) )
		{
			return TRUE;
		}
		return FALSE;
	}
	return FALSE;
}
//窗体消息循环

void Message( )
{
	MSG msg = { 0 };
	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
}




};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值