丹东市中心医院:c语言小程序设计

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 19:48:40
在屏幕随意作图程序:程序运行时,可以用鼠标画出图形,当鼠标在某一点被按下时该点就被画出,并画出鼠标拖动的轨迹。

有一本书是关于C语言的。

C语言精彩一百例

你可以去书店找找这本书,上面的程序很好的,我就曾以看过这本书,当然,我没有看完的,后来还给别人了。

技术要点:
1、用鼠标中断号
2、编写鼠标函数
1、获取击键状态函数
2、获取鼠标在屏幕位置函数
3、描画鼠标轨迹函数
3、具体的操作请自己写吧,相关的知识要点在相关的网络上都有!可以找到

在 Windwos程序设计 一书中有一个你要的程序 和 源码
可以到网上找到电子书 和 源码

自己写的,用vc6.0

// MouseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Mouse.h"
#include "MouseDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMouseDlg dialog

CMouseDlg::CMouseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMouseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMouseDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMouseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMouseDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMouseDlg, CDialog)
//{{AFX_MSG_MAP(CMouseDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMouseDlg message handlers

BOOL CMouseDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_bCursour=FALSE;
// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CMouseDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMouseDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMouseDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CMouseDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CDialog::OnLButtonDblClk(nFlags, point);
}

void CMouseDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CDialog::OnLButtonUp(nFlags, point);
}

void CMouseDlg::OnMouseMove(UINT nFlags, CPoint point)
{

CClientDC dc(this);

if((nFlags&MK_LBUTTON)==MK_LBUTTON)
{
CPen lpen(PS_SOLID,4,RGB(255,0,0));
dc.SelectObject(&lpen);
dc.MoveTo(m_prevX,m_prevY);
dc.LineTo(point.x,point.y);
m_prevX=point.x;
m_prevY=point.y;
}
else if((nFlags&MK_RBUTTON)==MK_RBUTTON)
{
CPen lpen(PS_SOLID,16,RGB(0,0,255));
dc.SelectObject(&lpen);
dc.MoveTo(m_prevX,m_prevY);
dc.LineTo(point.x,point.y);
m_prevX=point.x;
m_prevY=point.y;
}

CDialog::OnMouseMove(nFlags, point);
}

void CMouseDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
m_prevX=point.x;
m_prevY=point.y;

CDialog::OnLButtonDown(nFlags, point);
}

void CMouseDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
m_prevX=point.x;
m_prevY=point.y;
CDialog::OnRButtonDown(nFlags, point);
}

void CMouseDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
char lsChar;
HCURSOR lhCursor;
lsChar = char(nChar);
if(lsChar=='A')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_ARROW);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='B')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_IBEAM);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='C')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_WAIT);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='D')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='H')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENESW);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='F')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='G')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='I')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='J')
{
lhCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENS);
m_bCursour=TRUE;
SetCursor(lhCursor);
}
else if(lsChar=='X')
{
m_bCursour=FALSE;
OnOK();
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CMouseDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if(m_bCursour)
return TRUE;
else
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

你牛~~~~~~~~~
小弟佩服
(把程序写在上面的那位仁兄)

开玩笑,这还算小啊。