#include<windows.h>
int i=0,j=0,k=0;
HWND hwnd=GetDesktopWindow();
HDC hdc=GetWindowDC(hwnd);
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:

for(i=0;i<1024;i++)
{
for(j=0;j<768;j++)
{
SetPixelV(hdc,i,j,0xffffff);
}
}
return 0;
}
}鼠标左键按下后桌面并没有变白,为什么会这样呢?

解决方案 »

  1.   

    在winproc函数里设置断点,检查错误。
      

  2.   

    顶.
    LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);这个方法没有用起来.
      

  3.   

    你可以 用vc6.0 编译器自动创建一个 控制台窗口程序 看看那个系统回调函数就明白了,我猜测 lz 创建窗口时没成功或者至少回调函数没有关联号..
      

  4.   

    修改代码如下#include<windows.h>
    int i=0,j=0,k=0;
    HWND hwnd=GetDesktopWindow();
    HDC hdc=GetWindowDC(hwnd);
    MSG msg;
    WNDCLASS wndclass;
    LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
    {
        RegisterClass(&wndclass);//注册窗口类
        while(GetMessage(&msg,NULL,0,0))
        {
            DispatchMessage(&msg);
        }
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
    {
        switch (message)
        {
            case WM_LBUTTONDOWN:
        
                for(i=0;i<1024;i++)
                {
                    for(j=0;j<768;j++)
                    {
                        SetPixelV(hdc,i,j,0xffffff);
                    }
                }
                return 0;
        }
    }鼠标左键按下后桌面还是没有变白,为什么会这样呢?
      

  5.   

    修改代码如下:#include<windows.h>
    int i=0,j=0,k=0;
    HWND hwnd=GetDesktopWindow();
    HDC hdc=GetWindowDC(hwnd);
    MSG msg;
    WNDCLASS wndclass;
    LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
    {
        RegisterClass(&wndclass);//注册窗口类
        wndclass.lpfnWndProc=WndProc;//窗口消息处理
        while(GetMessage(&msg,NULL,0,0))
        {
            DispatchMessage(&msg);
        }
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
    {
        switch (message)
        {
            case WM_LBUTTONDOWN:
        
                for(i=0;i<1024;i++)
                {
                    for(j=0;j<768;j++)
                    {
                        SetPixelV(hdc,i,j,0xffffff);
                    }
                }
                return 0;
        }
    }鼠标左键按下后还是没反应
      

  6.   

    算了,你不去看书,给你个例子吧
    #include "stdafx.h" 
    #include <windows.h>
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
      int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
      PSTR szCmdLine, int iCmdShow)
    {
      static TCHAR szAppName[] = TEXT ("HelloWin") ;   HWND         hwnd ;
     MSG          msg ;WNDCLASS     wc ;
    wc.style         = CS_HREDRAW | CS_VREDRAW ;
       wc.lpfnWndProc   = WndProc ;
      wc.cbClsExtra    = 0 ;
       wc.cbWndExtra    = 0 ;
       wc.hInstance     = hInstance ;
     wc.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
       wc.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
        wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
      wc.lpszMenuName  = NULL ;
         wc.lpszClassName = szAppName ;
       if (!RegisterClass (&wc))
          {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;
           return 0 ;     }
         hwnd = CreateWindow (szAppName,                  // window class name
                            TEXT ("欢迎你的到来!"), // window caption
                            WS_OVERLAPPEDWINDOW,        // window style
                           CW_USEDEFAULT,              // initial x position
                             CW_USEDEFAULT,              // initial y position
                              CW_USEDEFAULT,              // initial x size
                            CW_USEDEFAULT,              // initial y size
                           NULL,                       // parent window handle
                             NULL,                       // window menu handle
                             hInstance,                  // program instance handle
                             NULL) ;                     // creation parameters
             ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;
        while (GetMessage (&msg, NULL, 0, 0))
         {
          TranslateMessage (&msg) ;
             DispatchMessage (&msg) ;
     }
        return msg.wParam ;}
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC         hdc ;
        PAINTSTRUCT ps ;
        RECT        rect ;
        switch (message)
         {
     case WM_PAINT:
            hdc = BeginPaint (hwnd, &ps) ;
             GetClientRect (hwnd, &rect) ;
            DrawText (hdc, TEXT ("你好,欢迎你来到VC之路!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
            EndPaint (hwnd, &ps) ;
            return 0 ;
        case WM_DESTROY:
              PostQuitMessage (0) ;
             return 0 ;
         }
       return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
      

  7.   

    你的回调过程没有调用。可能写的不对。
    给你找了个例子,比较简单,对比一下吧。http://zifeiyu520yang.blog.sohu.com/115402088.html
      

  8.   

    还没有学习windows程序呢。惭愧。
      

  9.   

    10楼,非要创建窗口吗?直接画桌面窗口不行吗?比如这段代码我试过确实是可以运行成功的。#include<windows.h>
    int i=0,j=0;
    HDC hdc=GetWindowDC(GetDesktopWindow());
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
    {
    for(i=0;i<1024;i++)
    {
    for(j=0;j<768;j++)
    {
    SetPixelV(hdc,i,j,0xffffff);
    }
    }
    }桌面确实全白了
      

  10.   

    你这个是直接在WinMain里处理的是可以
    可是你上面给出的代码是在回调函数里处理的绘制啊
    你要调用回调函数就得有个窗口类来调用啊
      

  11.   

    楼主去看windows编程关于鼠标&键盘消息的那部分吧。你的程序里处理的是自己进程的消息,但你的进程根本就没有创建窗口,所以当然不会处理鼠标消息。所以楼主的解决方案有3个
    1. 创建一个窗口,在窗口中处理鼠标消息。这个就不多说了。2. 用Hook截获其他窗口的鼠标事件,这个需要写个DLL把hook的代码写在dll中。楼主可以搜索SetWindowsHookEx3. 子类化Desktop Windows,让这个窗口自己处理鼠标消息。这个也需要写个DLL,在DLL中用SetWindowLongPtr。楼主可以搜索"DLL注入"
      

  12.   

    你在14楼说的桌面全白了,其实不是桌面白了,是你新建的窗口白了。你按你一下windows键,就看到原来的桌面了。
      

  13.   


    不是注册窗口类的问题,而是要有个实际的窗口来处理鼠标消息。楼主找本书去看看windows的消息机制吧。
      

  14.   

    20楼,桌面窗口不是窗口吗?HWND hwnd=GetDesktopWindow();
      

  15.   


    你用spy++看看
    桌面窗口名是ProgrammeManager
    实体桌面其实是个ListCtrl
      

  16.   

    1.你要注入到桌面 explorer.exe
    2. 你应该setwindowlong,hook 窗口的winproc为你自己的winproc
    3. 在你的winproc里处理 鼠标消息和paint消息ls几位给的是创建窗口,处理winproc,和lz说的在桌面上画不是一个事