| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 709 人关注过本帖
标题:[原创]一个关于鼠标的小程序,与大家分享
只看楼主 加入收藏
zihangxy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-27
收藏
 问题点数:0 回复次数:8 
[原创]一个关于鼠标的小程序,与大家分享

/*这是我编的一个在Windows系统下运行的小程序。欢迎大家看看,发表发表您的高见!*/

#include <windows.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

#define BIT(b,i) (((b)>>(i))&0x01)
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void IntToString(int n, char *str);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
char screen[8][128];

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;*/
wincl.hbrBackground = (HBRUSH) GetStockObject((WHITE_BRUSH));

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
0, /* Windows decides the position */
0, /* where the window ends up on the screen */
648, /* The programs width */
349, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}

void Draw(HWND hwnd, HDC hdc)
{
RECT rc,r;
int x,y;
int i,j;
HBRUSH hbrush;
HPEN hpen, hpenblack;
char str[10]={0};

hpen = CreatePen (PS_SOLID, 0, RGB(255,0,0)) ;
hpenblack=CreatePen (PS_SOLID, 0, RGB(0,0,0)) ;
r.left=0;
r.top=0;
r.right=640;
r.bottom=40;
GetClientRect(hwnd,&rc);
IntToString(rc.right,str);
DrawText (hdc, str, -1, &r,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
r.left=0;
r.top=40;
r.right=640;
r.bottom=80;
IntToString(rc.bottom,str);
DrawText (hdc, str, -1, &r,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
for (x=0; x<=rc.right; x+=5)
{
if (x%40==0)
{
SelectObject(hdc, hpen);
MoveToEx (hdc, x, 0, NULL) ;
LineTo(hdc, x, rc.bottom) ;
}
else
{
SelectObject(hdc, hpenblack);
MoveToEx(hdc, x, 0, NULL) ;
LineTo(hdc, x, rc.bottom) ;
}
}
for (y=0; y<=rc.bottom; y+=5)
{
if (y%40==0)
{
SelectObject(hdc, hpen);
MoveToEx(hdc, 0, y, NULL) ;
LineTo(hdc, rc.right, y) ;
}
else
{
SelectObject(hdc,hpenblack);
MoveToEx(hdc, 0, y, NULL) ;
LineTo(hdc, rc.right, y) ;
}
}
hbrush= CreateSolidBrush(255);
for (y=0;y<8;y++)
{
for (x=0;x<128;x++)
{
for (i=0;i<8;i++)
{
if (BIT(screen[y][x],i)==1)
{
rc.left=x*5;
rc.top=(y*8+i)*5;
rc.right=rc.left+5;
rc.bottom=rc.top+5;
FillRect(hdc, &rc, hbrush);
}
}
}
}

}

void IntToString(int n, char *str)
{
int i=0;
while (n>0)
{
str[i++]=n%10+'0';
n=(int)n/10;
}
strrev(str);
}

int write0(char b, int w)
{
return (~(int)pow(2,(w)))&(b);

}

int write1(char b, int w)
{
return (int)(pow(2,(w)))|(b);
}
void StoreData(int x, int y, int data)
{
if (data==0)
screen[y/8][x]=(char)write0(screen[y/8][x],y%8);
else
screen[y/8][x]=(char)write1(screen[y/8][x],y%8);
}

void writeToFile()
{
FILE *fp;
int i, j;

if ((fp=fopen("\\bitmap.txt","w"))==NULL)
{
return ;
}
for (i=0;i<8;i++)
{
for (j=0;j<128;j++)
{
/*fprintf(fp,"%c,",screen[i][j]);*/
fputc(screen[i][j],fp);
}
}
fclose(fp);
}
/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
int myx,myy;
int tmp;
switch (message) /* handle the messages */
{
case WM_LBUTTONDOWN:
tmp=LOWORD(lParam);/* x ordination */
myx=tmp-tmp%5;

tmp=HIWORD(lParam);/* y ordination */
myy=tmp-tmp%5;

/*result[myy/5][myx/5]=1;*/
StoreData(myx/5,myy/5,1);

InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON)
{
tmp=LOWORD(lParam);/* x ordination */
myx=tmp-tmp%5;

tmp=HIWORD(lParam);/* y ordination */
myy=tmp-tmp%5;

/*result[myy/5][myx/5]=1;*/
StoreData(myx/5,myy/5,1);

InvalidateRect(hwnd, NULL, FALSE);
}
break;
case WM_LBUTTONDBLCLK:
tmp=LOWORD(lParam);/* x ordination */
myx=tmp-tmp%5;

tmp=HIWORD(lParam);/* y ordination */
myy=tmp-tmp%5;

/*result[myy/5][myx/5]=0;*/
StoreData(myx/5,myy/5,0);
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_PAINT:
hdc=BeginPaint(hwnd, &ps);
Draw(hwnd,hdc);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
writeToFile();
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}

搜索更多相关主题的帖子: 鼠标 windows include int 
2007-01-30 21:01
herendagao
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-5-3
收藏
得分:0 
   我下下来了,可突然发现不知道他该在哪里运行,VC++6.0吗,还是tc2.0?

2007-02-28 11:47
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 

顶一下
但是我感觉改成这个能更好一些
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW-WS_SIZEBOX-WS_MAXIMIZEBOX, /* default window */
0, /* Windows decides the position */
0, /* where the window ends up on the screen */
648, /* The programs width */
349, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

2007-02-28 12:34
走刀口→超
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:5018
专家分:0
注 册:2006-3-14
收藏
得分:0 
以下是引用herendagao在2007-2-28 11:47:51的发言:
我下下来了,可突然发现不知道他该在哪里运行,VC++6.0吗,还是tc2.0?

VC6.0++


人在江湖【走】,怎能不挨【刀】;为了能活【口】,唯有把己【超】!come on...
2007-02-28 12:43
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 
c-free也行
2007-02-28 13:08
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 

我正需要着方便的电子书。楼主是总那里学到的着些知识?


羊肉串 葡萄干 哈密瓜!!
2007-02-28 17:21
C语言学习者
Rank: 4
等 级:贵宾
威 望:13
帖 子:1278
专家分:0
注 册:2006-9-26
收藏
得分:0 
不错,历害。

谁有强殖装甲第二部,可以Q我460054868
2007-02-28 17:33
lawin
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-1-29
收藏
得分:0 
专门针对VC++的书应该有介绍
2007-02-28 18:06
senyee
Rank: 1
等 级:新手上路
帖 子:422
专家分:0
注 册:2006-11-28
收藏
得分:0 
一个汉字都没看到~~

一头雾水...````
见笑了...

菜鸟~~请多指教~~
2007-03-01 17:36
快速回复:[原创]一个关于鼠标的小程序,与大家分享
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021495 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved