这样用LoadBitmap打开位图资源为什么不行?(50分)

  • 主题发起人 主题发起人 hoir
  • 开始时间 开始时间
H

hoir

Unregistered / Unconfirmed
GUEST, unregistred user!
发信人: jacer (jacer), 信区: Delphi<br>标 &nbsp;题: 用LoadBitmap打开位图资源的问题???<br>发信站: 武汉白云黄鹤站 (Tue May 23 19:40:22 2000), 站内信件<br><br>运行时总是出现'位图打开失败!',也就是说LoadBitmap函数总返回0<br>附:<br>.dpr源文件.:<br>program trapmouse;<br>{$R 'resrce.res' 'resrce.rc'}<br>//这里用{$R 'resrce.rc'}或{$R resrce.res}也不行<br>... ...<br>&nbsp; MainBMP,OldBitMap:HBitmap;<br>... ...<br>&nbsp; MainBMP:=LoadBitmap(Window,'MainBitMap'); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp;//6<br>&nbsp; if MainBMP=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; StayTime:=AlwaysStay;<br>&nbsp; &nbsp; MessageBox(0,'位图打开失败!',nil,mb_OK);<br>&nbsp; &nbsp; StayTime:=DefaultStayTime;<br>&nbsp; end;<br>&nbsp; OldBitMap:=SelectObject(PaintDC,MainBMP);<br>... ...<br><br>resrce.rc文件:<br>MainBitMap BITMAP "Trap80.bmp"<br>
 
1.首先把resrce..rc文件编译成resrce..res文件;<br>2.然后{$R resrce.res}<br>3.再然后 loadbitmap(hInstance,'MainBitMap');<br><br>BTW:你的语句里面的"window"是个什么东西?
 
Cake说得不错,一定要用该程序的hInstance
 
同意两位看法
 
后面的参数最好用数字,最保险。
 
cAkk;<br>Window是该程序的hInstance。<br>你说的我都做过了,问题是,不行。<br>我看别人的程序里用得好好的,我的就。。。<br><br>请问还有哪里会有问题?<br>
 
直接用的是 hInstance , 是一个全局变量,<br>不用声明,跟 screen 一样,<br><br>.rc 得用 brcc32 编译成 .res
 
要不吧你的完整程序贴出来吧
 
程序如下:(因为太长,有所删节)<br>program trapmouse;<br><br>{$R resrce.res}<br><br>uses<br>&nbsp; Messages,<br>&nbsp; SysUtils,<br>&nbsp; Graphics,<br>&nbsp; Windows;<br><br>//Windows API:CreateWinow,RegisterClass,DefWindowProc,..etc<br>// &nbsp;ShareMem;<br><br>{$R *.RES}<br>{$I trap!.inc}<br><br>const<br>&nbsp; AppName='Trap!';<br>&nbsp; Width=320;<br>&nbsp; Height=120;<br>&nbsp; id_BtnOK=100;<br>&nbsp; id_EditUser=200;<br>&nbsp; id_MoveTimer=300;<br>&nbsp; id_MainBitMap=350;<br>&nbsp; MoveInterval=3000; &nbsp; //窗口每三秒钟移动一次;<br>&nbsp; DefaultStayTime=5; &nbsp; //焦点在EDIT控件上时,窗口停留时间为3*5=15秒;<br>&nbsp; AlwaysStay=100000; &nbsp; //窗口总是不动;<br><br>var<br>&nbsp; OldR,R: TRECT;<br>&nbsp; HPos,VPos: Integer;<br>&nbsp; PassedFlag,CheatMode,CanMove,CanRandomMove: BOOL;<br>&nbsp; ResolutionRatioX,ResolutionRatioY:Integer;<br>&nbsp; MainMsg1,MainMsg2,MainMsg3,MainMsg4:String;<br>&nbsp; TimeCounter,StayTime:Integer;<br>&nbsp; HBtn_OK,HEdit_User,HEdit_Pwd:HWnd;<br>&nbsp; MyConfig:ConfigRecord;<br><br>procedure Trap_OnCreate(Window:HWnd;var Msg:TWMCreate);//AMessage,WParam,LParam:Longint);<br>var<br>&nbsp; myHDC:HDC;<br>&nbsp; Dummy:Integer;<br>begin<br>&nbsp; Configure(Window);<br>&nbsp; myHDC:=GetDC(Window);<br>&nbsp; if myHDC=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(0,'资源不足,无法启动程序!',nil,mb_OK);<br>&nbsp; &nbsp; ReleaseDC(Window,myHDC);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; OldR.left:=0;<br>&nbsp; OldR.right:=GetDeviceCaps(myHDC,HORZRES);//screen.width;<br>&nbsp; OldR.top:=0;<br>&nbsp; OldR.bottom:=GetDeviceCaps(myHDC,VERTRES);//screen.height;<br>&nbsp; ReleaseDC(Window,myHDC);<br>&nbsp; ResolutionRatioX:=OldR.right; &nbsp; &nbsp; &nbsp; //current resolution ratio.<br>&nbsp; ResolutionRatioY:=OldR.bottom;<br>&nbsp; R.left:=HPos;<br>&nbsp; R.right:=HPos+Width;<br>&nbsp; R.top:=VPos;<br>&nbsp; R.bottom:=VPos+Height;<br>&nbsp; ClipCursor(@R);<br>&nbsp; MainMsg1:='Note:This computer has';<br>&nbsp; MainMsg2:='been locked by TRAP!';<br>&nbsp; MainMsg3:='UserName:';<br>&nbsp; MainMsg4:='PassWord:';<br>&nbsp; HBtn_OK:=CreateWindowEx(0,'Button','&amp;OK',<br>&nbsp; ws_Child or ws_Visible or bs_DefPushButton,<br>&nbsp; &nbsp; 250,66,60,24,Window,id_BtnOK,hInstance,nil);<br>&nbsp; HEdit_User:=CreateWindowEx(0,'Edit','UserName',<br>&nbsp; ws_Child or ws_Visible or ws_TabStop or ws_Border or es_Right or es_AutoHScroll,<br>&nbsp; &nbsp; 120,45,100,20,Window,id_EditUser,hInstance,nil);<br>&nbsp; HEdit_Pwd:=CreateWindowEx(0,'Edit','PassWord',<br>&nbsp; ws_Child or ws_Visible or ws_TabStop or ws_Border or es_Right or es_AutoHScroll or es_PassWord,<br>&nbsp; &nbsp; 120,68,100,20,Window,id_EditUser,hInstance,nil);<br>//to disable ALT-TAB and CTRL-DEL-SHIFT<br>&nbsp; Dummy := 0;<br>//Disable ALT-TAB<br>&nbsp; SystemParametersInfo( SPI_SETFASTTASKSWITCH, 1, @Dummy, 0);<br>//Disable CTRL-ALT-DEL<br>&nbsp; SystemParametersInfo( SPI_SCREENSAVERRUNNING, 1, @Dummy, 0);<br>&nbsp; HideTaskBar;<br>&nbsp; Randomize;<br>&nbsp; SetTimer(Window,id_MoveTimer,MoveInterval,nil);<br>&nbsp; BringWindowToTop(Window);<br>end;<br><br>procedure Trap_OnDestroy(Window:HWnd;var Msg:TWMDestroy);<br>begin<br>... <br>end;<br><br>procedure Trap_OnPaint(Window:HWnd;var Msg:TWMPaint);<br>var<br>&nbsp; PaintX,PaintY:Integer;<br>&nbsp; TheFont,OldFont:HFont;<br>&nbsp; PaintDC:HDC;<br>&nbsp; PaintStruct:TPaintStruct;<br>&nbsp; MainBMP,OldBitMap:HBitmap;<br>begin<br>&nbsp; PaintDC:=BeginPaint(Window,PaintStruct);<br>&nbsp; if PaintDC=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(0,'资源不足,无法启动程序!',nil,mb_OK);<br>&nbsp; &nbsp; EndPaint(Window,PaintStruct);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; SetBKMode(PaintDC,TransParent);<br>&nbsp; TheFont:=GetFont(16,8,fw_Light,0,0,0,0,'Courier New');<br>&nbsp; OldFont:=SelectObject(PaintDC,TheFont); &nbsp; &nbsp; &nbsp;<br>&nbsp; SetTextColor(PaintDC,RGB(0,0,255));<br>&nbsp; TextOut(PaintDC,15,4,PChar(MainMsg1),Length(MainMsg1));<br>&nbsp; TextOut(PaintDC,15,20,PChar(MainMsg2),Length(MainMsg2));<br>&nbsp; SetTextColor(PaintDC,RGB(0,0,0));<br>&nbsp; TextOut(PaintDC,25,47,PChar(MainMsg3),Length(MainMsg3));<br>&nbsp; TextOut(PaintDC,25,70,PChar(MainMsg4),Length(MainMsg4));<br>&nbsp; SelectObject(PaintDC,OldFont);<br>&nbsp; DeleteObject(TheFont);<br>&nbsp; MainBMP:=LoadBitmap(Window,'MainBitMap'); &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; if MainBMP=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; StayTime:=AlwaysStay;<br>&nbsp; &nbsp; MessageBox(0,'位图打开失败!',nil,mb_OK);<br>&nbsp; &nbsp; StayTime:=DefaultStayTime;<br>&nbsp; end;<br>&nbsp; OldBitMap:=SelectObject(PaintDC,MainBMP);<br>&nbsp; PaintX:=220;<br>&nbsp; PaintY:=2;<br>&nbsp; BitBlt(PaintDC,PaintX,PaintY,PaintX+60,PaintY+60,MainBMP,0,0,SrcPaint);<br>&nbsp; SelectObject(PaintDC,OldBitMap);<br>&nbsp; DeleteObject(MainBMP);<br>&nbsp; EndPaint(Window,PaintStruct);<br>end;<br><br>... ...<br><br>function WindowProc(Window:HWnd;AMessage,WParam,LParam:Longint):Longint;stdcall;export;<br>var<br>&nbsp; AMsg:TMessage;<br>begin<br>&nbsp; WindowProc:=0;<br>&nbsp; AMsg.Msg:=AMessage;<br>&nbsp; AMsg.WParam:=WParam;<br>&nbsp; AMsg.LParam:=LParam;<br>&nbsp; AMsg.Result:=0;<br>&nbsp; case AMessage of<br>&nbsp; &nbsp; wm_Create:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Trap_OnCreate(Window,TWMCreate(AMsg));<br>&nbsp; &nbsp; &nbsp; WindowProc:=DefWindowProc(Window,AMessage,WParam,LParam);<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; wm_Paint: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trap_OnPaint(Window,TWMPaint(AMsg));<br><br>&nbsp; &nbsp; wm_KeyDown: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trap_OnKeyDown(Window,TWMKeyDown(AMsg));<br><br>&nbsp; &nbsp; wm_KeyUp: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trap_OnKeyUp(Window,TWMKeyUp(AMsg));<br><br>&nbsp; &nbsp; wm_Move: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Trap_OnMove(Window,TWMMove(AMsg));<br><br>&nbsp; &nbsp; wm_Timer: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trap_OnTimer(Window,TWMTimer(AMsg));<br><br>&nbsp; &nbsp; wm_NCLButtonDblClk: &nbsp;Trap_OnNCLButtonDblClk(Window,TWMNCLButtonDblClk(AMsg));<br><br>&nbsp; &nbsp; wm_Command: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trap_OnCommand(Window,TWMCommand(AMsg));<br><br>&nbsp; &nbsp; wm_Destroy: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trap_OnDestroy(Window,TWMDestroy(AMsg));<br><br>&nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WindowProc:=DefWindowProc(Window,AMessage,WParam,LParam);<br>&nbsp; end;<br>end;<br><br>function WinRegister:Boolean;<br>var<br>&nbsp; WindowClass:TWndClass;<br>begin<br>&nbsp; WindowClass.Style:=cs_NoClose;<br>&nbsp; WindowClass.lpfnWndProc:=@WindowProc;<br>&nbsp; WindowClass.cbClsExtra:=0;<br>&nbsp; WindowClass.cbWndExtra:=0;<br>&nbsp; WindowClass.hInstance:=HInstance;<br>&nbsp; WindowClass.hIcon:=LoadIcon(0,idi_Application);<br>&nbsp; WindowClass.hCursor:=LoadCursor(0,IDC_ARROW);<br>&nbsp; WindowClass.hbrBackground:=HBrush(color_Window);<br>&nbsp; WindowClass.lpszMenuName:=nil;<br>&nbsp; WindowClass.lpszClassName:=Appname;<br><br>&nbsp; Result:=RegisterClass(WindowClass)&lt;&gt;0;<br>end;<br><br>function WinCreate:hWnd;<br>var<br>&nbsp; hWindow:hWnd;<br>&nbsp; WinStyle : int64;<br>begin<br>&nbsp; WinStyle:=WS_DLGFRAME or DS_SETFOREGROUND;<br>&nbsp; hWindow:=CreateWindow(AppName,'Trap! ver 0.1',<br>&nbsp; WinStyle,HPos,VPos,Width,Height,0,0,hInstance,nil);<br>&nbsp; if HWindow&lt;&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; ShowWindow(hWindow,CmdShow);<br>&nbsp; &nbsp; UpdateWindow(hWindow);<br>&nbsp; end;<br>&nbsp; Result:=hWindow;<br>end;<br><br>var<br>&nbsp; AMessage:TMsg;<br>&nbsp; hWindow:hWnd;<br>begin<br>&nbsp; PassedFlag:=False;<br>&nbsp; CheatMode:=False;<br>&nbsp; CanMove:=True;<br>&nbsp; CanRandomMove:=True;<br>&nbsp; HPos:=240;<br>&nbsp; VPos:=240;<br>&nbsp; TimeCounter:=0;<br>&nbsp; StayTime:=5;<br>&nbsp; if not WinRegister then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(0,'窗口注册失败!',nil,mb_OK);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; hWindow:=WinCreate;<br>&nbsp; if hWindow=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(0,'窗口启动失败!',nil,mb_OK);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; while GetMessage(AMessage,0,0,0) do<br>&nbsp; begin<br>&nbsp; &nbsp; TranslateMessage(AMessage);<br>&nbsp; &nbsp; DispatchMessage(AMessage);<br>&nbsp; end;<br>&nbsp; Halt(AMessage.wParam);<br>end.<br><br>
 
你的 ‘MainBitMap’ 是在哪里定义的资源,????<br>一般不在 主程序名字.res 中,而要在另外一个资源中,<br>先得有资源才能调用,<br>MainBMP:=LoadBitmap(Window,'MainBitMap');<br>还有你的 window 不是 hInstance, 是 窗口句柄, 不一样的。<br><br>btw: &nbsp;LoadBitmap(hInstance, Makeintresource(aResID));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ~maybe Makeresourceint(aResID)<br><br>还有:你的这种写法很混乱<br>有点看不清楚, &nbsp;
 
LoadBitmap(param1, param2)<br>其中,param1是模块(比如说EXE或DLL)句柄(不是窗口句柄)。在Win32中,模块<br>句柄实际上就是模块在内存中的地址,LoadBitmap将从放在这个地址的那个模块<br>中得到param2所指示的位图资源,因为资源都是包含在模块中的,所以param1<br>不同,将从不同的模块中装入资源,如果param1是nil,将从windows本身装入<br>资源。而窗口句柄仅仅是指向一个窗口结构数据的指针,描述的是窗口,不是模块,<br>没有什么资源。param2是资源的名字,是字符串,如果只有资源的代号而没有<br>对应字符串,可用MAKEINTRESOURCE(ID)得到ID对应的字符串。<br>你如果想从当前运行的文件中读取资源,可以用全局变量HInstance或<br>MainInstance作为param1。或者用GetModuleHandle(nil)得到当前模块句柄。<br>如果想从别的模块中得到资源,可以用GetModuleHandle(ModuleName)得到那个<br>模块的句柄。
 
SuperMMX:<br>MainBitMap 在resrce.rc 文件中定义:<br>MainBitMap BITMAP "Trap80.bmp"<br>不好意思,程序贴出来就变了样啦。<br><br>partisian:<br>我用 GetModuleHandle(nil) 取代了 window, <br>MainBMP:=LoadBitmap(GetModuleHandlenil), 'MainBitMap');返回了非零值,<br>但是,图象还是显示不出来,why?<br>
 
为什么没人回答??<br><br>DELPHI的API帮助说,SelectObject的第二个参数必须由以下函数创建:<br>(The specified object must have been created by using one of the following functions): <br>CreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDIBSection<br>我在 1 处加上<br>&nbsp; MainBMP:=CreateCompatibleBitmap(PaintDC,60,60);<br>结果还是不行。<br>???
 
怎么搞的,这么基本的问题就搞不明白了呢,看看Delphi子代的演示程序ResExplorer能不能打开<br>你的资源文件, hInstance是Delphi中的全局变量,另外就是图像的名字是否正确了
 
我用loadbitmap没有任何问题,你最好新建一个工程,<br>练习一下loadBitmap的用法,看是否成功。
 
hoir:<br>这是我提的问题呀!<br>我已经解决了。<br>var<br>&nbsp; PaintDC:HDC;<br>&nbsp; PaintStruct:TPaintStruct;<br>&nbsp; MainBMP,DrawBMP,OldBitMap:HBitmap;<br>begin<br>... ...<br>&nbsp; MainBMP:=LoadBitmap(GetModuleHandle(nil),'MainBitMap'); <br>&nbsp; if MainBMP=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; StayTime:=AlwaysStay;<br>&nbsp; &nbsp; MessageBox(0,'位图打开失败!',nil,mb_OK);<br>&nbsp; &nbsp; StayTime:=DefaultStayTime;<br>&nbsp; end;<br>&nbsp; DrawBMP:=CreateCompatibleDC(PaintDC);<br>&nbsp; OldBitMap:=SelectObject(DrawBMP,MainBMP);<br>&nbsp; PaintX:=250;<br>&nbsp; PaintY:=2;<br>&nbsp; BitBlt(PaintDC,PaintX,PaintY,60,60,DrawBMP,0,0,SRCAND);<br>&nbsp; SelectObject(DrawBMP,OldBitMap);<br>&nbsp; DeleteObject(DrawBMP);<br>&nbsp; EndPaint(Window,PaintStruct);<br>... ...<br>end;
 
多人接受答案了。
 
后退
顶部