首页 > 编程语言 >不用资源文件的 VC 窗口程序

不用资源文件的 VC 窗口程序

时间:2023-07-09 16:45:58浏览次数:41  
标签:wParam VC 窗口 lpw hWnd 程序 lpdit IDM return

1.单个文件的windows  VC程序,不用资源文件。在win32模板程序基础上改。

2. 菜单 和 热键 的代码添加

3. 模式对话框的代码添加。 

程序运行界面,最简的win32程序

 

 代码如下,单个文件,比较麻烦的就是对话框。

// NOT.cpp : 
//

//#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

//#include "resource.h"
#define IDR_MAINFRAME                    128
#define IDD_NOT_DIALOG                    102
#define IDD_ABOUTBOX                    103
#define IDC_NOT                            109
#define IDC_STATIC                        -1

#define IDM_FILE_NEW                     20001
#define IDM_FILE_EXIT                    20002
#define IDM_TEST_TEST1                   20011
#define IDM_TEST_TEST2                   20012
#define IDM_HELP                         20021
#define IDM_ABOUT                         20022

#define MAX_LOADSTRING 100




// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING] = _T("NOT");                                // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING] = _T("NOT_XGZ_2023");                // The title bar text

//xgz 
HMENU m_hMenu = NULL;
HMENU m_hMenuPopup = NULL;
int   InitMenu();
int OnCreate(HWND, UINT, WPARAM, LPARAM);
int OnPaint(HWND, UINT, WPARAM, LPARAM);
int OnRButtonUp(HWND, UINT, WPARAM, LPARAM);
int OnTest1(HWND, UINT, WPARAM, LPARAM);

HACCEL  InitAccel();


// Foward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     MSG msg;
    
    HACCEL hAccelTable;
        
    MyRegisterClass(hInstance);

    if (!InitInstance (hInstance, nCmdShow)) 
    {
        return FALSE;
    }

    //hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_NOT);   //WM_SYSCOMMAND
    
    hAccelTable = InitAccel();


    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(NULL, IDI_APPLICATION); //标准图标
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(NULL, IDI_APPLICATION); //标准图标 

    return RegisterClassEx(&wcex);
}

int   InitMenu()
{
    HMENU hMenu,hMenuPopup;

    hMenu = CreateMenu();
    m_hMenu = hMenu;    //主菜单
    hMenuPopup = CreateMenu();
    AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_NEW, _T("&New"));
    AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
    AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_EXIT, _T("&Exit"));
    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, _T("&File"));  //File
    
    hMenuPopup = CreateMenu();
    m_hMenuPopup = hMenuPopup; //选一个做主程序的右键弹出菜单
    AppendMenu(hMenuPopup, MF_STRING, IDM_TEST_TEST1, _T("&Test1"));
    AppendMenu(hMenuPopup, MF_STRING, IDM_TEST_TEST2, _T("&Test2"));
    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, _T("&Test")); //Test

    AppendMenu(hMenu, MF_STRING, IDM_HELP, _T("&Help")); 
    AppendMenu(hMenu, MF_STRING, IDM_ABOUT, _T("&About")); 
        
    return 0;
}

HACCEL  InitAccel()
{
    ACCEL AccelTable[3] = {
        {FVIRTKEY | FCONTROL,    VK_UP,    IDM_TEST_TEST1},
        {FVIRTKEY | FALT,        VK_F2,    IDM_TEST_TEST2},
        0
    };
    HACCEL hAccel = CreateAcceleratorTable(AccelTable, 2);
    return hAccel;
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   InitMenu();
   SetMenu(hWnd, m_hMenu);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

#define ID_HELP   150
#define ID_TEXT   200
#define ID_EDIT   1200

LPWORD lpwAlign(LPWORD lpIn)
{
    ULONG ul;

    ul = (ULONG)lpIn;
    ul += 3;
    ul >>= 2;
    ul <<= 2;
    return (LPWORD)ul;
}



LRESULT InputBox(HINSTANCE hinst, HWND hwndOwner,
    LPTSTR lpszMessage, void* p)
{
    HGLOBAL hgbl;
    LPDLGTEMPLATE lpdt;
    LPDLGITEMTEMPLATE lpdit;
    LPWORD lpw;
    LPWSTR lpwsz;
    LRESULT ret;
    int nchar;

    hgbl = GlobalAlloc(GMEM_ZEROINIT, 2024);
    if (!hgbl)
        return -1;

    lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);

    //=====窗体============.
    lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU
        | DS_MODALFRAME | WS_CAPTION;
    lpdt->cdit = 4;  // number of controls
    lpdt->x = 100;  lpdt->y = 50;
    lpdt->cx = 200; lpdt->cy = 150;

    lpw = (LPWORD)(lpdt + 1);
    *lpw++ = 0;   // no menu
    *lpw++ = 0;   // predefined dialog box class (by default)

    wcscpy((wchar_t*)lpw, L"DialogBoxIndirect");
    nchar = wcslen((wchar_t*)lpw) + 1;
    lpw += nchar;

    //=======static text =============
    lpw = lpwAlign(lpw); // align DLGITEMTEMPLATE on DWORD boundary
    lpdit = (LPDLGITEMTEMPLATE)lpw;
    lpdit->x = 10; lpdit->y = 10;
    lpdit->cx = 180; lpdit->cy = 20;
    lpdit->id = ID_TEXT;  // text identifier
    lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;

    lpw = (LPWORD)(lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0082;                         // static class
        
    lpwsz = (LPWSTR)lpw;
    wcscpy((wchar_t*)lpw, lpszMessage); //初始化字符串
    nchar = wcslen((wchar_t*)lpw) + 1;
    lpw += nchar;

    lpw = lpwAlign(lpw); // align creation data on DWORD boundary
    *lpw++ = 0;           // no creation data
    
    //====== Edit =================
    lpw = lpwAlign(lpw); 
    lpdit = (LPDLGITEMTEMPLATE)lpw;
    lpdit->x = 10; lpdit->y = 40;
    lpdit->cx = 180; lpdit->cy = 50;
    lpdit->id = ID_EDIT;  // edit identifier
    lpdit->style = WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_MULTILINE | WS_VSCROLL;

    lpw = (LPWORD)(lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0081;    // Edit class

    lpwsz = (LPWSTR)lpw;
    wcscpy((wchar_t*)lpw, L"Input"); //初始化字符串
    nchar = wcslen((wchar_t*)lpw);  //XGZ 这里不能 +1,末尾的0被去掉了???
    lpw += nchar;
    
    lpw = lpwAlign(lpw); 
    *lpw++ = 0;           

    //========= OK button.==============
    lpw = lpwAlign(lpw); 
    lpdit = (LPDLGITEMTEMPLATE)lpw;
    lpdit->x = 80; lpdit->y = 120;
    lpdit->cx = 40; lpdit->cy = 20;
    lpdit->id = IDOK;  // OK button identifier
    lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;

    lpw = (LPWORD)(lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0080;    // button class

    lpwsz = (LPWSTR)lpw;
    wcscpy((wchar_t*)lpw, L"OK");
    nchar = wcslen((wchar_t*)lpw) + 1;
    lpw += nchar;
    lpw = lpwAlign(lpw); // align creation data on DWORD boundary
    *lpw++ = 0;           // no creation data

    //======CANCEL button.=========
    lpw = lpwAlign(lpw); 
    lpdit = (LPDLGITEMTEMPLATE)lpw;
    lpdit->x = 140; lpdit->y = 120;
    lpdit->cx = 40; lpdit->cy = 20;
    lpdit->id = IDCANCEL;  // CANCEL button identifier
    lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;

    lpw = (LPWORD)(lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0080;    // button class

    lpwsz = (LPWSTR)lpw;
    wcscpy((wchar_t*)lpw, L"CANCEL");
    nchar = wcslen((wchar_t*)lpw) + 1;
    lpw += nchar;
    lpw = lpwAlign(lpw); 
    *lpw++ = 0;           

    GlobalUnlock(hgbl);
    //ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE)hgbl,hwndOwner, (DLGPROC)About);
    //用带参数的方便扩展    
    ret = DialogBoxIndirectParam(hinst, (LPDLGTEMPLATE)hgbl,hwndOwner, (DLGPROC)About, (LPARAM)p);
    GlobalFree(hgbl);
    return ret;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;

    wchar_t p[100] = _T("This is a test");
    
    
    switch (message) 
    {
        case WM_CREATE:
            OnCreate(hWnd, message, wParam, lParam);
            break;
        case WM_PAINT:
            OnPaint(hWnd, message, wParam, lParam);
            break;
        case WM_RBUTTONUP:
            OnRButtonUp(hWnd, message, wParam, lParam);
            break;
        case WM_COMMAND:
            wmId    = LOWORD(wParam); 
            wmEvent = HIWORD(wParam); 
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_TEST_TEST1:
                    OnTest1(hWnd, message, wParam, lParam);
                    break;
                case IDM_HELP:
                    
                    break;
                case IDM_ABOUT:
                   //DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
                    InputBox(hInst, hWnd, _T("This is a DialogBoxIndirectParam sample."), (void*)p);
                    MessageBox(NULL, p, _T("Input"), MB_OK);
                                      
                   break;
                case IDM_FILE_EXIT:
                   DestroyWindow(hWnd);
                   break;
                default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

int OnCreate(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return 0;
}
int OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    RECT rt;
    GetClientRect(hWnd, &rt);
    DrawText(hdc, _T("NOT"), lstrlen(_T("NOT")), &rt, DT_CENTER);
    EndPaint(hWnd, &ps);
    
    return 0;
}

int OnRButtonUp(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    POINT point;

    //point.x = LOWORD(lParam);
    //point.y = LOWORD(lParam);
    //ClientToScreen(hWnd, &point);  //xgz 位置不对 ???

    GetCursorPos(&point);  //xgz 直接取屏幕坐标
    TrackPopupMenu(m_hMenuPopup, TPM_RIGHTBUTTON, point.x, point.y, 0, hWnd, NULL);

    return 0;
}

int OnTest1(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc = GetDC(hWnd);
    TCHAR* p = _T("Test1");
    TextOut(hdc, 10, 10, p, lstrlen(p));
    ReleaseDC(hWnd, hdc);
    return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND hWndEdit;
    wchar_t str[100];
    static void* p;
    
    switch (message)
    {
        case WM_INITDIALOG:

            //参数只能在INITDIALOG消息中传进,但是没接口传出,先用个静态变量保存指针
            wcscpy(str, (wchar_t*) lParam);
            SetDlgItemText(hDlg, ID_EDIT, str);
                
            p = (void*)lParam;
            return TRUE;
                
        case WM_PAINT:
            break;
        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
            {
                hWndEdit = GetDlgItem(hDlg, ID_EDIT);
                GetDlgItemText(hDlg, ID_EDIT, str, 100);
                str[99] = 0;
                wcscpy((wchar_t*)p, str);

                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }
    return FALSE;
}

 

标签:wParam,VC,窗口,lpw,hWnd,程序,lpdit,IDM,return
From: https://www.cnblogs.com/xgz21/p/17538926.html

相关文章

  • 算法题-生成窗口最大值数组
    https://leetcode.cn/problems/sliding-window-maximum/ classSolution{publicint[]maxSlidingWindow(int[]nums,intk){if(nums==null||nums.length==0||k<0){returnnull;}int[]result=newint[nums.length-k+1];......
  • 4. Q_ 设置_p_的_font_size_10rem_,当用户重置或拖曳浏览器窗口时,文本大小是
    Q:设置p的font-size:10rem,当用户重置或拖曳浏览器窗口时,文本大小是否会也随着变化?A:不会。rem是以html根元素中font-size的大小为基准的相对度量单位,文本的大小不会随着窗口的大小改变而改变。......
  • python打包Windows.exe程序(pyinstaller)
    python打包Windows.exe程序(pyinstaller)基础命令pipinstallpyinstaller使用pip命令来安装pyinstaller模块。-F:pyinstaller-Fhello.py-phello2.py-D:pyinstaller-Dhello.py-phello2.py-i:pyinstaller-itb.ico-Fhello.py-phello2.py其中前一个文件hel......
  • [AHK2] 轻松拖拽窗口
    使用:非最大化下,按住ctrlalt鼠标左键拖动如下:*<^<!LButton::{CoordMode"Mouse","Screen"MouseGetPos(&px,&py)WinGetPos(&wx,&wy,,,'A')dx:=wx-px,dy:=wy-pySetWinDelay-1WhileGet......
  • 小程序检查更新
    由于官方API没有提供主动下载新版本小程序的能力,仅提供了检测的能力。因此,当新版本下载失败时,没法主动触发重试,只能让用户继续访问旧版本的小程序。如果要马上应用最新版本,使用wx.getUpdateManagerAPI进行处理。在app.js里的onLaunch里加入如下代码onLaunch(){if(......
  • 小程序函数全集封装
    /***Createdbyzrs*/importVuefrom'vue'/***json拼接为字符串*@paramjson*@returns{string}*/exportfunctionqs(json){letstr=''for(letkeyinjson){if(json.hasOwnProperty(key)){str+=key+'......
  • windows编译ZLMediaKit(vcpkg)
    windows编译ZLMediaKit转载https://www.jianshu.com/p/f6f1c0b7e32b编译#下载ZLMediaKitgitclonehttps://gitee.com/xia-chu/ZLMediaKit.git#切换到ZLMediaKit目录cdZLMediaKit#更新子模块代码gitsubmoduleupdate--init#vcpkg安装opensslvcpkginstall--trip......
  • 长度最小的子数组滑动窗口
    /***给定一个含有n个正整数的数组和一个正整数s,找出该数组中满足其和≥s的长度最小的连续子数组,并返回其长度。如果不存在符合条件的子数组,返回0。**长度最小的子数组*示例:**输入:s=7,nums=[2,3,1,2,4,3]输出:2解释:子数......
  • .net core 6.0 mvc js对文件分片上传文件+控制器合并文件保存
    js,通过ajax将文件分片提交  遇到问题:.netcore6.0mvc上传文件位置主文件夹下的\bin\Debug\net6.0 文件访问不了问题在startup配置#region让upload文件夹可以外部访问stringsUploadPath="/upload";stringsFDir=ToolsBasic.UsPath(sUploadPath);if(!Direc......
  • 1. Vue程序初体验
    Vue是一个基于JavaScrip(tJS)实现的框架。要使用它就需要先拿到Vue的js文件。从Vue官网(https://v2.cn.vuejs.org/)下载vue.js文件1.下载并安装vue.js第一步:打开Vue2官网,点击下图所示的“起步”:第二步:继续点击下图所示的“安装”第三步:在“安装”页面向下滚动,......