我们知道在Win32程序中,大量使用如:WPARAM,LPARAM以及以H开头的各类句柄参数(HBRUSH,HPEN,HWND等)
这几个参数有时候可以作为int型使用,有时候可以用来传递大型的struct结构等,为什么这样多功能。下面用c语言对这几个参数进行简单的模拟,希望对c语言初学者有帮助。
/*Program By stn_lcd! Please run it in TC2.0*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define WPARAM long
#define LPARAM long
#define HWND long
#define DWORD int
#define OVERLAPPEDWINDOW 0x0c
#define WS_VISIBLE 0x02
#define WS_CAPTION 0x04
#define WS_BORDER 0x08
#define WS_MAXIMIZE 0x10
#define _MAKE(_pP) (WPARAM)(&_pP)
#define MAX_LENGTH 100
typedef struct demo{
int x;
int y;
}demo;
typedef struct demo2{
char a;
int x;
int xx;
}demo2;
typedef struct WNDCLASS {
int x;
int y;
int width;
int height;
DWORD dwtype;
char* title;
}WNDCLASS;
HWND CreateWindow(int x,int y,int width,int height,DWORD dwtype,char* title) {
WNDCLASS* wndclass;
wndclass=(WNDCLASS*)malloc(sizeof(WNDCLASS));
wndclass->x=x;
wndclass->y=y;
wndclass->width=width;
wndclass->height=height;
wndclass->dwtype=dwtype;
wndclass->title=(char*)malloc(MAX_LENGTH);
strcpy(wndclass->title,title);
return (HWND)wndclass;
}
void ShowWindow(HWND hWnd) {
WNDCLASS* win=(WNDCLASS*)hWnd;
printf("\nx:%d,y:%d,width:%d,height:%d,dwtype:0x%x,title:%s\n",
win->x,win->y,win->width,win->height,win->dwtype,win->title);
}
void DestroyWindow(HWND hWnd) {
WNDCLASS* win=(WNDCLASS*)hWnd;
free(win->title);
free(win);
}
void Disp(WPARAM wParam,LPARAM lParam) {
demo* dp1=(demo*)wParam;
demo2* dp2=(demo2*)wParam;
switch(lParam) {
case 0:
printf("\nx is:%d,y is:%d\n",dp1->x,dp1->y);
break;
case 1:
printf("\na is:%c,x is:%d,xx is:%d\n",dp2->a,dp2->x,dp2->xx);
break;
}
}
main() {
demo n;
demo2 n2;
demo* nx;
HWND hwnd;
clrscr();
n.x=13;
n.y=15;
n2.a='s';
n2.x=124;
n2.xx=127;
Disp(_MAKE(n),(LPARAM)0);
Disp(_MAKE(n2),(LPARAM)1);
nx=(demo*)malloc(sizeof(demo));
nx->x=24;
nx->y=222;
Disp((WPARAM)nx,(LPARAM)0);
hwnd=CreateWindow(10,20,100,300,WS_CAPTION|WS_BORDER|WS_VISIBLE,
"Hello Stn_Lcd!");
ShowWindow(hwnd);
DestroyWindow(hwnd);
getch();
}