首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求windows界面没显示有关问题,附代码

2013-04-20 
求windows界面没显示问题,附代码?#includewindows.hstatic LRESULT CALLBACK WndProc (HWND, UINT, WPAR

求windows界面没显示问题,附代码?

#include<windows.h>
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
static TCHAR szAppName[] = TEXT ("读取.txt文件") ;              
void DataManage(char *,DWORD );
 byte Buff[64];
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{     
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

  hwnd=CreateWindow (szAppName,                  
                          TEXT ("The Hello Program"),
                          WS_OVERLAPPEDWINDOW,        
                          CW_USEDEFAULT,            
                          CW_USEDEFAULT,             
                          CW_USEDEFAULT,              


                          CW_USEDEFAULT,             
                          NULL,                     
                          NULL,                      
                          hInstance,                  
                          NULL) ;                    

     ShowWindow (hwnd, SW_SHOWNORMAL) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}


static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{  
     HDC         hdc ;            
     PAINTSTRUCT ps ;                    
 static int  cxClient, cyClient;     
 DWORD dwread;
 static BOOL      read;
 static void * lpBuff;    
 byte   Buff;
     switch (message)
     {
     case WM_CREATE:
         {
HANDLE h;
            hdc=GetDC(hwnd);
h=CreateFile(TEXT("E:\data.txt"), //里面数据格式为  12-AE-5-6A-
GENERIC_READ,       
0,                
NULL,          
OPEN_EXISTING,      
FILE_ATTRIBUTE_READONLY,                      
0);
if(h!=INVALID_HANDLE_VALUE)
{
DWORD length=GetFileSize(h,NULL);
lpBuff = LocalAlloc(GPTR, sizeof(char) * (length + 1));
if(lpBuff!=NULL)
{
read=ReadFile(h,lpBuff,length,&dwread, 0);  


DataManage((char*)lpBuff,length);  //datamanege调用数据处理数组
}
}  
ReleaseDC(hwnd,hdc);
return 0;
 }

case WM_SIZE:                                
{
cxClient=LOWORD(lParam);               
cyClient=HIWORD(lParam);      
   
}   
          return 0 ;

     case WM_PAINT:
         {    
if(read)
{
RECT        rect ;   
hdc = BeginPaint (hwnd, &ps) ;                 
GetClientRect (hwnd, &rect) ;
InflateRect(&rect, -5, -5);
SetTextColor(hdc, RGB(34, 120, 34));    
DrawText(hdc,(LPCTSTR)lpBuff,-1,&rect,DT_WORDBREAK | DT_EDITCONTROL ) ; 
DrawText(hdc,(LPCWSTR)Buff,-1,&rect,DT_WORDBREAK | DT_EDITCONTROL ) ;
EndPaint (hwnd, &ps) ;
}
            return 0 ;
         }

     case WM_DESTROY:
if (lpBuff != NULL)
{
           LocalFree(lpBuff);
         }
        PostQuitMessage (0) ;
        return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}


void DataManage(char *p,DWORD len)
{

int j=0;
for(DWORD i=0;i<len;i++,++p)
{
if((((*p)>=0x30)&((*p)<=0x46)))
{
if((*(p+1)>=0x30)&(*(p+1)<=0x46))
{
Buff[j]=((int)*p)*16+(int)(*(p+1));i++;j++;++p;continue;
    }
else
{
Buff[j]=(int)*p;j++;continue;
}
}
else
p++;continue;
}
Buff[len]='\0';
}

代码单步调试进入死循环(while这块),求怎么弄哦?
[解决办法]
引用:
引用:C/C++ code?代码单步调试进入死循环(while这块),求怎么弄哦?
这是一个消息循环,
问题不在这。
是你在不应该return 0的地方,却return 0了。
break就可以了。
这个是正解
static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{  
     HDC         hdc ;            
     PAINTSTRUCT ps ;                    
     static int  cxClient, cyClient;     
     DWORD dwread;


     static BOOL      read;
     static void * lpBuff;    
     byte   Buff;
     switch (message)
     {
     case WM_CREATE:
         {
            HANDLE h;
            hdc=GetDC(hwnd);
            h=CreateFile(TEXT("E:\data.txt"), //里面数据格式为  12-AE-5-6A-
                        GENERIC_READ,       
                        0,                
                        NULL,          
                        OPEN_EXISTING,      
                        FILE_ATTRIBUTE_READONLY,                      
                        0);
            if(h!=INVALID_HANDLE_VALUE)
            {
                DWORD length=GetFileSize(h,NULL);
                lpBuff = LocalAlloc(GPTR, sizeof(char) * (length + 1));
                if(lpBuff!=NULL)
                {
                    read=ReadFile(h,lpBuff,length,&dwread, 0);                                  
                    DataManage((char*)lpBuff,length);  //datamanege调用数据处理数组
                }
            }  
            ReleaseDC(hwnd,hdc);
            return 0;
         }
 
    case WM_SIZE:                                


        {
            cxClient=LOWORD(lParam);               
            cyClient=HIWORD(lParam);      
    
        }   
          return 0 ;
 
     case WM_PAINT:
         {    
            if(read)
            {
                RECT        rect ;   
                hdc = BeginPaint (hwnd, &ps) ;                 
                GetClientRect (hwnd, &rect) ;
                InflateRect(&rect, -5, -5);
                SetTextColor(hdc, RGB(34, 120, 34));    
                DrawText(hdc,(LPCTSTR)lpBuff,-1,&rect,DT_WORDBREAK 
[解决办法]
 DT_EDITCONTROL ) ; 
                DrawText(hdc,(LPCWSTR)Buff,-1,&rect,DT_WORDBREAK 
[解决办法]
 DT_EDITCONTROL ) ;
                EndPaint (hwnd, &ps) ;
            }
            return 0 ;
         }
 
     case WM_DESTROY:
        if (lpBuff != NULL)
        {
           LocalFree(lpBuff);
         }
        PostQuitMessage (0) ;
        return 0 ;//此类的都应该是break;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;//DefWindowProc这个函数是默认的窗口处理函数,我们可以把不关心的消息都丢给它来处理。
}


[解决办法]
引用:
额,看错了,同意一楼,因为少个‘\’,导致CreateFileA返回值为INVALID_HANDLE_VALUE
而read初始值为0,所以没显示。
还有这句删掉吧,byte   Buff;
呵呵、显示出问题只能是你把默认的绘图函数没有重载明白。跟你引用外部文件没有关系。
            if(h!=INVALID_HANDLE_VALUE)
            {


                DWORD length=GetFileSize(h,NULL);
                lpBuff = LocalAlloc(GPTR, sizeof(char) * (length + 1));
                if(lpBuff!=NULL)
                {
                    read=ReadFile(h,lpBuff,length,&dwread, 0);                                  
                    DataManage((char*)lpBuff,length);  //datamanege调用数据处理数组
                }
            }

  
这段代码里完全没有跟页面相关的东西、虽然文件名是错误的。但是与页面不显示没有任何关系
[解决办法]
引用:
引用:
额,看错了,同意一楼,因为少个‘\’,导致CreateFileA返回值为INVALID_HANDLE_VALUE
而read初始值为0,所以没显示。
还有这句删掉吧,byte   Buff;呵呵、显示出问题只能是你把默认的绘图函数没有重载明白。跟你引用外部文件没有关系。


C/C++ code
?



12345678910

……


你在逗我吗?
case WM_PAINT: 这个消息里用的read变量怎么解释呢,给别人回答问题,希望先走一下程序,除非你是大神
[解决办法]
引用:
还有就是他用的windows api  哪里来的重载
==实际上你把switch()
{
}全部注释掉、窗体也能创建。内部的响应都在DefWindowProc (hwnd, message, wParam, lParam) ;中完成了,只不过样子是个白色的窗体。而整个窗体创建过程完全实现了。完全是重载,不过表现形式不一样,这里不需要写什么函数和消息映射。
至于上面那个。我的确没有很认真的看。我只是看到标题上说界面没有显示--(感觉楼主的意思是界面没有预期效果,不是没有显示)我把程序跑了一下。下面的应该是能符合楼主要求的(臆测,因为楼主确实没说是要什么样的)


#include<windows.h>
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
static TCHAR szAppName[] = TEXT ("读取.txt文件") ;              
void DataManage(char *,DWORD );
byte Buffs[64];
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{     
HWND         hwnd ;
MSG          msg ;
WNDCLASS     wndclass ;

wndclass.style         = CS_HREDRAW 
[解决办法]
 CS_VREDRAW;
wndclass.lpfnWndProc   = WndProc ;
wndclass.cbClsExtra    = 0 ;
wndclass.cbWndExtra    = 0 ;
wndclass.hInstance     = hInstance ;
wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;


wndclass.lpszMenuName  = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd=CreateWindow (szAppName,                  
TEXT ("The Hello Program"),
WS_OVERLAPPEDWINDOW,        
CW_USEDEFAULT,            
CW_USEDEFAULT,             
CW_USEDEFAULT,              
CW_USEDEFAULT,             
NULL,                     
NULL,                      
hInstance,                  
NULL) ;                    

ShowWindow (hwnd, SW_SHOWNORMAL) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}


static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{  
HDC         hdc ;            
PAINTSTRUCT ps ;                    
static int  cxClient, cyClient;     
DWORD dwread;
static BOOL      read;
static void * lpBuff;    
switch (message)
{
case WM_CREATE:
{
HANDLE h;
hdc=GetDC(hwnd);
h=CreateFile(TEXT("E:\\data.txt"), //里面数据格式为  12-AE-5-6A-
GENERIC_READ,       
0,                
NULL,          
OPEN_EXISTING,      
FILE_ATTRIBUTE_READONLY,                      
0);
if(h!=INVALID_HANDLE_VALUE)
{
DWORD length=GetFileSize(h,NULL);
lpBuff = LocalAlloc(GPTR, sizeof(char) * (length + 1));
if(lpBuff!=NULL)
{
read=ReadFile(h,lpBuff,length,&dwread, 0);                                  
DataManage((char*)lpBuff,length);  //datamanege调用数据处理数组
}
}  
ReleaseDC(hwnd,hdc);
return 0;
}

case WM_SIZE:                                


{
cxClient=LOWORD(lParam);               
cyClient=HIWORD(lParam);      

}   
return 0 ;

case WM_PAINT:
{    
if(read)
{
RECT        rect ;   
hdc = BeginPaint (hwnd, &ps) ;                 
GetClientRect (hwnd, &rect) ;
InflateRect(&rect, -5, -5);
SetTextColor(hdc, RGB(34, 120, 34));    
DrawTextA( hdc,( char * )lpBuff,-1,&rect,DT_WORDBREAK 
[解决办法]
 DT_EDITCONTROL );
rect.left += 400;
DrawTextA( hdc , ( char * ) Buffs ,-1,&rect,DT_WORDBREAK 
[解决办法]
 DT_EDITCONTROL ) ;
EndPaint (hwnd, &ps) ;
}
return 0 ;
}

case WM_DESTROY:
if (lpBuff != NULL)
{
LocalFree(lpBuff);
}
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}


void DataManage(char *p,DWORD len)
{

int j=0;
for(DWORD i=0;i<len;i++,++p)
{
if((((*p)>=0x30)&((*p)<=0x46)))
{
if((*(p+1)>=0x30)&(*(p+1)<=0x46))
{
Buffs[j]=((int)*p)*16+(int)(*(p+1));i++;j++;++p;continue;
}
else
{
Buffs[j]=(int)*p;j++;continue;
}
}
else
p++;continue;
}
Buffs[len]='\0';
}

最终的效果是 绿色12-AE-5-6A-                EBA(这是一种内部密码方式?臆测)
[解决办法]
附一段Win32标准应用程序

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{

}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(hwnd, &ps);

RECT rc;
GetClientRect(hwnd, &rc);

HBRUSH br = CreateSolidBrush(RGB(200,240,200));
FillRect(dc, &rc, br);

LOGFONT lf = { 0 };
strcpy_s(lf.lfFaceName, "微软雅黑");
lf.lfHeight = 30;
HFONT font = CreateFontIndirect(&lf);
SelectObject(dc, font);

SetTextColor(dc, RGB(0,0,255));
SetBkMode(dc, TRANSPARENT);
DrawText(dc, "Hello World", -1, &rc, DT_SINGLELINE 
[解决办法]
 DT_VCENTER 
[解决办法]
 DT_CENTER);

DeleteObject(br);
DeleteObject(font);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);


}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR cmd, int nCmdShow)
{
try{
WNDCLASS wc = { sizeof(WNDCLASS) };
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "[Win32标准应用程序]";
wc.style = CS_VREDRAW 
[解决办法]
 CS_HREDRAW;

ATOM atom = RegisterClass(&wc);
if(atom == 0)
throw 1;

HWND hwnd = CreateWindow(wc.lpszClassName, "Windows32 Application", 
WS_VISIBLE
[解决办法]
WS_POPUP
[解决办法]
WS_CAPTION
[解决办法]
WS_SYSMENU, 100, 50, 700, 500, NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
throw 2;

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

MSG msg;
while(true)
{
BOOL bRet = GetMessage(&msg, NULL, 0, 0);
if(bRet == -1)
continue;// Error Message
else if(bRet)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
break;// Exit
}
}
}catch(...){}

return 0;
}


[解决办法]
引用:
引用:引用:还有就是他用的windows api  哪里来的重载==实际上你把switch()
{
}全部注释掉、窗体也能创建。内部的响应都在DefWindowProc (hwnd, message, wParam, lParam) ;中完成了,只不过样子是个白色的窗体。而整个窗体创建过程完全实现了。……


下面这个是符合要求的--不过基本没有扩展性。。我的午休啊--
#include<windows.h>
#include <stdio.h>
#include <stdlib.h>
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
static TCHAR szAppName[] = TEXT ("读取.txt文件") ;              
void DataManage( char * data ,DWORD length );
char InData[64];
char OutData[64];
int iResult[3];//这个数组根据你的长度来吧,或者用vector<int>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{     
HWND         hwnd ;
MSG          msg ;
WNDCLASS     wndclass ;

wndclass.style         = CS_HREDRAW 
[解决办法]
 CS_VREDRAW;
wndclass.lpfnWndProc   = WndProc ;
wndclass.cbClsExtra    = 0 ;
wndclass.cbWndExtra    = 0 ;
wndclass.hInstance     = hInstance ;
wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;


wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName  = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd=CreateWindow (szAppName,                  
TEXT ("The Hello Program"),
WS_OVERLAPPEDWINDOW,        
CW_USEDEFAULT,            
CW_USEDEFAULT,             
CW_USEDEFAULT,              
CW_USEDEFAULT,             
NULL,                     
NULL,                      
hInstance,                  
NULL) ;                    

ShowWindow (hwnd, SW_SHOWNORMAL) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}


static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{  
HDC         hdc ;            
PAINTSTRUCT ps ;                    
static int  cxClient, cyClient;     
DWORD dwread;
static BOOL      read;
static void * lpBuff;    
switch (message)
{
case WM_CREATE:
{
FILE * pFile = fopen( "E:\\data.txt" , "rb" );
if ( NULL != pFile )
{
fread( InData , sizeof( char ) , 64 , pFile );
fseek( pFile , 0 , SEEK_SET );
fscanf( pFile , "%x-%x-%x" , iResult , iResult + 1 , iResult + 2 );
fclose( pFile );
pFile = NULL;
sprintf( OutData , "%d-%d-%d" , iResult[0] , iResult[1] , iResult[2] );
}
return 0;
}

case WM_SIZE:                                
{
cxClient=LOWORD(lParam);               
cyClient=HIWORD(lParam);      

}   
return 0 ;


case WM_PAINT:
{    

RECT        rect ;   
hdc = BeginPaint (hwnd, &ps) ;                 
GetClientRect (hwnd, &rect) ;
InflateRect(&rect, -5, -5);
SetTextColor(hdc, RGB(34, 120, 34));    
DrawTextA( hdc, InData ,-1,&rect,DT_WORDBREAK 
[解决办法]
 DT_EDITCONTROL );
rect.left += 400;
DrawTextA( hdc , OutData ,-1,&rect,DT_WORDBREAK 
[解决办法]
 DT_EDITCONTROL ) ;
EndPaint (hwnd, &ps) ;

return 0 ;
}

case WM_DESTROY:
if (lpBuff != NULL)
{
LocalFree(lpBuff);
}
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}


热点排行