#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 );
}
}
};