代码没发现错误.不过你画线之前,应该先创建画笔(CreatePen),并选入HDC(SelectObject)

解决方案 »

  1.   

    #include<windows.h>
    #include<stdio.h>LRESULT CALLBACK MyWindowProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    );
    int WINAPI WinMain(
      HINSTANCE hInstance,      // handle to current instance
      HINSTANCE hPrevInstance,  // handle to previous instance
      LPSTR lpCmdLine,          // command line
      int nCmdShow              // show state
    )
    {
    MSG msg;
    static TCHAR szClassName[]=TEXT("Hello Windows");
    HWND hwnd;
        WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndclass.hIcon=LoadIcon(NULL,IDI_WARNING);
        wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=MyWindowProc;
    wndclass.lpszClassName=szClassName;
    wndclass.lpszMenuName=0;
    wndclass.style=CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wndclass); hwnd=CreateWindow(szClassName,"WoQu",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
    CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,NULL,NULL))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }LRESULT CALLBACK MyWindowProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    )
    {
    PAINTSTRUCT ps;
    switch(uMsg)
    {
    case WM_PAINT:
    HDC hdc;
    hdc=BeginPaint(hwnd,&ps);
    MoveToEx(hdc,100,120,NULL);
    LineTo(hdc,200,250);
    EndPaint(hwnd,&ps);
    break;

    default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;}
      

  2.   

    修改这样试一下,把HDC定义在前面,不能在CASE里面定义变量,或者用{}包含也可以:
    HDC hdc;
    switch(uMsg)
    {
    case WM_PAINT:
    hdc=BeginPaint(hwnd,&ps);
    MoveToEx(hdc,100,120,NULL);
    LineTo(hdc,200,250);
    EndPaint(hwnd,&ps);
    break;default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    另外严谨的做法应该是建立画笔,因为默认的画笔可以与底色一样,也可能看不到线,这样不好定位错误
      

  3.   

    改过之后还是运行不了啊  在我的另外一个程序是可以的
    到底是什么情况啊
    代码如下
    #include<windows.h>
    #include<stdio.h>LRESULT CALLBACK MyWindowProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    );
    int WINAPI WinMain(
      HINSTANCE hInstance,      // handle to current instance
      HINSTANCE hPrevInstance,  // handle to previous instance
      LPSTR lpCmdLine,          // command line
      int nCmdShow              // show state
    )
    {
    MSG msg;
    static TCHAR szClassName[]=TEXT("Hello Windows");
    HWND hwnd;
        WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndclass.hIcon=LoadIcon(NULL,IDI_WARNING);
        wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=MyWindowProc;
    wndclass.lpszClassName=szClassName;
    wndclass.lpszMenuName=0;
    wndclass.style=CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wndclass); hwnd=CreateWindow(szClassName,"WoQu",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
    CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,NULL,NULL))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }LRESULT CALLBACK MyWindowProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    )
    {
    static bool PLAYSND=FALSE;
    static int TEXTPOS=50;
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect; switch(uMsg)
    {
    // case WM_CREATE:
    // PlaySound ("LemonTree.wav", NULL, SND_FILENAME | SND_ASYNC ) ;
    // break; case WM_PAINT:
    TEXTPOS=50;
    hdc=BeginPaint(hwnd,&ps);
    TextOut(hdc,0,0,"窗口你好",strlen("窗口你好"));
    GetClientRect(hwnd,&rect);
    if(TRUE==PLAYSND)
    DrawText(hdc,"正在播放音乐Lemon Tree",-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    // TextOut(hdc,450,230,"正在播放音乐Lemon Tree",strlen("正在播放音乐Lemon Tree"));
    else
    DrawText(hdc,"按空格键播放音乐",-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    // TextOut(hdc,450,230 ,"按空格键播放音乐",strlen("按空格键播放音乐")); MoveToEx(hdc,100,120,NULL);  //画线
                       LineTo(hdc,200,250); EndPaint(hwnd,&ps);
    break;
            
    case WM_CHAR:
    if(('q'==wParam) | ('Q'==wParam))
    DestroyWindow(hwnd);
    else 
    if(' '==wParam)
    {
    PlaySound ("LemonTree.wav", NULL, SND_FILENAME | SND_ASYNC ) ;
    PLAYSND=TRUE;
    InvalidateRect(NULL,NULL,TRUE);
    }
    else
    MessageBox(hwnd,"按Q键也能退出","提示",MB_OK);
    break; case WM_LBUTTONDOWN:
    MessageBox(hwnd,"这是鼠标左键","NOTICE",MB_OK);/* HDC hDc;
         int iWidth;
    hDc=GetDC(hwnd);
    iWidth=GetDeviceCaps(hDc,HORZRES);      //iWidth=1366
    ReleaseDC(hwnd,hDc);
    */
    break; case WM_RBUTTONDOWN:
    HDC hDC;
    hDC=GetDC(hwnd);
    TextOut(hDC,0,TEXTPOS,"点右键干嘛",strlen("点右键干嘛"));
    TEXTPOS+=20;
    ReleaseDC(hwnd,hDC);
    break; case WM_CLOSE:
    if(IDYES==MessageBox(hwnd,"确定关闭吗","提示",MB_YESNO))
    DestroyWindow(hwnd);
    break; case WM_DESTROY:
    PostQuitMessage(0);
    break; default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
    }
      

  4.   


    #include<windows.h>
    #include<stdio.h>LRESULT CALLBACK MyWindowProc( HWND hwnd, // handle to window
                                   UINT uMsg, // message identifier
                                   WPARAM wParam, // first message parameter
                                   LPARAM lParam // second message parameter );
    int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance
                        HINSTANCE hPrevInstance, // handle to previous instance
                        LPSTR lpCmdLine, // command line
                        int nCmdShow // show state )
    {
         MSG msg;
         static TCHAR szClassName[]=TEXT("Hello Windows");
         HWND hwnd;
         WNDCLASS wndclass;
         wndclass.cbClsExtra=0;
         wndclass.cbWndExtra=0;
         wndclass.hbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
         wndclass.hIcon=LoadIcon(NULL,IDI_WARNING);
         wndclass.hInstance=hInstance;
         wndclass.lpfnWndProc=MyWindowProc;
         wndclass.lpszClassName=szClassName;
         wndclass.lpszMenuName=0;
         wndclass.style=CS_HREDRAW | CS_VREDRAW;     RegisterClass(&wndclass);

         hwnd=CreateWindow(szClassName,"WoQu",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
                       CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

         ShowWindow(hwnd,SW_SHOWNORMAL);

         UpdateWindow(hwnd);

         while(GetMessage(&msg,NULL,NULL,NULL))
         {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
          }
          return 0;}LRESULT CALLBACK MyWindowProc( HWND hwnd, // handle to window
                                   UINT uMsg, // message identifier
                                   WPARAM wParam, // first message parameter
                                   LPARAM lParam // second message parameter)
    {
         PAINTSTRUCT ps;
         HDC hdc; // 在这里定义设备属性表(Device Contex)句柄
         switch(uMsg)
         {
          case WM_PAINT:
               hdc=BeginPaint(hwnd,&ps);
               MoveToEx(hdc,100,120,NULL);
               LineTo(hdc,200,250);
               EndPaint(hwnd,&ps);
               break;

          default:
               return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
          return 0;
    }
      

  5.   

    你的画线代码放在WM_PAINT消息里不合适,因为没有触发WM_PAINT消息,可以放在WM_CREAT里面