C语言粗略模拟列表框单击出现,单击消失,简陋的代码
#include <Windows.h>
#include "touwenjian.h" //自己要新建一个 名字为 touwenjian.h的头文件
HWND hWnd_Lable = NULL;
HINSTANCE g_hInstance;
HWND CheckBox_01,CheckBox_02;
static HWND ClickHwnd_01,ClickHwnd_02;
int zx=10,zy=10;
LRESULT WINAPI ClickWinProc_01(HWND,UINT,WPARAM,LPARAM);
LRESULT WINAPI ClickWinProc_02(HWND,UINT,WPARAM,LPARAM);
LRESULT WINAPI WinProc(HWND hWnd,UINT messageId,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hInstanceLp,LPSTR lpCmdLine,int nShowCmd){
WNDCLASSEX wx;
HWND hWnd = NULL;
MSG msg = {0};
int x=0,y=0; //存储屏幕的高度和宽度
//先定义窗口的样式
wx.style=CS_HREDRAW|CS_VREDRAW;
wx.cbSize = sizeof(WNDCLASSEX);
wx.cbClsExtra = 0;
wx.cbWndExtra = 0;
wx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
//(HBRUSH)GetStockObject(WHITE_BRUSH); //设置窗体背景为白色
//(HBRUSH)GetStockObject(BLACK_BRUSH); //设置窗体背景为黑色
wx.hCursor = NULL;
wx.hIcon = NULL;
wx.hIconSm = NULL;
wx.hInstance = hInstance;
wx.lpfnWndProc = WinProc;
wx.lpszClassName = "myFrom";
wx.lpszMenuName = NULL;
RegisterClassEx(&wx); //向系统注册窗口
//定义子窗口信息,因为只是简单的创建,没有重新定义wx,
wx.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,122,122)); //设置子窗口的背景颜色颜色
//wx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wx.lpfnWndProc = ClickWinProc_01;
wx.lpszClassName = "ClickFrom_01";
RegisterClassEx(&wx); //向系统注册子窗口
//定义子窗口信息,因为只是简单的创建,没有重新定义wx,
wx.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,122,122)); //设置子窗口的背景颜色颜色
//wx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wx.lpfnWndProc = ClickWinProc_02;
wx.lpszClassName = "ClickFrom_02";
RegisterClassEx(&wx); //向系统注册子窗口
x = GetSystemMetrics(SM_CXSCREEN); //获取屏幕宽度
y = GetSystemMetrics(SM_CYSCREEN); //获取屏幕高度
//在系统中创建窗口,并返回一个句柄
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"myFrom",("登录"),
WS_ICONIC|WS_SYSMENU, //创建一个最小化,并且拥有关闭按钮的窗口的样式
//WS_OVERLAPPEDWINDOW,
500,200,300,800,NULL,NULL,hInstance,NULL);
//判断窗口是否创建成功
if(hWnd==NULL){
MessageBox(NULL,"错误提示","窗口创建失败",MB_OK);
}
//显示窗口,并且以什么样的形式显示
ShowWindow(hWnd,1); //激活显示一个窗口
//消息处理
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg); //翻译消息
DispatchMessage(&msg); //分派消息
}
g_hInstance = hInstance;
return msg.message; //返回消息的ID
return 0;
}
LRESULT WINAPI WinProc(HWND hWnd,UINT messageId,WPARAM wParam,LPARAM lParam){
hWnd_Lable = hWnd;
switch(messageId){
case WM_CREATE:
{
// 创建子窗口1
ClickHwnd_01 = CreateWindow("ClickFrom_01","标题",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_DEFPUSHBUTTON | BS_PUSHBUTTON ,
10,0,50,50,
hWnd,
(HMENU)1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE ),
NULL);
// 创建子窗口2
ClickHwnd_02 = CreateWindow("ClickFrom_02","标题",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_DEFPUSHBUTTON | BS_PUSHBUTTON ,
10,90,50,50,
hWnd,
(HMENU)1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE ),
NULL);
//创建列表框
CheckBox_01 = CreateWindow("listbox","标题",
WS_VISIBLE | WS_CHILD | LBS_STANDARD ,
10,60,100,100,
hWnd_Lable,
(HMENU)2,
(HINSTANCE)GetWindowLong(hWnd_Lable, GWL_HINSTANCE ),
NULL);
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目1");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目2");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目3");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目4");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目5");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目6");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目7");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目8");
SendMessage(CheckBox_01,LB_ADDSTRING,0,(LPARAM)"项目9");
//设置子窗口2的位置
LPPOINT p = new POINT();
RECT rect;
int x= 0,y = 0;
GetClientRect(ClickHwnd_02,&rect); //调用函数获取窗体的尺寸
x=rect.right ; //获取到窗体的宽度
y=rect.bottom; //获取到窗体的高度
MapWindowPoints(ClickHwnd_02,hWnd_Lable,p,2);
//MoveWindow(ClickHwnd_02,p->x,p->y+y,50,50,TRUE); //改变子窗口的位置
//创建列表框
CheckBox_02 = CreateWindow("listbox","标题",
WS_VISIBLE | WS_CHILD | LBS_STANDARD ,
p->x,p->y + y+10,100,100,
hWnd_Lable,
(HMENU)3,
(HINSTANCE)GetWindowLong(hWnd_Lable, GWL_HINSTANCE ),
NULL);
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目1");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目2");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目3");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目4");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目5");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目6");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目7");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目8");
SendMessage(CheckBox_02,LB_ADDSTRING,0,(LPARAM)"项目9");
ShowWindow(CheckBox_01,SW_HIDE);
ShowWindow(CheckBox_02,SW_HIDE);
return 0;
}
case WM_PAINT:
{
HDC hdc=NULL,hdc_02=NULL,hdc_03 = NULL;
PAINTSTRUCT ps;
HBITMAP hbitmap;
//加载图片的路径
hbitmap=(HBITMAP)LoadImage(NULL,"..\\11\\1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hbitmap == NULL)
{
MessageBox(NULL,"图片加载出错","提示",MB_OK);
}
//获取窗口上的HDC设备句柄
hdc = BeginPaint(hWnd,&ps);
//存储HDC源设备句柄
hdc_02 = CreateCompatibleDC(NULL);
//加载图片到源设备中
SelectObject(hdc_02,hbitmap);
//存储HDC目标设备句柄
//hdc_01 = BeginPaint(hWnd,&ps);
//显示图片
// BitBlt(hdc,0,0,300,300,hdc_02,0,0,SRCCOPY);
// TextOut(hdc,50,50,"123",sizeof("123"));
hdc_03 = BeginPaint(ClickHwnd_01,&ps);
TextOut(hdc_03,5,5,"窗口1",sizeof("窗口1")-1);
hdc = NULL;
hdc = BeginPaint(ClickHwnd_02,&ps);
TextOut(hdc,5,5,"窗口2",sizeof("窗口2")-1);
DeleteDC(hdc_02);
DeleteDC(hdc_03);
EndPaint(hWnd,&ps);
/*
//创建存储HDC目标设备,源设备
//PAINTSTRUCT ps;
//创建存储图片信息
HBITMAP hbitmap;
//加载图片的路径
hbitmap=(HBITMAP)LoadImage(NULL,"C:\\Users\\rym\\Desktop\\1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
//存储HDC源设备句柄
hdc_02 = CreateCompatibleDC(NULL);
//加载图片到源设备中
SelectObject(hdc_02,hbitmap);
//存储HDC目标设备句柄
hdc_01 = BeginPaint(hWnd,&ps1);
//显示图片
BitBlt(hdc_01,0,0,200,200,hdc_02,0,0,SRCCOPY);
//ReleaseDC(hWnd,hdc);
EndPaint(hWnd,&ps);
EndPaint(hWnd,&ps1);
//DeleteDC(hdc_01);
DeleteDC(hdc_02);
*/
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd,messageId,wParam,lParam);
}
LRESULT WINAPI ClickWinProc_01(HWND hWnd,UINT messageId,WPARAM wParam,LPARAM lParam){
switch(messageId){
//处理子窗口的 WM_LBUTTONDOWN 消息
case WM_LBUTTONDOWN:
{
if(From1==0)
{
//改变子窗口的位置
LPPOINT p = new POINT();
RECT rect;
int x= 0,y = 0;
ShowWindow(CheckBox_01,SW_SHOWNORMAL); //显示子窗口
GetClientRect(CheckBox_01,&rect); //调用函数获取窗体的尺寸
x=rect.right ; //获取到窗体的宽度
y=rect.bottom; //获取到窗体的高度
MapWindowPoints(CheckBox_01,hWnd_Lable,p,2); //获取窗口在另一个窗口中的坐标 字窗口1下的列表框1
MoveWindow(ClickHwnd_02,p->x,p->y+y+10,50,50,TRUE); //改变子窗口的位置 子窗口2
/* int MapWindowPoints(HWND hWndFrom, HWND hWndTo,LPPOINT lpPoints, UINT cPoints);
这个函数,要弄清楚各个参数代表的意思不然很容易导致内存溢出,起哄 UINT代表的是无符号整型
*/
LPPOINT p1 = new POINT();
RECT rect1;
int c = 0;
GetClientRect(ClickHwnd_02,&rect1);
c = rect1.bottom;
MapWindowPoints(ClickHwnd_02,hWnd_Lable,p1,2); //获取窗口在另一个窗口中的坐标
MoveWindow(CheckBox_02,p1->x,p1->y+c+10,100,100 ,TRUE); //改变子窗口的位置
//根据设置,重画全部或者部分窗口
RedrawWindow(hWnd_Lable, NULL, NULL,
RDW_INTERNALPAINT); //即使窗口无效,也向其投递一条WM_PAINT消息
MessageBox( hWnd,"鼠标左键在子窗口客户区中按下!", "子窗口消息", MB_OK);
From1=1;
}
else
{
ShowWindow(CheckBox_01,SW_HIDE); //隐藏子窗口
From1 = 0;
LPPOINT p1 = new POINT();
//改变子窗口的位置
LPPOINT p = new POINT();
RECT rect;
int x= 0,y = 0;
GetClientRect(ClickHwnd_01,&rect); //调用函数获取窗体的尺寸
x=rect.right ; //获取到窗体的宽度
y=rect.bottom; //获取到窗体的高度
MapWindowPoints(ClickHwnd_01,hWnd_Lable,p,2); //获取窗口在另一个窗口中的坐标
MoveWindow(ClickHwnd_02,p->x,p->y+y+10,50,50,TRUE); //改变子窗口的位置
RECT rect1;
int c = 0;
GetClientRect(ClickHwnd_02,&rect1);
c = rect1.bottom;
MapWindowPoints(ClickHwnd_02,hWnd_Lable,p1,2); //获取窗口在另一个窗口中的坐标
MoveWindow(CheckBox_02,p1->x,p1->y+c+10,100,100 ,TRUE); //改变子窗口的位置
}
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd,messageId,wParam,lParam);
}
LRESULT WINAPI ClickWinProc_02(HWND hWnd,UINT messageId,WPARAM wParam,LPARAM lParam){
switch(messageId){
//处理子窗口的 WM_LBUTTONDOWN 消息
case WM_LBUTTONDOWN:
{
if(From2==0)
{
ShowWindow(CheckBox_02,SW_SHOWNORMAL); //显示子窗口
From2=1;
}
else
{
ShowWindow(CheckBox_02,SW_HIDE);
From2 = 0;
}
//根据设置,重画全部或者部分窗口
RedrawWindow(hWnd, NULL, NULL,
RDW_INTERNALPAINT); //即使窗口无效,也向其投递一条WM_PAINT消息
MessageBox( hWnd,"鼠标左键在子窗口客户区中按下!", "子窗口消息", MB_OK);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd,messageId,wParam,lParam);
}
-----------------------------------------------------------------------------------------
下面是 头文件touwenjian.h里
#include <stdio.h>
// 定义判断子窗体1和子窗体是否打开的变量
//初始值为0,0代表窗口是关闭的,1代表是打开的
int From1=0;
int From2=0;