C++ Windows 下各种常用的消息 30
正确调用PostMessage()。正确调用SendMessage()比较PostMessage函数和SendMessage函数的不同,并能够使用Outputdebugs...
正确调用PostMessage()。
正确调用SendMessage()
比较PostMessage 函数和SendMessage 函数的不同,并能够使用
Outputdebugstr 函数输出结果。
需完成以上条件,可以的话写一下注释 展开
正确调用SendMessage()
比较PostMessage 函数和SendMessage 函数的不同,并能够使用
Outputdebugstr 函数输出结果。
需完成以上条件,可以的话写一下注释 展开
3个回答
展开全部
我用控制台给你输出结果 你要是想知道原理 可以给你再解释下
简单说就是 SendMessage 直接将消息发送到窗口回调函数--阻塞函数
PostMessage将消息发到系统消息队列 由系统消息队列发送到窗口进程消息队列 再从窗口进程消息队列被GetMessage提出来被DispatchMessage发送到窗口回调函数--非阻塞
顺便问问 你是想学还是交作业来着? 下面这段代码有你要的内容 也有一些小东西 可 窗口检测有鼠标左键单击 和上下左右移动文本框 以自己调试着玩玩 希望对你有帮助
#include "stdafx.h"
#include "stdio.h"
HINSTANCE g_hInstance = 0;
HANDLE g_hOutput = 0;
#define WM_MYMESSAGE WM_USER + 1001
int number = 0;
int Text_x = 100;
int Text_y = 100;
void OnPaint (HWND hWnd)
{
char pszText[256] = "WM_PAINT";
sprintf (pszText, "%s = %d\n", pszText, ++number);
WriteConsole (g_hOutput, pszText, strlen (pszText), NULL, NULL);
PAINTSTRUCT ps = {0};
HDC hdc = BeginPaint (hWnd, &ps);
TextOut (hdc, Text_x, Text_y, "我勒个去!", 9);
EndPaint (hWnd, &ps);
}
void OnKeyDown (HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf (szText, "WM_KEYDOWN:%08X\n", wParam);
WriteConsole (g_hOutput, szText, strlen (szText), NULL, NULL);
switch (wParam)
{
case 0x25:
Text_x -= 10;
break;
case 0x26:
Text_y -= 10;
break;
case 0x27:
Text_x += 10;
break;
case 0x28:
Text_y += 10;
break;
}
if (Text_x < 0 || Text_x >= 1000)
Text_x = 0;
if (Text_y < 0 || Text_y >= 1000)
Text_y = 0;
InvalidateRect (hWnd, NULL, true);
}
void OnKeyUp (HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf (szText, "WM_KEYUP:%08X\n", wParam);
WriteConsole (g_hOutput, szText, strlen (szText), NULL, NULL);
}
void OnChar (HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf (szText, "WM_CHAR:%08X\n", wParam);
WriteConsole (g_hOutput, szText, strlen (szText), NULL, NULL);
}
//窗口处理函数
LRESULT CALLBACK WndProc (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_CHAR:
OnChar (hWnd, wParam);
break;
case WM_KEYDOWN:
OnKeyDown (hWnd, wParam);
break;
case WM_KEYUP:
OnKeyUp (hWnd, wParam);
break;
case WM_LBUTTONDOWN:
InvalidateRect (hWnd, NULL, true);//将整个窗口声明为窗口无效区域(等着GetMessage()检查)
break;
case WM_PAINT:
OnPaint (hWnd);
break;
case WM_MYMESSAGE:
MessageBox (NULL, "自己的消息被处理", "Infor", MB_OK);
break;
case WM_CREATE:
// SendMessage (hWnd, WM_MYMESSAGE, 1, 2);//发送到自己的处理函数(这里是WndProc())
PostMessage (hWnd, WM_MYMESSAGE, 1, 2);//发送到系统消息队列
break;
case WM_DESTROY:
// PostQuitMessage (0);
// SendMessage (hWnd, WM_QUIT, 0, 0);
PostMessage (hWnd, WM_QUIT, 0, 0);
break;
}
return DefWindowProc (hWnd, nMsg, wParam, lParam);
}
//注册窗口
bool Register (LPSTR lpClassName, WNDPROC wndproc)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof ( wce );
wce.cbClsExtra = 200;
wce.cbWndExtra = 200;
wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInstance;
wce.lpfnWndProc = wndproc;
wce.lpszClassName = lpClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM nAtom = RegisterClassEx (&wce);
if (nAtom == 0)
return false;
return true;
}
//创建主窗口
HWND CreateMain (LPSTR lpClassName, LPSTR lpWndName)
{
HWND hWnd = CreateWindowEx (0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
return hWnd;
}
//显示窗口
void Display ( HWND hWnd )
{
ShowWindow ( hWnd, SW_SHOW );
UpdateWindow ( hWnd );
}
//消息循环
void Message ()
{
MSG nMsg = { 0 };
/* while ( GetMessage ( &nMsg, NULL, 0, 0 ) )
{
TranslateMessage ( &nMsg );
DispatchMessage ( &nMsg );
}*/
while (1)
{
if ( PeekMessage (&nMsg, NULL, 0, 0, PM_NOREMOVE) )
{
//有消息
if ( ! GetMessage ( &nMsg, NULL, 0, 0 ) )
{
return ;
}
else
{
TranslateMessage ( &nMsg );
DispatchMessage ( &nMsg );
}
}
else
{
//空闲处理
// WriteConsole ( g_hOutput, "Idle", 4, NULL, NULL );
}
}
}
void SetExtra ( HWND hWnd )
{
SetClassLong ( hWnd, 196, 100 );
SetWindowLong ( hWnd, 0, 100 );
}
void GetExtra ( HWND hWnd )
{
DWORD nClassExtra = GetClassLong ( hWnd, 196 );
DWORD nWndExtra = GetWindowLong ( hWnd, 0 );
char szText[256] = { 0 };
sprintf (szText, "窗口类=%d,窗口=%d", nClassExtra, nWndExtra);
MessageBox ( NULL, szText, "infor", MB_OK);
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
AllocConsole ();
g_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
g_hInstance = hInstance;
if (! Register ("Main", WndProc))
{
MessageBox (NULL , "注册失败", "Error", MB_OK);
return -1;
}
HWND hWnd = CreateMain ("Main", "window");
Display (hWnd);
Message ();
return 0;
}
简单说就是 SendMessage 直接将消息发送到窗口回调函数--阻塞函数
PostMessage将消息发到系统消息队列 由系统消息队列发送到窗口进程消息队列 再从窗口进程消息队列被GetMessage提出来被DispatchMessage发送到窗口回调函数--非阻塞
顺便问问 你是想学还是交作业来着? 下面这段代码有你要的内容 也有一些小东西 可 窗口检测有鼠标左键单击 和上下左右移动文本框 以自己调试着玩玩 希望对你有帮助
#include "stdafx.h"
#include "stdio.h"
HINSTANCE g_hInstance = 0;
HANDLE g_hOutput = 0;
#define WM_MYMESSAGE WM_USER + 1001
int number = 0;
int Text_x = 100;
int Text_y = 100;
void OnPaint (HWND hWnd)
{
char pszText[256] = "WM_PAINT";
sprintf (pszText, "%s = %d\n", pszText, ++number);
WriteConsole (g_hOutput, pszText, strlen (pszText), NULL, NULL);
PAINTSTRUCT ps = {0};
HDC hdc = BeginPaint (hWnd, &ps);
TextOut (hdc, Text_x, Text_y, "我勒个去!", 9);
EndPaint (hWnd, &ps);
}
void OnKeyDown (HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf (szText, "WM_KEYDOWN:%08X\n", wParam);
WriteConsole (g_hOutput, szText, strlen (szText), NULL, NULL);
switch (wParam)
{
case 0x25:
Text_x -= 10;
break;
case 0x26:
Text_y -= 10;
break;
case 0x27:
Text_x += 10;
break;
case 0x28:
Text_y += 10;
break;
}
if (Text_x < 0 || Text_x >= 1000)
Text_x = 0;
if (Text_y < 0 || Text_y >= 1000)
Text_y = 0;
InvalidateRect (hWnd, NULL, true);
}
void OnKeyUp (HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf (szText, "WM_KEYUP:%08X\n", wParam);
WriteConsole (g_hOutput, szText, strlen (szText), NULL, NULL);
}
void OnChar (HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf (szText, "WM_CHAR:%08X\n", wParam);
WriteConsole (g_hOutput, szText, strlen (szText), NULL, NULL);
}
//窗口处理函数
LRESULT CALLBACK WndProc (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_CHAR:
OnChar (hWnd, wParam);
break;
case WM_KEYDOWN:
OnKeyDown (hWnd, wParam);
break;
case WM_KEYUP:
OnKeyUp (hWnd, wParam);
break;
case WM_LBUTTONDOWN:
InvalidateRect (hWnd, NULL, true);//将整个窗口声明为窗口无效区域(等着GetMessage()检查)
break;
case WM_PAINT:
OnPaint (hWnd);
break;
case WM_MYMESSAGE:
MessageBox (NULL, "自己的消息被处理", "Infor", MB_OK);
break;
case WM_CREATE:
// SendMessage (hWnd, WM_MYMESSAGE, 1, 2);//发送到自己的处理函数(这里是WndProc())
PostMessage (hWnd, WM_MYMESSAGE, 1, 2);//发送到系统消息队列
break;
case WM_DESTROY:
// PostQuitMessage (0);
// SendMessage (hWnd, WM_QUIT, 0, 0);
PostMessage (hWnd, WM_QUIT, 0, 0);
break;
}
return DefWindowProc (hWnd, nMsg, wParam, lParam);
}
//注册窗口
bool Register (LPSTR lpClassName, WNDPROC wndproc)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof ( wce );
wce.cbClsExtra = 200;
wce.cbWndExtra = 200;
wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInstance;
wce.lpfnWndProc = wndproc;
wce.lpszClassName = lpClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM nAtom = RegisterClassEx (&wce);
if (nAtom == 0)
return false;
return true;
}
//创建主窗口
HWND CreateMain (LPSTR lpClassName, LPSTR lpWndName)
{
HWND hWnd = CreateWindowEx (0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
return hWnd;
}
//显示窗口
void Display ( HWND hWnd )
{
ShowWindow ( hWnd, SW_SHOW );
UpdateWindow ( hWnd );
}
//消息循环
void Message ()
{
MSG nMsg = { 0 };
/* while ( GetMessage ( &nMsg, NULL, 0, 0 ) )
{
TranslateMessage ( &nMsg );
DispatchMessage ( &nMsg );
}*/
while (1)
{
if ( PeekMessage (&nMsg, NULL, 0, 0, PM_NOREMOVE) )
{
//有消息
if ( ! GetMessage ( &nMsg, NULL, 0, 0 ) )
{
return ;
}
else
{
TranslateMessage ( &nMsg );
DispatchMessage ( &nMsg );
}
}
else
{
//空闲处理
// WriteConsole ( g_hOutput, "Idle", 4, NULL, NULL );
}
}
}
void SetExtra ( HWND hWnd )
{
SetClassLong ( hWnd, 196, 100 );
SetWindowLong ( hWnd, 0, 100 );
}
void GetExtra ( HWND hWnd )
{
DWORD nClassExtra = GetClassLong ( hWnd, 196 );
DWORD nWndExtra = GetWindowLong ( hWnd, 0 );
char szText[256] = { 0 };
sprintf (szText, "窗口类=%d,窗口=%d", nClassExtra, nWndExtra);
MessageBox ( NULL, szText, "infor", MB_OK);
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
AllocConsole ();
g_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
g_hInstance = hInstance;
if (! Register ("Main", WndProc))
{
MessageBox (NULL , "注册失败", "Error", MB_OK);
return -1;
}
HWND hWnd = CreateMain ("Main", "window");
Display (hWnd);
Message ();
return 0;
}
镭速传输
2024-10-28 广告
2024-10-28 广告
作为深圳市云语科技有限公司的一员,我们推出的FTP替代升级方案,旨在解决传统FTP在安全性、效率、稳定性及管理方面的不足。我们的产品通过采用自主研发的Raysync传输协议,实现高效、安全的文件传输,即使在恶劣网络环境下也能确保传输的稳定性...
点击进入详情页
本回答由镭速传输提供
展开全部
这个除非有现成的代码,要不就应该没人单独给你写,还是自己慢慢学着练习吧,这样理解比较深刻。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-27
展开全部
在中国《政治经济学》书中、有《马克思》所讲的《剩余价值论》和《剩余价值率》上所明确了超过一定的标准就是“剥削”而目前《国家电网》对“农电工”除了工资、奖金、福利待遇没有按照《国家电网》工作人员支付外其它的“工作管理、考核、工作要求标准”等都执行是一样标准、这就是纯粹的“对农电工的剥削”如果有关部门问紧了,它们就说是《劳务派谴》等进行脱词......
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询