1.主程序:
#include "windows.h"
#include "fh.h"
HINSTANCE hinst;
POINT point;
POINT fhq;LPCTSTR lpszAppName="Generic";
LPCTSTR lpszTitle="Generic Application";
BOOL Registerwin95(CONST WNDCLASS* LPWC);
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
MSG msg;
HWND hwnd;
WNDCLASS WC;
WC.style=CS_HREDRAW|CS_VREDRAW;
WC.lpfnWndProc=(WNDPROC)WndProc;
WC.cbClsExtra=0;
WC.cbWndExtra=0;
WC.hInstance=0;
WC.hCursor=LoadCursor(NULL,IDC_ARROW);
WC.hIcon=LoadIcon(hInstance,lpszAppName);
WC.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
WC.lpszMenuName=lpszAppName;
WC.lpszClassName=lpszAppName;
if (!Registerwin95(&WC))
return false;
hinst=hInstance;
hwnd=CreateWindow(lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,
CW_USEDEFAULT,0,
NULL,
NULL,
hInstance,
NULL
);
if(!hwnd)
return false;
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{ TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
BOOL Registerwin95(CONST WNDCLASS* LPWC)
{ WNDCLASSEX wcex;
wcex.style=LPWC->style;
wcex.lpfnWndProc=LPWC->lpfnWndProc;
wcex.cbClsExtra=LPWC->cbClsExtra;
wcex.cbWndExtra=LPWC->cbWndExtra;
wcex.hInstance=LPWC->hInstance;
wcex.hIcon=LPWC->hIcon;
wcex.hCursor=LPWC->hCursor;
wcex.hbrBackground=LPWC->hbrBackground;
wcex.lpszMenuName=LPWC->lpszMenuName;
wcex.lpszClassName=LPWC->lpszClassName;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.hIconSm=LoadIcon(wcex.hInstance,"SMALL");
return RegisterClassEx(&wcex);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
{ HDC hdc;
static char cLeftBtn[]="LEFT BUTTON";
switch (uMsg)
{ case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);
SetBkColor(hdc,RGB(255,255,0));
TextOut(hdc,0,0,cLeftBtn,strlen(cLeftBtn));
ReleaseDC(hwnd,hdc);
point.x=LOWORD(lParam);
point.y=HIWORD(lParam);break;
case WM_LBUTTONUP:
fhq.x=LOWORD(lParam);
fhq.y=LOWORD(lParam);
CClientDC dc(this);
//HDC hdc;
//hdc=::GetDC(hwnd);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CPen *p=dc.SelectObject(&pen);
dc.MoveTo(point);
dc.LineTo(fhq);
dc.SelectObject(p);
//MoveToEx(hdc,point.x,point.y,NULL);
//LineTo(hdc,fhq.x,fhq.y);
//ReleaseDC(hwnd,hdc);
//dc.SelectObject(p);
//MessageBox(hwnd,"我爱你!!","好呀",NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_TEST:
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hwnd,uMsg,wParam,lParam));
}
return(0L);
}
2.头文件fh.h
#define IDM_EXIT 100
#define IDM_TEST 200
#define IDM_ABOUT 300
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK About(HWND,UINT,WPARAM,LPARAM);
#include "windows.h"
#include "fh.h"
HINSTANCE hinst;
POINT point;
POINT fhq;LPCTSTR lpszAppName="Generic";
LPCTSTR lpszTitle="Generic Application";
BOOL Registerwin95(CONST WNDCLASS* LPWC);
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
MSG msg;
HWND hwnd;
WNDCLASS WC;
WC.style=CS_HREDRAW|CS_VREDRAW;
WC.lpfnWndProc=(WNDPROC)WndProc;
WC.cbClsExtra=0;
WC.cbWndExtra=0;
WC.hInstance=0;
WC.hCursor=LoadCursor(NULL,IDC_ARROW);
WC.hIcon=LoadIcon(hInstance,lpszAppName);
WC.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
WC.lpszMenuName=lpszAppName;
WC.lpszClassName=lpszAppName;
if (!Registerwin95(&WC))
return false;
hinst=hInstance;
hwnd=CreateWindow(lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,
CW_USEDEFAULT,0,
NULL,
NULL,
hInstance,
NULL
);
if(!hwnd)
return false;
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{ TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
BOOL Registerwin95(CONST WNDCLASS* LPWC)
{ WNDCLASSEX wcex;
wcex.style=LPWC->style;
wcex.lpfnWndProc=LPWC->lpfnWndProc;
wcex.cbClsExtra=LPWC->cbClsExtra;
wcex.cbWndExtra=LPWC->cbWndExtra;
wcex.hInstance=LPWC->hInstance;
wcex.hIcon=LPWC->hIcon;
wcex.hCursor=LPWC->hCursor;
wcex.hbrBackground=LPWC->hbrBackground;
wcex.lpszMenuName=LPWC->lpszMenuName;
wcex.lpszClassName=LPWC->lpszClassName;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.hIconSm=LoadIcon(wcex.hInstance,"SMALL");
return RegisterClassEx(&wcex);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
{ HDC hdc;
static char cLeftBtn[]="LEFT BUTTON";
switch (uMsg)
{ case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);
SetBkColor(hdc,RGB(255,255,0));
TextOut(hdc,0,0,cLeftBtn,strlen(cLeftBtn));
ReleaseDC(hwnd,hdc);
point.x=LOWORD(lParam);
point.y=HIWORD(lParam);break;
case WM_LBUTTONUP:
fhq.x=LOWORD(lParam);
fhq.y=LOWORD(lParam);
CClientDC dc(this);
//HDC hdc;
//hdc=::GetDC(hwnd);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CPen *p=dc.SelectObject(&pen);
dc.MoveTo(point);
dc.LineTo(fhq);
dc.SelectObject(p);
//MoveToEx(hdc,point.x,point.y,NULL);
//LineTo(hdc,fhq.x,fhq.y);
//ReleaseDC(hwnd,hdc);
//dc.SelectObject(p);
//MessageBox(hwnd,"我爱你!!","好呀",NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_TEST:
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hwnd,uMsg,wParam,lParam));
}
return(0L);
}
2.头文件fh.h
#define IDM_EXIT 100
#define IDM_TEST 200
#define IDM_ABOUT 300
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK About(HWND,UINT,WPARAM,LPARAM);
#include <afxwin.h> //MFC core and standard components
#include <afxext.h> //MFC extensions
#include <afxdtctl.h> //MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> //MFC support for Windows Common Controls
#endif _AFX_NO_AFXCMN_SUPPORT