#include<windows.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int nMode;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="映射模式";
char lpszTitle[]="P103";
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
    wndclass.lpszClassName=lpszClassName;
wndclass.lpszMenuName=NULL;
wndclass.style=0; if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
} hwnd =CreateWindow
(
lpszClassName,
    lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
); ShowWindow(hwnd,nCmdShow);
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;
HBRUSH hB1,hB2,hB3,hB4,hB5;
HPEN hP;
switch(message)
{
case WM_CREATE:
hP = CreatePen(PS_DASHDOT,25,RGB(50,125,40));
break; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetMapMode(hdc,nMode);

SelectObject(hdc,hP);
MoveToEx(hdc,0,10,NULL);
LineTo(hdc,700,600);

hB1=(HBRUSH)GetStockObject(WHITE_BRUSH);
SelectObject(hdc,hB1);
Rectangle(hdc,0,0,360,900); hB2=(HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hdc,hB2);
Rectangle(hdc,360,0,720,900);       hB3=(HBRUSH)GetStockObject(GRAY_BRUSH);
SelectObject(hdc,hB3);
Rectangle(hdc,720,0,1080,900);      hB4=(HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hdc,hB4);
Rectangle(hdc,1080,0,1440,900); hB5=(HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hdc,hB5);
     Pie(hdc,0,0,300,200,50,50,300,50); EndPaint(hwnd,&ps);
return 0;

break;
case WM_DESTROY:
DeleteObject(hB1);
DeleteObject(hB2);
DeleteObject(hB3);
DeleteObject(hB4);
DeleteObject(hB5);
DeleteObject(hP); PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;

}

解决方案 »

  1.   

    你的问题很多呀
    WM_CREATE只在createwindow的时候才调用,所以CreatePen只被调用了一次,而你这里用的是局部变量,需要每次都CreatePen才行,
    你最好把HBRUSH hB1,hB2,hB3,hB4,hB5; 
    HPEN hP; 
    都设成全局变量吧,要不然你得把DeleteObject(hB1); 
    DeleteObject(hB2); 
    DeleteObject(hB3); 
    DeleteObject(hB4); 
    DeleteObject(hB5); 
    DeleteObject(hP); 
    放到EndPaint(hwnd,&ps); 的后面
    因为你在收到case WM_DESTROY: 的时候并不执行前面的hB1=(HBRUSH)GetStockObject(WHITE_BRUSH); 
    SelectObject(hdc,hB1); 会出错,
    你还是去找本windows程序设计的书来看吧
      

  2.   

    --------------------Configuration: 498 - Win32 Debug--------------------
    Compiling...
    498.cpp
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/498.exe : fatal error LNK1120: 1 unresolved externals
    执行 link.exe 时出错.498.exe - 1 error(s), 0 warning(s)
    帮忙以后怎么可以解决这个问题不胜感激! 
      

  3.   

    Windows项目要使用Windows子系统, 而不是Console, 可以这样设置: 
    [Project] --> [Settings] --> 选择"Link"属性页, 在Project Options中将/subsystem:console改成/subsystem:windows