鱼C论坛

 找回密码
 立即注册

WinAPI学习 基础篇 学习笔记_10

已有 261 次阅读2012-3-16 12:32

1.
图标资源
资源不能从程序源码中定义变量直接存取,windows提供函数
直接或间接的把它们加载内存以备使用
图标就是个文件
加载图标资源
HICON LoadIcon(
 HINSTANCE hInstance,  //窗口实例
 LPCTSTR lpIconName  //图标资源名称
);
//就是把图标的宏转换为 LoadIcon 中的类型
#define MAKEINTRESOURCE(i) (LPTSTR)((DWORD)((WORD)(i))) 
在.exe中像素是32*32  标题栏上是16*16
图标命名 2中
一种直接IDI_ICON  宏的模式
一种是命名用 字符串 "MYICON"
在程序中增加
static TCHAR iconName[] = "MYICON";
在LoadIcon中放入
//绘制图标
BOOL DrawIcon(
  HDC hdc
  int x,
  int y,
  HICON hicon
);
//得到一些系统信息  获得图标大小
int GetSystemMetrics(
  int nIndex
);
SM_CXICON, SM_CYICON
//动态修改图标
DWORD SetClassLong(
  HWND, hWnd,    //窗口句柄
  int nlndex,    //修改图标时值为GCL_HICON
  LONG dwNewLong   //新的图标句柄
);
GCL_CBCLSEXTRA  Sets the size, in bytes, of the extra memory associated with the class. Setting this value does not change the number of extra bytes already allocated.
GCL_CBWNDEXTRA  Sets the size, in bytes, of the extra window memory associated with each window in the class. Setting this value does not change the number of extra bytes already allocated. For information on how to access this memory, see SetWindowLong.
GCL_HBRBACKGROUND Replaces a handle to the background brush associated with the class.
GCL_HCURSOR   Replaces a handle to the cursor associated with the class.
GCL_HICON   Replaces a handle to the icon associated with the class.
GCL_HICONSM   Replace a handle to the small icon associated with the class.
GCL_HMODULE   Replaces a handle to the module that registered the class.
GCL_MENUNAME  Replaces the address of the menu name string. The string identifies the menu resource associated with the class.
GCL_STYLE   Replaces the window-class style bits.
GCL_WNDPROC   Replaces the address of the window procedure associated with the class
 
/////////////////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "resource.h"
static TCHAR ICONNAME[] = "MYICON";
LRESULT CALLBACK MyWinProc(
         HWND hwnd,
         UINT uMsg,
         WPARAM wParam,
         LPARAM lParam
         );
int WINAPI WinMain(HINSTANCE hInstance,
       HINSTANCE hPrevInstance,
       LPSTR lpCmdLine,
       int nShowCmd)
{
 WNDCLASS wndClass;
 TCHAR* strClassName = TEXT("lovt");
 HWND  hWndMsg;
 MSG   msg;
 
 ZeroMemory(&wndClass, sizeof(WNDCLASS));
 wndClass.cbClsExtra = 0;
 wndClass.cbWndExtra = 0;
 wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
// wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
// wndClass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));
 wndClass.hIcon = LoadIcon(NULL, ICONNAME);
 wndClass.hInstance = hInstance;
 wndClass.lpfnWndProc = MyWinProc;
 wndClass.lpszClassName = strClassName;
 wndClass.lpszMenuName = NULL;
 wndClass.style = CS_HREDRAW | CS_VREDRAW;
 
 if(!RegisterClass(&wndClass))
 {
  MessageBox(NULL, TEXT("注册失败!"), TEXT("ERROR"), MB_OK);
 }
 
 hWndMsg = CreateWindow(strClassName,
  TEXT("图标"),
  WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
  100, 100, 400, 300,
  NULL,
  NULL,
  hInstance,
  NULL
  );
 ShowWindow(hWndMsg, nShowCmd);
 while(GetMessage(&msg, hWndMsg, 0, 0)) //从消息队列中获取消息 如果HWND 为NULL 获取系统消息队列
 {
  TranslateMessage(&msg);  //键盘消息转换
  DispatchMessage(&msg);  //消息传回windows windows调用消息处理函数
 }
 
}
#define WM_MYUSER  WM_USER+1       //非系统标识符
LRESULT CALLBACK MyWinProc(
         HWND hwnd,
         UINT uMsg,
         WPARAM wParam,
         LPARAM lParam )
{
 PAINTSTRUCT ps;
 HDC hdc;
 HICON hIcon;
 static HINSTANCE hInstance;
 static int iconHeight, iconWidth;
 switch(uMsg)
 {
 case WM_CREATE:
  hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
  iconWidth = GetSystemMetrics(SM_CXICON);
  iconHeight = GetSystemMetrics(SM_CYICON);
  return 0;
 case WM_PAINT:
  hdc = BeginPaint(hwnd, &ps);
  hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
  DrawIcon(hdc, 10, 50, hIcon);
  EndPaint(hwnd, &ps);
  return 0;
 case WM_LBUTTONDOWN:
  SetClassLong(hwnd, GCL_HICON,
   (LONG)LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)));
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0); //非队列消息
  return 0;
 default:
  break;
 }
 
 return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

 
 
 
 
 

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-17 11:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部