急求怎么用C++ 编写一窗口应用程序?能实现简单的画线功能。7
发布网友
发布时间:2024-02-27 16:29
我来回答
共1个回答
热心网友
时间:2024-03-22 05:01
编译软件VC 6.0
文件-新建-工程-Win32 Application-OK
文件-新建-文件-C++sources文件
左边点开新建的 文件 复制代码。。。编译运行
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
TCHAR SoftName[]=TEXT("TEXTSOFT");
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, SoftName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = SoftName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
SoftName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (SoftName, SoftName,
WS_OVERLAPPEDWINDOW,
GetSystemMetrics (SM_CXSCREEN) / 4,
GetSystemMetrics (SM_CYSCREEN) / 4,
GetSystemMetrics (SM_CXSCREEN) / 2,
GetSystemMetrics (SM_CYSCREEN) / 2,
NULL, NULL, hInstance, NULL) ;
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)
{
static int cx,cy;
static POINT pt[2];
PAINTSTRUCT ps;
HDC hdc;
switch(message)
{
case WM_CREATE:
return 0;
case WM_SIZE:
cx=LOWORD(lParam);
cy=HIWORD(lParam);
return 0;
case WM_LBUTTONDOWN:
pt[0].x=LOWORD(lParam);
pt[0].y=HIWORD(lParam);
return 0;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON)
{
pt[1].x=LOWORD(lParam);
pt[1].y=HIWORD(lParam);
}
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
MoveToEx(hdc,pt[0].x,pt[0].y,0);
LineTo(hdc,pt[1].x,pt[1].y);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}