关于StretchBlt显示图片的有关问题
关于StretchBlt显示图片的问题
求助。。不知道为什么,我把下面这个函数,每0.01秒输出一次,随机显示图片,但是会出现,偶尔图片刷不出来的情况。。。这是为什么?
------解决思路----------------------
请判断每个函数的返回值!
------解决思路----------------------
不显示的时候,相关函数肯定返回错误了。你信不信?反正我信了。
------解决思路----------------------
LoadImage
Return Values
If the function succeeds, the return value is the handle of the newly loaded image.
If the function fails, the return value is NULL. To get extended error information, callGetLastError.
GetObject
Return Values
If the function succeeds, and lpvObject is a valid pointer, the return value is the number of bytes stored into the buffer.
If the function succeeds, and lpvObject is NULL, the return value is the number of bytes required to hold the information the function would store into the buffer.
If the function fails, the return value is zero.
To get extended error information, callGetLastError.
StretchBlt
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
To get extended error information, callGetLastError.
------解决思路----------------------
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
------解决思路----------------------
还可以试试使用消息断点:
Setting a Breakpoint on a Message Received by a Windows Function
You can set message breakpoints to stop the debugger when a particular WndProc function message is received.
Note Message breakpoints work only on x86- or Pentium-based systems.
To set a breakpoint on a message
From the Edit menu, click Breakpoints.
The Breakpoints dialog box appears.
Click the Messages tab.
In the Break At WndProc text box, type the name of the Windows function.
If you are setting a breakpoint during a debug session, the list contains the exported functions in your project.
In the Set One Breakpoint For Each Message To Watch list box, select the message.
To set another breakpoint, press ENTER, and then repeat steps 3 and 4.
The Breakpoints list displays the currently active breakpoints.
Click OK to set the breakpoints.
Note If you interrupt execution while Windows or other system code is running, the results can be unpredictable.
求助。。不知道为什么,我把下面这个函数,每0.01秒输出一次,随机显示图片,但是会出现,偶尔图片刷不出来的情况。。。这是为什么?
int CgLotteryDlg::showBMP(int BMPindex) {
HWND hwnd;
HDC hdc;
HBITMAP hbm;
HDC hdcBits;
BITMAP bm;
hwnd = GetDlgItem(IDD_GLOTTERY_DIALOG)->GetSafeHwnd();
hdc = ::GetDC(hwnd);
vector<CString> FindedFiles;
TCHAR tszBMPPath[MAX_PATH];
StrCpy(tszBMPPath, m_tszPath);
StrCat(tszBMPPath, TEXT("\\bmp"));
FindFileByPath(tszBMPPath,FindedFiles);
StrCat(tszBKPath, FindedFiles[1]);
hbm=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),FindedFiles[BMPindex],IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
hdcBits = CreateCompatibleDC(hdc);
GetObject (hbm, sizeof(BITMAP), &bm);
::SelectObject(hdcBits,hbm);
int cx=GetSystemMetrics(SM_CXFULLSCREEN);
int cy=GetSystemMetrics(SM_CYFULLSCREEN);
int width;
int height;
if(bm.bmWidth>=bm.bmHeight)
{
width = 480;
height = (480 * bm.bmHeight)/bm.bmWidth;
}
else
{
height = 500;
width = (480 * bm.bmWidth)/bm.bmHeight;
}
int x = (cx - width)/2;
int y = (cy - height)/2;
SetStretchBltMode(hdc,STRETCH_HALFTONE);
StretchBlt(hdc,x,y,width, height,hdcBits,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
int a = 0;
if(!DeleteObject(hdcBits))
a = 1;
if(!DeleteObject(hbm))
a = 1;
if(!::ReleaseDC(hwnd,hdc))
a = 1;
//getch();
return 0;
}
------解决思路----------------------
请判断每个函数的返回值!
------解决思路----------------------
不显示的时候,相关函数肯定返回错误了。你信不信?反正我信了。
------解决思路----------------------
LoadImage
Return Values
If the function succeeds, the return value is the handle of the newly loaded image.
If the function fails, the return value is NULL. To get extended error information, callGetLastError.
GetObject
Return Values
If the function succeeds, and lpvObject is a valid pointer, the return value is the number of bytes stored into the buffer.
If the function succeeds, and lpvObject is NULL, the return value is the number of bytes required to hold the information the function would store into the buffer.
If the function fails, the return value is zero.
To get extended error information, callGetLastError.
StretchBlt
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
To get extended error information, callGetLastError.
------解决思路----------------------
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt
------解决思路----------------------
0==pszFmt[0]) return;
_vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
------解决思路----------------------
还可以试试使用消息断点:
Setting a Breakpoint on a Message Received by a Windows Function
You can set message breakpoints to stop the debugger when a particular WndProc function message is received.
Note Message breakpoints work only on x86- or Pentium-based systems.
To set a breakpoint on a message
From the Edit menu, click Breakpoints.
The Breakpoints dialog box appears.
Click the Messages tab.
In the Break At WndProc text box, type the name of the Windows function.
If you are setting a breakpoint during a debug session, the list contains the exported functions in your project.
In the Set One Breakpoint For Each Message To Watch list box, select the message.
To set another breakpoint, press ENTER, and then repeat steps 3 and 4.
The Breakpoints list displays the currently active breakpoints.
Click OK to set the breakpoints.
Note If you interrupt execution while Windows or other system code is running, the results can be unpredictable.