首页
社区
课程
招聘
[旧帖] [分享]Windows 核心编程 学习—26.5(使用消息发送数据) 0.00雪花
发表于: 2010-9-25 15:31 1073

[旧帖] [分享]Windows 核心编程 学习—26.5(使用消息发送数据) 0.00雪花

2010-9-25 15:31
1073
Windows 核心编程 学习—26.5(使用消息发送数据)

声明:
高手可以略过,若有 高手指点,那 求之不得,本文更多是给自己一点信心,原来我也能学会一点点 ,Windows核心编程,这么难得 怪东西,

本人买了本<<window核心编程>>(第4版)都2年了,看的是一塌糊涂,太难,智力太低,这天又拿出来,摆弄一下,我不按顺序了,什么,线程,作业了,dll库了,直接到P671页,学习—26.5(使用消息发送数据),为什么,要跳,原因是原书的源代码我是一个也没编译通过,我对C++不会,另外再加上MFC类库,看的我是云里雾里,到现在还晕呐,有天我忽然想,我就看书中的理论,用SDK来编程,结果就有了这篇,发现书中的理论如果用SDK的话,可能更容易理解,毕竟MFC是给那些大牛们用的,以下代码是ANSI的,在,原可执行文件运行期间,创建一个进程(这里是打开计算器calc.exe),一般在SystemRoot\system32\calc.exe,我这里是c:\\windows\\system32\\calc.exe,然后睡眠 5秒,主线程将把 消息传给calc.exe进程(即计算器窗口),并把标题 计算器 改为------(A Test Caption)括号不算, 计算机 真的很神奇,我的感受是,象我这样自学,又周围没有同好,自己再不给自己打气,很快 ,就真的泄气了,兴趣,最重要 ,了。。。 。。。
在VC++6.0下编译通过,系统XPsp2,流程图:用画图板画的,有点简陋,大家将就吧,另外代码模板,是才从Windows游戏编程大师技巧(第二版)源码,中 copy 下的,向前辈致敬!
源代码如下:
// DEMO2_3.CPP - A complete windows program

// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include <windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <stdio.h>     
#include <math.h>

// DEFINES ////////////////////////////////////////////////

// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"

// GLOBALS ////////////////////////////////////////////////

// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
                                                    UINT msg,
                            WPARAM wparam,
                            LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT                ps;                // used in WM_PAINT
HDC                                hdc;        // handle to a device context

// what is the message
switch(msg)
        {       
        case WM_CREATE:
        {
                // do initialization stuff here
                        STARTUPINFO si = { sizeof(si) };
                        PROCESS_INFORMATION pi;
                        TCHAR szCommandLine[] = TEXT("Calculator");
                        if(!CreateProcess(TEXT("c:\\windows\\system32\\calc.exe"),szCommandLine,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
                        { MessageBox (NULL, TEXT ("Error !"), TEXT ("Error"), 0);}
                        Sleep(5000);// 等待5秒,这主要是为了方便看到 计算器程序 窗口的标题 计算器 被改为 A Test Caption
                        SendMessage(FindWindow(NULL,"计算器"),WM_SETTEXT,0,(LPARAM)"A Test Caption");
        // return success
                return(0);
                } break;

        case WM_PAINT:
                {
                // simply validate the window
                hdc = BeginPaint(hwnd,&ps);         
                // you would do all your painting here
        EndPaint(hwnd,&ps);

        // return success
                return(0);
                   } break;

        case WM_DESTROY:
                {
                // kill the application, this sends a WM_QUIT message
                PostQuitMessage(0);

        // return success
                return(0);
                } break;

        default:break;

    } // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain(        HINSTANCE hinstance,
                                        HINSTANCE hprevinstance,
                                        LPSTR lpcmdline,
                                        int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create
HWND           hwnd;         // generic window handle
MSG                   msg;                 // generic message

// first fill in the window class stucture
winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style                        = CS_DBLCLKS | CS_OWNDC |
                          CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc        = WindowProc;
winclass.cbClsExtra                = 0;
winclass.cbWndExtra                = 0;
winclass.hInstance                = hinstance;
winclass.hIcon                        = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor                = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground        = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName        = NULL;
winclass.lpszClassName        = WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

// register the window class
if (!RegisterClassEx(&winclass))
        return(0);

// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
                            WINDOW_CLASS_NAME,   // class
                                                    "Your Basic Window", // title
                                                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                                                     0,0,            // initial x,y
                                                    400,400,  // initial width, height
                                                    NULL,            // handle to parent
                                                    NULL,            // handle to menu
                                                    hinstance,// instance of this application
                                                    NULL)))        // extra creation parms
return(0);

// enter main event loop
while(GetMessage(&msg,NULL,0,0))
         {
         // translate any accelerator keys
         TranslateMessage(&msg);

         // send the message to the window proc
         DispatchMessage(&msg);
         } // end while

// return to Windows like this
return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////
如能对阁下有所帮助,也算我有心想回报,久从net上索取的,这几多年!

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 1
支持
分享
最新回复 (1)
雪    币: 77
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
正准备着学,但不知道从什么地方开始学!
2010-9-25 18:48
0
游客
登录 | 注册 方可回帖
返回
//