| 发表于:2007-04-06 23:14:18 楼主 |
在api中怎么解决鼠标拉动图片时的闪烁白条(纯api代码) 网上有很多关于这个的问题 但是都是mfc的代码 都是说的双重缓存技术,但是我不知道在纯api怎么实现 我现在正在学api 还没有接触mfc 所以看不懂mfc代码 //以下是我的代码 麻烦大家帮我 这代码应该怎么改才不会闪烁 --------------------------------- #include <windows.h> #include <stdio.h> #include "resource.h " hdc hdc,hdcbit;//定义图形句柄 hbitmap hbm; bitmap bm; lresult callback wndproc(hwnd hwnd, uint message,wparam wparam, lparam lparam);//申明窗口处理函数 //-------初始化窗口类--------------------------------- int apientry winmain( hinstance hinstance, //应用程序当前实例句柄 hinstance hprevinstance, //应用程序其他实例句柄 lptstr lpcmdline, //指向程序命令行参数的指针 int ncmdshow //应用程序开始执行时窗口显示方式的整数值标识 ) { hwnd hwnd; //定义窗口句柄 msg msg; //定义消息的类 wndclass wndclass; //定义窗口的类 char lpszclassname[]= "窗口 "; //定义窗口类名 char lpsztitle[]= "我的窗口 "; //定义窗口标题 wndclass.style=0; //设置窗口样式 wndclass.lpfnwndproc=wndproc;//定义窗口处理函数 wndclass.cbclsextra=0;//窗口类无扩展 wndclass.cbwndextra=0;//窗口实例无扩展 wndclass.hinstance=hinstance;//当前实例句柄 wndclass.hicon=loadicon(null,idi_application);//窗口最小化图标为缺省 wndclass.hcursor=loadcursor(hinstance,makeintresource(idb_bitmap));//窗口采用箭头光标 wndclass.hbrbackground=(hbrush)(color_window+1);//窗口背景色为白色 wndclass.lpszmenuname= "menu ";//窗口中无菜单 wndclass.lpszclassname=lpszclassname;//定义窗口类名 //-------初始化窗口类--------------------------------- //-------注册窗口--------------------------------- if(!registerclass(&wndclass)) //注册窗口 { messagebeep(0); //如果注册失败,则发出警告 return false; //返回为假 } //-------注册窗口--------------------------------- //-------创建窗口--------------------------------- hwnd=createwindow( lpszclassname, //窗口类名 lpsztitle, //窗口标题名 ws_overlappedwindow ¦ws_vscroll ¦ws_hscroll, //创建窗口的样式,ws_vscroll垂直滚动条,ws_hscroll水平滚动条 100,100, //窗口左上角坐标 800,600, //窗口宽度和度高 null, //该窗口的父窗口句柄 null, //窗口主菜单句柄 hinstance, //创建窗口的应用程序当前句柄 null //指向一个传递给窗口的参数值的指针 ); //-------创建窗口--------------------------------- hbm=loadbitmap(hinstance,makeintresource(idb_bitmap)); getobject(hbm,sizeof(bitmap),(lpvoid)&bm); showwindow(hwnd,ncmdshow); //显示窗口函数 updatewindow(hwnd); //刷新窗口函数 //-------消息循环--------------------------------- while (getmessage(&msg,null,0,0)) { translatemessage(&msg); dispatchmessage(&msg); } return (int) msg.wparam; } //-------消息循环--------------------------------- //-------窗口处理函相数------------------------------ lresult callback wndproc(hwnd hwnd, uint message,wparam wparam, lparam lparam) { static hinstance hinst ; paintstruct ps;//定义指向包含绘图信息的结构变量 static point npos,opos,apos; static bool ndw=false; char str[20]; switch(message) { case wm_create: hdc=getdc(hwnd); hdcbit=createcompatibledc(hdc); hbm=createcompatiblebitmap(hdc,bm.bmwidth,bm.bmheight);//双重缓存 hinst = ((lpcreatestruct)lparam)-> hinstance;// 获取程序实例句柄 setclasslong(hwnd, gcl_hicon, (long)loadicon(hinst, makeintresource(idb_bitmap))); setcursor(loadcursor(hinst, makeintresource(idb_bitmap))); releasedc(hwnd,hdc); break; case wm_lbuttondown: if(loword(lparam)> =apos.x&&loword(lparam) <=apos.x+bm.bmwidth&&hiword(lparam)> =apos.y&&hiword(lparam) <=apos.y+bm.bmheight) { setcursor(loadcursor(hinst,makeintresource(idc_cursor))); npos.x=loword(lparam); npos.y=hiword(lparam); ndw=true; } break; case wm_rbuttondown: invalidaterect(hwnd,null,true); break; case wm_lbuttonup: if(ndw==true) { apos.x=apos.x+opos.x; apos.y=apos.y+opos.y; ndw=false; setcursor(loadcursor(null,idc_arrow)); invalidaterect(hwnd,null,true); } break; case wm_mousemove: if(ndw==true) { opos.x=loword(lparam)-npos.x; opos.y=hiword(lparam)-npos.y; invalidaterect(hwnd,null,true); releasedc(hwnd,hdc); } else setcursor(loadcursor(null,idc_arrow)); if(loword(lparam)> =apos.x&&loword(lparam) <=apos.x+bm.bmwidth&&hiword(lparam)> =apos.y&&hiword(lparam) <=apos.y+bm.bmheight) setcursor(loadcursor(hinst,makeintresource(idc_cursor))); break; case wm_paint: hdc = beginpaint(hwnd, &ps); //开始绘图 selectobject(hdcbit,hbm); if(ndw==false) stretchblt(hdc,apos.x,apos.y,bm.bmwidth,bm.bmheight,hdcbit,0,0,bm.bmwidth,bm.bmheight,srccopy); else stretchblt(hdc,apos.x+opos.x,apos.y+opos.y,bm.bmwidth,bm.bmheight,hdcbit,0,0,bm.bmwidth,bm.bmheight,srccopy); sprintf(str, "%d,%d ",apos.x,apos.y); textout(hdc,0,0,str,strlen(str)); endpaint(hwnd,&ps);//结束绘图 return 0; case wm_destroy://退出消息 deleteobject(hdcbit); postquitmessage(0);//退出处理函数 return 0; default: //缺省消息处理函数 return defwindowproc(hwnd,message,wparam,lparam); } return(0); } //-------窗口处理函相数------------------------------ |
|
|
|
|