您的位置:首页 > 其它

CreateWindowEx()自定义窗口句柄,消息处理

2016-03-21 22:44 239 查看
//
/***************************************************
MyWnd.h -- MACRO Definition of the wnd ,Can be used anywhere U Want
version 1.0.8, March 21th, 2016

Copyright (C) 2015-2016 Acheld CHEN

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

Acheld CHEN http://blog.csdn.net/acheld 
****************************************************/

/////////////////////////////////////////
#ifndef MYWND_H
#define MYWND_H

#include "wtypes.h"
#include <assert.h>

#define _MYWND_IMPLEMENT_DYNCREATE \
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);\
HWND CreateMyWnd()\
{\
HINSTANCE hInstance = GetModuleHandle(NULL);\
srand(GetTickCount());\
char wndClassName[255];\
sprintf(wndClassName, "MyWnd%d", rand());\
WNDCLASSEX wcex=\
{\
sizeof(WNDCLASSEX),CS_HREDRAW|CS_VREDRAW,\
MyWndProc,0,0,hInstance,LoadIcon(NULL,IDI_APPLICATION),\
LoadCursor(NULL,IDC_ARROW),(HBRUSH)GetStockObject(GRAY_BRUSH),\
NULL,wndClassName,\
LoadIcon(NULL,IDI_APPLICATION)\
};\
RegisterClassEx(&wcex);\
HWND hwnd;\
hwnd=CreateWindowEx(NULL,wndClassName,wndClassName,WS_OVERLAPPEDWINDOW,10,10,200,200,NULL,NULL,hInstance,NULL);

if(!hwnd){return NULL;}\
ShowWindow(hwnd,SW_HIDE);\
UpdateWindow(hwnd);\
return hwnd;\
}

#define _MYWND_DECLARE_MESSAGE_FUNC(MsgFunc) \
LRESULT MsgFunc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

#define _MYWND_BEGIN_MESSAGE_MAP \
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)\
{\
switch(msg)\
{\
case WM_CREATE:\
break;\
case WM_DESTROY:\
PostQuitMessage(0);\
break;

#define _MYWND_MESSAGE(msg, MsgFunc) \
case msg:\
MsgFunc(hwnd, msg, wparam, lparam);\
break;

#define _MYWND_END_MESSAGE_MAP \
}\
return ::DefWindowProc(hwnd,msg,wparam,lparam);\
}

#define _MYWND_CREATE(hwnd)  hwnd = CreateMyWnd();

#endif


//////

MYWND_IMPLEMENT_DYNCREATE

MYWND_DECLARE_MESSAGE_FUNC(MsgFunc)

MYWND_BEGIN_MESSAGE_MAP

MYWND_MESSAGE(msg, MsgFunc)

MYWND_END_MESSAGE_MAP


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CreateWindowEx