win32显示位图

本文详细介绍了如何使用位图信息在窗口中显示,并涉及窗口创建的基本概念,包括使用WNDCLASS、RegisterClass、CreateWindow等函数。文章还讨论了在快速改变窗口大小和鼠标移动时遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

            位图信息显示
 程序思路:
           是很普通的思路,网上已经说了很多遍了,就是创建dc、创建兼容dc、
    加载位图、显示位图。
 
           在这个函数里,工程是空的工程,所以,窗口的创建是需要代码创建的。
    使用的数据结构是:WNDCLASS和  函数:RegisterClass()、CreateWindow()。
 
 使用到的函数:
              1.LoadImage()——加载磁盘上的位图,如果在IDE里添加资源的方式
                  添加位图,那么使用函数是:LoadBitmap()
  2.CreateCompatibleBitmap()——创建兼容位图
  3.CreateCompatibleDC()——创建兼容dc
  4.SelectObject()——选择对象
  5.BitBlt()——显示位图,如果需要缩放就用StretchBlt()
     
 存在的问题:
             1.如果改变窗口大小的速度很快的时候,就死掉了,不知道为什么
  2.鼠标在窗口上晃动的时候屏幕会闪、会出现一条水平的白线
 
         I don't know how to reslove it  :(


#include <stdio.h>
#include <windows.h>

LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);


HINSTANCE g_hInstance;

int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow      // show state of window     
  ){


WNDCLASS fans;
fans.style=CS_HREDRAW | CS_VREDRAW ;
fans.lpfnWndProc=WindowProc;
fans.cbClsExtra=0;
fans.cbWndExtra=0;
fans.hInstance=hInstance;//这个就是WinMain函数的第一个参数。
    fans.hIcon=LoadIcon(NULL,IDI_ERROR);
fans.hCursor=LoadCursor(NULL,IDC_CROSS);
fans.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
fans.lpszMenuName=NULL;
fans.lpszClassName="Idontknow";
RegisterClass(&fans);
    g_hInstance=hInstance;

HWND h;
h=CreateWindow("Idontknow",
"woshinid",
WS_OVERLAPPEDWINDOW,
100,100,800,480,
NULL,NULL,
hInstance,
NULL);
    ShowWindow(h,SW_SHOWNORMAL);
    UpdateWindow(h);
    MSG msg;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;

}


LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
  ){
PAINTSTRUCT paintinformation;
HDC hdc;
switch(uMsg)
{
case WM_LBUTTONDOWN:
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_PAINT:
{
hdc=BeginPaint(hwnd,&paintinformation);
RECT rect;
GetClientRect(hwnd,&rect);
HBITMAP  bimp = (HBITMAP)LoadImage(NULL,
"C:\aa.bmp",IMAGE_BITMAP,1280,800,LR_LOADFROMFILE);
CreateCompatibleBitmap(hdc,1280,800);
HDC hh=CreateCompatibleDC(NULL);
SelectObject(hh,bimp);
BitBlt(hdc,0,0,400,200,hh,0,0,SRCCOPY);
EndPaint(hwnd,&paintinformation);
            ReleaseDC(hwnd,hh);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}


ReleaseDC(hwnd,hdc);

return 0L;
}//WindowProc()函数结束
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值