还是看直接看代码实在!!
前几天看个自己定义消息,看着书上的文字晕了半天还是没有明白!
还是得感谢网友“回到鱼得世界”得帮忙!把我上次得那个一堆BUG得问题也给解决了!!
还是看下MSDN上如何定义自定义消息的。
The WM_USER constant is used by applications to help define private messages for use by private window classes, u sually of the form WM_USER+X, where X is an integer value.
大致意思就是常量WM_USER 被应用程序用来自定义消息,通常用WM_USER +X来定义
语法
#define WM_USER 0x0400
/
WM_USER through 0x7FFF //
Integer messages for use by private window classes. //
/
然后再看MSDN上具体的说明:
Message numbers in the second range (WM_USER through 0x7FFF)
can be defined and used by an application to send messages within a private
window class. These values cannot be used to define messages that are meaningful
throughout an application, because some predefined window classes already define
values in this range. For example, predefined control classes such as BUTTON,
EDIT, LISTBOX, and COMBOBOX may use these values.
Messages in this range should not be sent to other applications unless the
applications have been designed to exchange messages and to attach the same
meaning to the message numbers.
在一个自定义的窗口可以用WM_USER 到 0x7FFF之间的值去定义自定义消息,但是不能用来定义meaningful
(这里的meaningful应该是按钮,控键,编辑一类的消息)的消息,还有在这个范围内的消息不能被送到其他的没有定义该消息的
应用程序中去.
还是看网友“回到鱼得世界”帮我改的那个程序吧!
一些实现的细节我就忽略不写了,在上次写的那篇文章里有具体的代码!
#include <windows.h>
#include "resource.h"
#define WM_CHANGECOLOR WM_USER+100//全局变量
HINSTANCE hinst;
COLORREF tmpcolor;// 函数的声明
BOOL CALLBACK DialogFun(HWND, UINT,WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
InitApplication(HINSTANCE);
InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL PaintWindows(HWND,int);//定义粉刷窗口函数// 应用程序进入点函数
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
...............................
}//应用程序的初始化
BOOL InitApplication(HINSTANCE hinstance)
{..............................}
//创建并显示窗口
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{.................................}
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{PAINTSTRUCT ps;
HDC hdc;
RECT rect;
switch (uMsg)
{
case WM_COMMAND:
//hMenu = GetMenu(hwnd);
switch(LOWORD(wParam))
{
case IDM_DIALOG_TEST:
DialogBox(hinst,"MyDialog",hwnd,(DLGPROC)DialogFun);
return 0;
case IDM_TEST1:
MessageBox(hwnd,"Test Menu1","menu1",MB_OK);
return 0;
case IDM_TEST2:
MessageBeep(0);
MessageBox(hwnd,"菜单2测试","navyblue",MB_OK);
return 0;
case IDM_ABOUT_ME:
MessageBox(hwnd," 做人要厚道!","这家伙很懒提示您:",MB_OK);
return 0;
//CreateDialog
}
case WM_PAINT:
HBRUSH brush,oldbrush;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
brush=CreateSolidBrush(tmpcolor);//创建画刷
GetClientRect(hwnd,&rect);//获得主窗口的面积
oldbrush=(HBRUSH)SelectObject(hdc,brush);
Rectangle(hdc,rect.left, rect.top, rect.right, rect.bottom);
SelectObject(hdc,oldbrush);
EndPaint(hwnd, &ps);
return 0;
// Process other messages.
case WM_CHANGECOLOR:
GetClientRect(hwnd,&rect);//获得主窗口的面积
InvalidateRect(hwnd, &rect, TRUE);
return 0;
case WM_DESTROY:
// Clean up window-specific data objects.
PostQuitMessage (0) ;
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}BOOL CALLBACK DialogFun(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
{
switch(message){..............}return 0;
}