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

加载图片并显示。窗口一片空白。解决方法

2012-03-03 
加载图片并显示。。。窗口一片空白。。。图片已加载显示,超过256色,没法编辑。不过程序应该照样可以显示,这应该不

加载图片并显示。。。窗口一片空白。。。



图片已加载显示,超过256色,没法编辑。不过程序应该照样可以显示,这应该不是问题。。

是不是图片的宽度高度什么的没设置对??

急。。

大家帮忙哦。。。先谢谢了。。



#include <windows.h>
#include <wingdi.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  PSTR szCmdLine, int iCmdShow)
{
  static TCHAR szAppName[] = TEXT ("HelloWin") ;
  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, // window class name
  TEXT ("The Hello Program"), // window caption
  WS_OVERLAPPEDWINDOW, // window style
  CW_USEDEFAULT, // initial x position
  CW_USEDEFAULT, // initial y position
  CW_USEDEFAULT, // initial x size
  CW_USEDEFAULT, // initial y size
  NULL, // parent window handle
  NULL, // window menu handle
  hInstance, // program instance handle
  NULL) ; // creation parameters
   
  ShowWindow (hwnd, iCmdShow) ;
  UpdateWindow (hwnd) ;

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

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  HDC hdcMem, hdc ;
  PAINTSTRUCT ps ;
HBITMAP bitmap,IDB_BITMAP1;//定义bitmap对象
BITMAP bmp;
HINSTANCE hInst,hInstance;



  switch (message)
  {
  case WM_CREATE:

 
  InvalidateRect(hwnd,NULL,TRUE);
   
  return 0 ;
   
  case WM_PAINT:

 

hdc=BeginPaint(hwnd,&ps);

  hdcMem=CreateCompatibleDC(NULL);

bitmap=LoadBitmap( hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
   
GetObject(bitmap,sizeof(bmp),&bmp);//获得bitmap信息到bmp结构中;
   
SelectObject(hdcMem,bitmap);//将BMP选入到内存DC中

DeleteObject(bitmap);//删除对象

  //StretchBlt(hdc,0,0,500/4,500/4,hdcMem,0,0,500/4,500/4,WHITENESS );

BitBlt(hdc,0,0,500/4,500/4,hdcMem,0,0,SRCINVERT);//将内存DC中的内容拷贝到当前窗口DC中,并显示

 
  DeleteDC(hdcMem);//删除DC;




EndPaint(hwnd,&ps);
   
  return 0 ;
   
  case WM_DESTROY:
  PostQuitMessage (0) ;
  return 0 ;
  }
  return DefWindowProc (hwnd, message, wParam, lParam) ;
}

[解决办法]
修改后的代码,本地跑ok的。
需要添加一个叫IDB_BITMAP1的资源。

C/C++ code
#include <windows.h>#include <wingdi.h>#include "resource.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){    static TCHAR szAppName[] = TEXT ("HelloWin") ;    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, // window class name        TEXT ("The Hello Program"), // window caption        WS_OVERLAPPEDWINDOW, // window style        CW_USEDEFAULT, // initial x position        CW_USEDEFAULT, // initial y position        CW_USEDEFAULT, // initial x size        CW_USEDEFAULT, // initial y size        NULL, // parent window handle        NULL, // window menu handle        hInstance, // program instance handle        NULL) ; // creation parameters    ShowWindow (hwnd, iCmdShow) ;    UpdateWindow (hwnd) ;    while (GetMessage (&msg, NULL, 0, 0))    {        TranslateMessage (&msg) ;        DispatchMessage (&msg) ;    }    return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){    HDC hdcMem, hdc ;    PAINTSTRUCT ps ;    HBITMAP bitmap;//定义bitmap对象    BITMAP bmp;    HINSTANCE hInst,hInstance;    switch (message)    {    case WM_CREATE:        InvalidateRect(hwnd,NULL,TRUE);        return 0 ;    case WM_PAINT:        hdc=BeginPaint(hwnd,&ps);        hdcMem=CreateCompatibleDC(hdc);        bitmap=LoadBitmap( (HINSTANCE)GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));        GetObject(bitmap,sizeof(bmp),&bmp);//获得bitmap信息到bmp结构中;        SelectObject(hdcMem,bitmap);//将BMP选入到内存DC中        //StretchBlt(hdc,0,0,500/4,500/4,hdcMem,0,0,500/4,500/4,WHITENESS );        BitBlt(hdc,0,0,500/4,500/4,hdcMem,0,0,SRCCOPY);//将内存DC中的内容拷贝到当前窗口DC中,并显示        DeleteObject(bitmap);//删除对象        DeleteDC(hdcMem);//删除DC;        EndPaint(hwnd,&ps);        return 0 ;    case WM_DESTROY:        PostQuitMessage (0) ;        return 0 ;    }    return DefWindowProc (hwnd, message, wParam, lParam) ;} 

热点排行