急求怎么用C++ 编写一窗口应用程序?能实现简单的画线功能。
主要说下具体步骤和要下的软件;代码有的话就麻烦发下给我a.bcxyz123789@163.com,没代码也没关系。...
主要说下具体步骤和要下的软件;
代码有的话就麻烦发下给我a.bcxyz123789@163.com,
没代码也没关系。 展开
代码有的话就麻烦发下给我a.bcxyz123789@163.com,
没代码也没关系。 展开
1个回答
展开全部
编译软件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) ;
}
文件-新建-工程-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) ;
}
追问
这些代码只能画一条线;
麻烦再帮我解决一下,
让它可以存在2条以上的线 ;
谢谢
追答
字数限制,前面不变 后面这个回调函数覆盖下
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cx,cy,count=0;
static POINT ptStart[100],ptEnd[100];
PAINTSTRUCT ps;
HDC hdc;
int i;
TCHAR str[50];
switch(message)
{
case WM_CREATE:
return 0;
case WM_SIZE:
cx=LOWORD(lParam);
cy=HIWORD(lParam);
return 0;
case WM_LBUTTONDOWN:
ptStart[count].x=LOWORD(lParam);
ptStart[count].y=HIWORD(lParam);
SetCapture(hwnd);
return 0;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON)
{
ptEnd[count].x=LOWORD(lParam);
ptEnd[count].y=HIWORD(lParam);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_LBUTTONUP:
ReleaseCapture();
count++;
return 0;
case WM_RBUTTONDOWN:
for(i=0;i<count;i++)
{
ptStart[i].x=0;
ptStart[i].y=0;
ptEnd[i].x=0;
ptEnd[i].y=0;
}
count=0;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(i=0;i<=count;i++)
{
MoveToEx(hdc,ptStart[i].x,ptStart[i].y,NULL);
LineTo(hdc,ptEnd[i].x,ptEnd[i].y);
wsprintf(str,"%d",i);
TextOut(hdc,0,0,str,lstrlen(str));
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询